| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "Nazarova_K_rad_sort_batcher_metod/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <tbb/blocked_range.h> | ||
| 4 | #include <tbb/parallel_reduce.h> | ||
| 5 | |||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <functional> | ||
| 9 | #include <limits> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "Nazarova_K_rad_sort_batcher_metod/common/include/common.hpp" | ||
| 13 | |||
| 14 | namespace nazarova_k_calc_integ_rectangles { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | bool HasValidInput(const InType &input) { |
| 19 | const std::size_t dimension = input.lower_bounds.size(); | ||
| 20 |
4/8✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 28 times.
|
28 | if (!input.function || dimension == 0U || input.upper_bounds.size() != dimension || input.steps.size() != dimension) { |
| 21 | return false; | ||
| 22 | } | ||
| 23 | |||
| 24 | std::size_t total_cells = 1U; | ||
| 25 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
|
84 | for (std::size_t i = 0; i < dimension; ++i) { |
| 26 |
4/8✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 56 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 56 times.
|
56 | if (input.steps[i] == 0U || !std::isfinite(input.lower_bounds[i]) || !std::isfinite(input.upper_bounds[i]) || |
| 27 | input.lower_bounds[i] > input.upper_bounds[i]) { | ||
| 28 | return false; | ||
| 29 | } | ||
| 30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
|
56 | if (total_cells > (std::numeric_limits<std::size_t>::max() / input.steps[i])) { |
| 31 | return false; | ||
| 32 | } | ||
| 33 | 56 | total_cells *= input.steps[i]; | |
| 34 | } | ||
| 35 | |||
| 36 | return true; | ||
| 37 | } | ||
| 38 | |||
| 39 | 497648 | void FillPointFromLinearIndex(const InType &input, const std::vector<double> &step_sizes, std::size_t linear_index, | |
| 40 | std::vector<double> &point) { | ||
| 41 | std::size_t current_index = linear_index; | ||
| 42 |
2/2✓ Branch 0 taken 1001456 times.
✓ Branch 1 taken 497648 times.
|
1499104 | for (std::size_t axis = 0U; axis < point.size(); ++axis) { |
| 43 | 1001456 | const std::size_t coordinate_index = current_index % input.steps[axis]; | |
| 44 | 1001456 | current_index /= input.steps[axis]; | |
| 45 | 1001456 | point[axis] = input.lower_bounds[axis] + ((static_cast<double>(coordinate_index) + 0.5) * step_sizes[axis]); | |
| 46 | } | ||
| 47 | 497648 | } | |
| 48 | |||
| 49 | } // namespace | ||
| 50 | |||
| 51 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | NazarovaKCalcIntegRectanglesTBB::NazarovaKCalcIntegRectanglesTBB(const InType &in) { |
| 52 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 53 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | GetInput() = in; |
| 54 | 28 | GetOutput() = {}; | |
| 55 | 28 | } | |
| 56 | |||
| 57 | 28 | bool NazarovaKCalcIntegRectanglesTBB::ValidationImpl() { | |
| 58 | 28 | return HasValidInput(GetInput()); | |
| 59 | } | ||
| 60 | |||
| 61 | 28 | bool NazarovaKCalcIntegRectanglesTBB::PreProcessingImpl() { | |
| 62 | const auto &input = GetInput(); | ||
| 63 | 28 | dimension_ = input.lower_bounds.size(); | |
| 64 | 28 | step_sizes_.assign(dimension_, 0.0); | |
| 65 | 28 | cell_volume_ = 1.0; | |
| 66 | 28 | total_cells_ = 1U; | |
| 67 | 28 | result_ = 0.0; | |
| 68 | |||
| 69 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
|
84 | for (std::size_t i = 0; i < dimension_; ++i) { |
| 70 | 56 | step_sizes_[i] = (input.upper_bounds[i] - input.lower_bounds[i]) / static_cast<double>(input.steps[i]); | |
| 71 | 56 | cell_volume_ *= step_sizes_[i]; | |
| 72 | 56 | total_cells_ *= input.steps[i]; | |
| 73 | } | ||
| 74 | |||
| 75 | 28 | GetOutput() = 0.0; | |
| 76 | 28 | return true; | |
| 77 | } | ||
| 78 | |||
| 79 | 28 | bool NazarovaKCalcIntegRectanglesTBB::RunImpl() { | |
| 80 | const auto &input = GetInput(); | ||
| 81 | 56 | const double sum = tbb::parallel_reduce(tbb::blocked_range<std::size_t>(0U, total_cells_), 0.0, | |
| 82 | 28 | [&](const tbb::blocked_range<std::size_t> &range, double local_sum) { | |
| 83 | 6491 | std::vector<double> point(dimension_, 0.0); | |
| 84 |
2/2✓ Branch 0 taken 497648 times.
✓ Branch 1 taken 6491 times.
|
504139 | for (std::size_t linear_index = range.begin(); linear_index < range.end(); ++linear_index) { |
| 85 | 497648 | FillPointFromLinearIndex(input, step_sizes_, linear_index, point); | |
| 86 | 497648 | local_sum += input.function(point); | |
| 87 | } | ||
| 88 | 6491 | return local_sum; | |
| 89 | 28 | }, std::plus<>()); | |
| 90 | |||
| 91 | 28 | result_ = sum * cell_volume_; | |
| 92 | 28 | GetOutput() = result_; | |
| 93 | 28 | return true; | |
| 94 | } | ||
| 95 | |||
| 96 | 28 | bool NazarovaKCalcIntegRectanglesTBB::PostProcessingImpl() { | |
| 97 | 28 | return true; | |
| 98 | } | ||
| 99 | |||
| 100 | } // namespace nazarova_k_calc_integ_rectangles | ||
| 101 |