| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "dergynov_s_integrals_multistep_rectangle/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <tbb/blocked_range.h> | ||
| 4 | #include <tbb/parallel_reduce.h> | ||
| 5 | |||
| 6 | #include <algorithm> | ||
| 7 | #include <cmath> | ||
| 8 | #include <cstddef> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "dergynov_s_integrals_multistep_rectangle/common/include/common.hpp" | ||
| 13 | |||
| 14 | namespace dergynov_s_integrals_multistep_rectangle { | ||
| 15 | namespace { | ||
| 16 | |||
| 17 | bool ValidateBorders(const std::vector<std::pair<double, double>> &borders) { | ||
| 18 | return std::ranges::all_of(borders, [](const auto &p) { | ||
| 19 | const auto &[left, right] = p; | ||
| 20 |
3/6✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 132 times.
✗ Branch 5 not taken.
|
132 | return std::isfinite(left) && std::isfinite(right) && left < right; |
| 21 | }); | ||
| 22 | } | ||
| 23 | |||
| 24 | } // namespace | ||
| 25 | |||
| 26 |
1/2✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
|
76 | DergynovSIntegralsMultistepRectangleTBB::DergynovSIntegralsMultistepRectangleTBB(const InType &in) { |
| 27 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 28 | GetInput() = in; | ||
| 29 | 76 | GetOutput() = 0.0; | |
| 30 | 76 | } | |
| 31 | |||
| 32 |
1/2✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
|
76 | bool DergynovSIntegralsMultistepRectangleTBB::ValidationImpl() { |
| 33 | const auto &[func, borders, n] = GetInput(); | ||
| 34 | |||
| 35 |
1/2✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
|
76 | if (!func) { |
| 36 | return false; | ||
| 37 | } | ||
| 38 |
1/2✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
|
76 | if (n <= 0) { |
| 39 | return false; | ||
| 40 | } | ||
| 41 |
1/2✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
|
76 | if (borders.empty()) { |
| 42 | return false; | ||
| 43 | } | ||
| 44 | |||
| 45 | return ValidateBorders(borders); | ||
| 46 | } | ||
| 47 | |||
| 48 | 76 | bool DergynovSIntegralsMultistepRectangleTBB::PreProcessingImpl() { | |
| 49 | 76 | GetOutput() = 0.0; | |
| 50 | 76 | return true; | |
| 51 | } | ||
| 52 | |||
| 53 | 76 | bool DergynovSIntegralsMultistepRectangleTBB::RunImpl() { | |
| 54 | const auto &input = GetInput(); | ||
| 55 | const auto &func = std::get<0>(input); | ||
| 56 | const auto &borders = std::get<1>(input); | ||
| 57 | 76 | const int n = std::get<2>(input); | |
| 58 | 76 | const int dim = static_cast<int>(borders.size()); | |
| 59 | |||
| 60 | 76 | std::vector<double> h(dim); | |
| 61 | double cell_volume = 1.0; | ||
| 62 | |||
| 63 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 76 times.
|
208 | for (int i = 0; i < dim; ++i) { |
| 64 | 132 | const double left = borders[i].first; | |
| 65 | 132 | const double right = borders[i].second; | |
| 66 | 132 | h[i] = (right - left) / n; | |
| 67 | 132 | cell_volume *= h[i]; | |
| 68 | } | ||
| 69 | |||
| 70 | size_t total_points = 1; | ||
| 71 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 76 times.
|
208 | for (int i = 0; i < dim; ++i) { |
| 72 | 132 | total_points *= n; | |
| 73 | } | ||
| 74 | |||
| 75 | 152 | double total_sum = tbb::parallel_reduce(tbb::blocked_range<size_t>(0, total_points), 0.0, | |
| 76 |
1/2✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
|
20757 | [&](const tbb::blocked_range<size_t> &range, double local_sum) { |
| 77 |
2/2✓ Branch 0 taken 5034800 times.
✓ Branch 1 taken 20681 times.
|
5055481 | for (size_t linear_idx = range.begin(); linear_idx != range.end(); ++linear_idx) { |
| 78 | size_t tmp = linear_idx; | ||
| 79 | 5034800 | std::vector<double> point(dim); | |
| 80 | |||
| 81 |
2/2✓ Branch 0 taken 14575600 times.
✓ Branch 1 taken 5034800 times.
|
19610400 | for (int dimension = dim - 1; dimension >= 0; --dimension) { |
| 82 | 14575600 | int idx_val = static_cast<int>(tmp % static_cast<size_t>(n)); | |
| 83 | 14575600 | tmp /= static_cast<size_t>(n); | |
| 84 | 14575600 | point[dimension] = borders[dimension].first + ((static_cast<double>(idx_val) + 0.5) * h[dimension]); | |
| 85 | } | ||
| 86 | |||
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5034800 times.
|
5034800 | double f_val = func(point); |
| 88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5034800 times.
|
5034800 | if (!std::isfinite(f_val)) { |
| 89 | return local_sum; | ||
| 90 | } | ||
| 91 |
1/2✓ Branch 0 taken 5034800 times.
✗ Branch 1 not taken.
|
5034800 | local_sum += f_val; |
| 92 | } | ||
| 93 | return local_sum; | ||
| 94 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
538 | }, [](double a, double b) { return a + b; }); |
| 95 | |||
| 96 |
1/2✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
|
76 | GetOutput() = total_sum * cell_volume; |
| 97 | 76 | return std::isfinite(GetOutput()); | |
| 98 | } | ||
| 99 | |||
| 100 | 76 | bool DergynovSIntegralsMultistepRectangleTBB::PostProcessingImpl() { | |
| 101 | 76 | return std::isfinite(GetOutput()); | |
| 102 | } | ||
| 103 | |||
| 104 | } // namespace dergynov_s_integrals_multistep_rectangle | ||
| 105 |