| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "redkina_a_integral_simpson/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <oneapi/tbb.h> | ||
| 4 | |||
| 5 | #include <cmath> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <functional> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "redkina_a_integral_simpson/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace redkina_a_integral_simpson { | ||
| 13 | namespace { | ||
| 14 | |||
| 15 | 80 | std::vector<std::vector<double>> PrecomputeWeights(const std::vector<int> &n) { | |
| 16 | const size_t dim = n.size(); | ||
| 17 | 80 | std::vector<std::vector<double>> weights(dim); | |
| 18 |
2/2✓ Branch 0 taken 152 times.
✓ Branch 1 taken 80 times.
|
232 | for (size_t i = 0; i < dim; ++i) { |
| 19 |
1/2✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
|
152 | const int ni = n[i]; |
| 20 |
1/2✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
|
152 | weights[i].resize(ni + 1); |
| 21 |
2/2✓ Branch 0 taken 9624 times.
✓ Branch 1 taken 152 times.
|
9776 | for (int idx = 0; idx <= ni; ++idx) { |
| 22 |
2/2✓ Branch 0 taken 304 times.
✓ Branch 1 taken 9320 times.
|
9624 | if (idx == 0 || idx == ni) { |
| 23 | 304 | weights[i][idx] = 1.0; | |
| 24 |
2/2✓ Branch 0 taken 4736 times.
✓ Branch 1 taken 4584 times.
|
9320 | } else if (idx % 2 == 1) { |
| 25 | 4736 | weights[i][idx] = 4.0; | |
| 26 | } else { | ||
| 27 | 4584 | weights[i][idx] = 2.0; | |
| 28 | } | ||
| 29 | } | ||
| 30 | } | ||
| 31 | 80 | return weights; | |
| 32 | ✗ | } | |
| 33 | |||
| 34 | 80 | std::vector<size_t> ComputeStrides(const std::vector<int> &n) { | |
| 35 | const size_t dim = n.size(); | ||
| 36 | 80 | std::vector<size_t> strides(dim); | |
| 37 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (dim == 0) { |
| 38 | return strides; | ||
| 39 | } | ||
| 40 | 80 | strides[dim - 1] = 1; | |
| 41 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 80 times.
|
152 | for (size_t i = dim - 1; i > 0; --i) { |
| 42 | 72 | strides[i - 1] = strides[i] * static_cast<size_t>(n[i] + 1); | |
| 43 | } | ||
| 44 | return strides; | ||
| 45 | } | ||
| 46 | |||
| 47 | } // namespace | ||
| 48 | |||
| 49 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | RedkinaAIntegralSimpsonTBB::RedkinaAIntegralSimpsonTBB(const InType &in) { |
| 50 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 51 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | GetInput() = in; |
| 52 | 80 | } | |
| 53 | |||
| 54 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | bool RedkinaAIntegralSimpsonTBB::ValidationImpl() { |
| 55 | const auto &in = GetInput(); | ||
| 56 | size_t dim = in.a.size(); | ||
| 57 | |||
| 58 |
3/6✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 80 times.
|
80 | if (dim == 0 || in.b.size() != dim || in.n.size() != dim) { |
| 59 | return false; | ||
| 60 | } | ||
| 61 |
2/2✓ Branch 0 taken 152 times.
✓ Branch 1 taken 80 times.
|
232 | for (size_t i = 0; i < dim; ++i) { |
| 62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 152 times.
|
152 | if (in.a[i] >= in.b[i]) { |
| 63 | return false; | ||
| 64 | } | ||
| 65 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 152 times.
|
152 | if (in.n[i] <= 0 || in.n[i] % 2 != 0) { |
| 66 | return false; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | 80 | return static_cast<bool>(in.func); | |
| 70 | } | ||
| 71 | |||
| 72 | 80 | bool RedkinaAIntegralSimpsonTBB::PreProcessingImpl() { | |
| 73 | const auto &in = GetInput(); | ||
| 74 | 80 | func_ = in.func; | |
| 75 | 80 | a_ = in.a; | |
| 76 | 80 | b_ = in.b; | |
| 77 | 80 | n_ = in.n; | |
| 78 | 80 | result_ = 0.0; | |
| 79 | 80 | return true; | |
| 80 | } | ||
| 81 | |||
| 82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | bool RedkinaAIntegralSimpsonTBB::RunImpl() { |
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (!func_) { |
| 84 | return false; | ||
| 85 | } | ||
| 86 | 80 | const size_t dim = a_.size(); | |
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (dim == 0) { |
| 88 | return false; | ||
| 89 | } | ||
| 90 | |||
| 91 | 80 | std::vector<double> h(dim); | |
| 92 | double h_prod = 1.0; | ||
| 93 |
2/2✓ Branch 0 taken 152 times.
✓ Branch 1 taken 80 times.
|
232 | for (size_t i = 0; i < dim; ++i) { |
| 94 | 152 | h[i] = (b_[i] - a_[i]) / static_cast<double>(n_[i]); | |
| 95 | 152 | h_prod *= h[i]; | |
| 96 | } | ||
| 97 | |||
| 98 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | const auto weights = PrecomputeWeights(n_); |
| 99 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | const auto strides = ComputeStrides(n_); |
| 100 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | if (strides.empty()) { |
| 101 | return false; | ||
| 102 | } | ||
| 103 | |||
| 104 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | const size_t total_points = strides[0] * static_cast<size_t>(n_[0] + 1); |
| 105 | |||
| 106 | 160 | double sum = tbb::parallel_reduce(tbb::blocked_range<size_t>(0, total_points), 0.0, | |
| 107 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | [&](const tbb::blocked_range<size_t> &range, double local_sum) { |
| 108 | 10021 | std::vector<int> indices(dim); | |
| 109 |
1/4✓ Branch 1 taken 10021 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
10021 | std::vector<double> point(dim); |
| 110 | |||
| 111 |
2/2✓ Branch 0 taken 925136 times.
✓ Branch 1 taken 10021 times.
|
935157 | for (size_t linear_idx = range.begin(); linear_idx != range.end(); ++linear_idx) { |
| 112 | size_t remainder = linear_idx; | ||
| 113 |
2/2✓ Branch 0 taken 2123928 times.
✓ Branch 1 taken 925136 times.
|
3049064 | for (size_t dim_idx = 0; dim_idx < dim; ++dim_idx) { |
| 114 | 2123928 | indices[dim_idx] = static_cast<int>(remainder / strides[dim_idx]); | |
| 115 | 2123928 | remainder %= strides[dim_idx]; | |
| 116 | } | ||
| 117 | |||
| 118 | double w_prod = 1.0; | ||
| 119 |
2/2✓ Branch 0 taken 2123928 times.
✓ Branch 1 taken 925136 times.
|
3049064 | for (size_t dim_idx = 0; dim_idx < dim; ++dim_idx) { |
| 120 | 2123928 | const int i_idx = indices[dim_idx]; | |
| 121 | 2123928 | point[dim_idx] = a_[dim_idx] + (static_cast<double>(i_idx) * h[dim_idx]); | |
| 122 | 2123928 | w_prod *= weights[dim_idx][i_idx]; | |
| 123 | } | ||
| 124 | |||
| 125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 925136 times.
|
1850272 | local_sum += w_prod * func_(point); |
| 126 | } | ||
| 127 | 10021 | return local_sum; | |
| 128 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
278 | }, [](double x, double y) -> double { return x + y; }); |
| 129 | |||
| 130 | double denominator = 1.0; | ||
| 131 |
2/2✓ Branch 0 taken 152 times.
✓ Branch 1 taken 80 times.
|
232 | for (size_t i = 0; i < dim; ++i) { |
| 132 | 152 | denominator *= 3.0; | |
| 133 | } | ||
| 134 | |||
| 135 | 80 | result_ = (h_prod / denominator) * sum; | |
| 136 | 80 | return true; | |
| 137 | 80 | } | |
| 138 | |||
| 139 | 80 | bool RedkinaAIntegralSimpsonTBB::PostProcessingImpl() { | |
| 140 | 80 | GetOutput() = result_; | |
| 141 | 80 | return true; | |
| 142 | } | ||
| 143 | |||
| 144 | } // namespace redkina_a_integral_simpson | ||
| 145 |