| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "zyuzin_n_multi_integrals_simpson/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <thread> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "oneapi/tbb/blocked_range.h" | ||
| 9 | #include "oneapi/tbb/global_control.h" | ||
| 10 | #include "oneapi/tbb/parallel_reduce.h" | ||
| 11 | #include "zyuzin_n_multi_integrals_simpson/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace zyuzin_n_multi_integrals_simpson { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | ZyuzinNSimpsonTBB::ZyuzinNSimpsonTBB(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | GetInput() = in; |
| 18 | 48 | GetOutput() = 0.0; | |
| 19 | 48 | } | |
| 20 | |||
| 21 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | bool ZyuzinNSimpsonTBB::ValidationImpl() { |
| 22 | const auto &input = GetInput(); | ||
| 23 |
2/4✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
|
48 | if (input.lower_bounds.size() != input.upper_bounds.size() || input.lower_bounds.size() != input.n_steps.size()) { |
| 24 | return false; | ||
| 25 | } | ||
| 26 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (input.lower_bounds.empty()) { |
| 27 | return false; | ||
| 28 | } | ||
| 29 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
|
144 | for (size_t i = 0; i < input.lower_bounds.size(); ++i) { |
| 30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
|
96 | if (input.lower_bounds[i] > input.upper_bounds[i]) { |
| 31 | return false; | ||
| 32 | } | ||
| 33 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
|
96 | if (input.n_steps[i] <= 0 || input.n_steps[i] % 2 != 0) { |
| 34 | return false; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | 48 | return static_cast<bool>(input.func); | |
| 38 | } | ||
| 39 | |||
| 40 | 48 | bool ZyuzinNSimpsonTBB::PreProcessingImpl() { | |
| 41 | 48 | result_ = 0.0; | |
| 42 | 48 | return true; | |
| 43 | } | ||
| 44 | |||
| 45 | ✗ | double ZyuzinNSimpsonTBB::GetSimpsonWeight(int index, int n) { | |
| 46 |
2/4✓ Branch 0 taken 407584 times.
✓ Branch 1 taken 36272 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
443856 | if (index == 0 || index == n) { |
| 47 | return 1.0; | ||
| 48 | } | ||
| 49 |
2/4✓ Branch 0 taken 212860 times.
✓ Branch 1 taken 194724 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
407584 | if (index % 2 == 1) { |
| 50 | 212860 | return 4.0; | |
| 51 | } | ||
| 52 | return 2.0; | ||
| 53 | } | ||
| 54 | |||
| 55 | 48 | double ZyuzinNSimpsonTBB::ComputeSimpsonMultiDim() { | |
| 56 | const auto &input = GetInput(); | ||
| 57 | 48 | const size_t num_dims = input.lower_bounds.size(); | |
| 58 | |||
| 59 | 48 | std::vector<double> h(num_dims); | |
| 60 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
|
144 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 61 | 96 | h[dim] = (input.upper_bounds[dim] - input.lower_bounds[dim]) / input.n_steps[dim]; | |
| 62 | } | ||
| 63 | |||
| 64 | size_t total_points = 1; | ||
| 65 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
|
144 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 66 | 96 | total_points *= static_cast<size_t>(input.n_steps[dim] + 1); | |
| 67 | } | ||
| 68 | |||
| 69 | 48 | const int max_threads = static_cast<int>(std::max(1U, std::thread::hardware_concurrency())); | |
| 70 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | oneapi::tbb::global_control thread_limiter(oneapi::tbb::global_control::max_allowed_parallelism, max_threads); |
| 71 | constexpr size_t kGrainSize = 4096; | ||
| 72 | const double sum = | ||
| 73 | 96 | oneapi::tbb::parallel_reduce(oneapi::tbb::blocked_range<size_t>(0, total_points, kGrainSize), 0.0, | |
| 74 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | [&](const oneapi::tbb::blocked_range<size_t> &range, double local_sum) { |
| 75 | 84 | std::vector<double> point(num_dims); | |
| 76 |
2/2✓ Branch 0 taken 166968 times.
✓ Branch 1 taken 84 times.
|
167052 | for (size_t point_idx = range.begin(); point_idx < range.end(); ++point_idx) { |
| 77 | auto temp = point_idx; | ||
| 78 | double weight = 1.0; | ||
| 79 |
2/2✓ Branch 0 taken 443856 times.
✓ Branch 1 taken 166968 times.
|
610824 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 80 |
2/2✓ Branch 0 taken 407584 times.
✓ Branch 1 taken 36272 times.
|
443856 | const auto axis_points = static_cast<size_t>(input.n_steps[dim]) + 1U; |
| 81 | 443856 | const auto index = static_cast<int>(temp % axis_points); | |
| 82 | 443856 | temp /= axis_points; | |
| 83 |
2/2✓ Branch 0 taken 407584 times.
✓ Branch 1 taken 36272 times.
|
443856 | point[dim] = input.lower_bounds[dim] + (static_cast<double>(index) * h[dim]); |
| 84 | 443856 | weight *= GetSimpsonWeight(index, input.n_steps[dim]); | |
| 85 | } | ||
| 86 | |||
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 166968 times.
|
333936 | local_sum += weight * input.func(point); |
| 88 | } | ||
| 89 | 84 | return local_sum; | |
| 90 | 21 | }, [](double left, double right) { return left + right; }); | |
| 91 | |||
| 92 | double factor = 1.0; | ||
| 93 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
|
144 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 94 | 96 | factor *= h[dim] / 3.0; | |
| 95 | } | ||
| 96 | |||
| 97 | 96 | return sum * factor; | |
| 98 | } | ||
| 99 | |||
| 100 | 48 | bool ZyuzinNSimpsonTBB::RunImpl() { | |
| 101 | 48 | result_ = ComputeSimpsonMultiDim(); | |
| 102 | 48 | return true; | |
| 103 | } | ||
| 104 | |||
| 105 | 48 | bool ZyuzinNSimpsonTBB::PostProcessingImpl() { | |
| 106 | 48 | GetOutput() = result_; | |
| 107 | 48 | return true; | |
| 108 | } | ||
| 109 | |||
| 110 | } // namespace zyuzin_n_multi_integrals_simpson | ||
| 111 |