| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "zyuzin_n_multi_integrals_simpson/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <numeric> | ||
| 6 | #include <thread> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "util/include/util.hpp" | ||
| 10 | #include "zyuzin_n_multi_integrals_simpson/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace zyuzin_n_multi_integrals_simpson { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
96 | ZyuzinNSimpsonSTL::ZyuzinNSimpsonSTL(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
96 | GetInput() = in; |
| 17 | 96 | GetOutput() = 0.0; | |
| 18 | 96 | } | |
| 19 | |||
| 20 |
1/2✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
|
96 | bool ZyuzinNSimpsonSTL::ValidationImpl() { |
| 21 | const auto &input = GetInput(); | ||
| 22 |
2/4✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 96 times.
|
96 | if (input.lower_bounds.size() != input.upper_bounds.size() || input.lower_bounds.size() != input.n_steps.size()) { |
| 23 | return false; | ||
| 24 | } | ||
| 25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
|
96 | if (input.lower_bounds.empty()) { |
| 26 | return false; | ||
| 27 | } | ||
| 28 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 96 times.
|
288 | for (size_t i = 0; i < input.lower_bounds.size(); ++i) { |
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
|
192 | if (input.lower_bounds[i] > input.upper_bounds[i]) { |
| 30 | return false; | ||
| 31 | } | ||
| 32 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 192 times.
|
192 | if (input.n_steps[i] <= 0 || input.n_steps[i] % 2 != 0) { |
| 33 | return false; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | 96 | return static_cast<bool>(input.func); | |
| 37 | } | ||
| 38 | |||
| 39 | 96 | bool ZyuzinNSimpsonSTL::PreProcessingImpl() { | |
| 40 | 96 | result_ = 0.0; | |
| 41 | 96 | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | ✗ | double ZyuzinNSimpsonSTL::GetSimpsonWeight(int index, int n) { | |
| 45 |
2/4✓ Branch 0 taken 815168 times.
✓ Branch 1 taken 72544 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
887712 | if (index == 0 || index == n) { |
| 46 | return 1.0; | ||
| 47 | } | ||
| 48 |
2/4✓ Branch 0 taken 425720 times.
✓ Branch 1 taken 389448 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
815168 | if (index % 2 == 1) { |
| 49 | 425720 | return 4.0; | |
| 50 | } | ||
| 51 | return 2.0; | ||
| 52 | } | ||
| 53 | |||
| 54 | 96 | double ZyuzinNSimpsonSTL::ComputeSimpsonMultiDim() { | |
| 55 | const auto &input = GetInput(); | ||
| 56 | 96 | const size_t num_dims = input.lower_bounds.size(); | |
| 57 | |||
| 58 | 96 | std::vector<double> h(num_dims); | |
| 59 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 96 times.
|
288 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 60 | 192 | h[dim] = (input.upper_bounds[dim] - input.lower_bounds[dim]) / input.n_steps[dim]; | |
| 61 | } | ||
| 62 | |||
| 63 | 96 | size_t total_points = 1; | |
| 64 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 96 times.
|
288 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 65 | 192 | total_points *= static_cast<size_t>(input.n_steps[dim]) + 1U; | |
| 66 | } | ||
| 67 | |||
| 68 |
2/4✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 96 times.
✗ Branch 5 not taken.
|
96 | const size_t requested_threads = static_cast<size_t>(std::max(1, ppc::util::GetNumThreads())); |
| 69 | 96 | const size_t num_threads = std::min(requested_threads, total_points); | |
| 70 | |||
| 71 |
1/4✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
96 | std::vector<double> partial_sums(num_threads, 0.0); |
| 72 | 96 | std::vector<std::thread> workers; | |
| 73 |
2/4✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 96 times.
✗ Branch 4 not taken.
|
96 | workers.reserve(num_threads > 0 ? num_threads - 1 : 0); |
| 74 | |||
| 75 | 240 | auto compute_chunk = [&](size_t thread_id) { | |
| 76 | 240 | const size_t begin = (thread_id * total_points) / num_threads; | |
| 77 | 240 | const size_t end = ((thread_id + 1) * total_points) / num_threads; | |
| 78 | |||
| 79 | 240 | std::vector<double> point(num_dims); | |
| 80 | double local_sum = 0.0; | ||
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 333936 times.
✓ Branch 1 taken 240 times.
|
334176 | for (size_t point_idx = begin; point_idx < end; ++point_idx) { |
| 83 | auto temp = point_idx; | ||
| 84 | double weight = 1.0; | ||
| 85 | |||
| 86 |
2/2✓ Branch 0 taken 887712 times.
✓ Branch 1 taken 333936 times.
|
1221648 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 87 |
2/2✓ Branch 0 taken 815168 times.
✓ Branch 1 taken 72544 times.
|
887712 | const auto axis_points = static_cast<size_t>(input.n_steps[dim]) + 1U; |
| 88 | 887712 | const auto index = static_cast<int>(temp % axis_points); | |
| 89 | 887712 | temp /= axis_points; | |
| 90 |
2/2✓ Branch 0 taken 815168 times.
✓ Branch 1 taken 72544 times.
|
887712 | point[dim] = input.lower_bounds[dim] + (static_cast<double>(index) * h[dim]); |
| 91 | 887712 | weight *= GetSimpsonWeight(index, input.n_steps[dim]); | |
| 92 | } | ||
| 93 | |||
| 94 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 333936 times.
|
667872 | local_sum += weight * input.func(point); |
| 95 | } | ||
| 96 | |||
| 97 |
1/2✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
|
240 | partial_sums[thread_id] = local_sum; |
| 98 | 240 | }; | |
| 99 | |||
| 100 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 96 times.
|
240 | for (size_t thread_id = 1; thread_id < num_threads; ++thread_id) { |
| 101 |
1/2✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
|
144 | workers.emplace_back(compute_chunk, thread_id); |
| 102 | } | ||
| 103 |
1/2✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
|
96 | compute_chunk(0); |
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 96 times.
|
240 | for (auto &worker : workers) { |
| 106 |
1/2✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
|
144 | worker.join(); |
| 107 | } | ||
| 108 | |||
| 109 | const double sum = std::accumulate(partial_sums.begin(), partial_sums.end(), 0.0); | ||
| 110 | |||
| 111 | double factor = 1.0; | ||
| 112 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 96 times.
|
288 | for (size_t dim = 0; dim < num_dims; ++dim) { |
| 113 | 192 | factor *= h[dim] / 3.0; | |
| 114 | } | ||
| 115 | |||
| 116 | 96 | return sum * factor; | |
| 117 | 96 | } | |
| 118 | |||
| 119 | 96 | bool ZyuzinNSimpsonSTL::RunImpl() { | |
| 120 | 96 | result_ = ComputeSimpsonMultiDim(); | |
| 121 | 96 | return true; | |
| 122 | } | ||
| 123 | |||
| 124 | 96 | bool ZyuzinNSimpsonSTL::PostProcessingImpl() { | |
| 125 | 96 | GetOutput() = result_; | |
| 126 | 96 | return true; | |
| 127 | } | ||
| 128 | |||
| 129 | } // namespace zyuzin_n_multi_integrals_simpson | ||
| 130 |