| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "../include/rect_method_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <numeric> | ||
| 7 | #include <thread> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "../../common/include/common.hpp" | ||
| 12 | #include "util/include/util.hpp" | ||
| 13 | |||
| 14 | namespace kutergin_v_multidimensional_integration_rect_method { | ||
| 15 | |||
| 16 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | RectMethodSTL::RectMethodSTL(const InType &in) { |
| 17 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 18 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | GetInput() = in; |
| 19 | 40 | GetOutput() = 0.0; | |
| 20 | 40 | } | |
| 21 | |||
| 22 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | bool RectMethodSTL::ValidationImpl() { |
| 23 | const auto &input = GetInput(); | ||
| 24 |
2/4✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
|
40 | if (input.limits.size() != input.n_steps.size() || input.limits.empty()) { |
| 25 | return false; | ||
| 26 | } | ||
| 27 | return std::ranges::all_of(input.n_steps, [](int n) { return n > 0; }); | ||
| 28 | } | ||
| 29 | |||
| 30 | 40 | bool RectMethodSTL::PreProcessingImpl() { | |
| 31 | 40 | local_input_ = GetInput(); | |
| 32 | 40 | res_ = 0.0; | |
| 33 | 40 | return true; | |
| 34 | } | ||
| 35 | |||
| 36 | 40 | bool RectMethodSTL::RunImpl() { | |
| 37 | size_t dims = local_input_.limits.size(); // число размерностей пространства | ||
| 38 | |||
| 39 | // вычисление числа шагов и общего числа итераций | ||
| 40 | size_t total_iterations = 1; | ||
| 41 | 40 | std::vector<double> h(dims); | |
| 42 | double d_v = 1.0; | ||
| 43 | |||
| 44 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 40 times.
|
160 | for (size_t i = 0; i < dims; i++) { |
| 45 | 120 | total_iterations *= local_input_.n_steps[i]; | |
| 46 | 120 | h[i] = (local_input_.limits[i].second - local_input_.limits[i].first) / local_input_.n_steps[i]; | |
| 47 | 120 | d_v *= h[i]; | |
| 48 | } | ||
| 49 | |||
| 50 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | int num_threads = ppc::util::GetNumThreads(); |
| 51 | |||
| 52 | 40 | std::vector<std::thread> threads; | |
| 53 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | std::vector<double> partial_sums(num_threads, 0.0); |
| 54 | |||
| 55 | 40 | size_t chunk = total_iterations / num_threads; | |
| 56 | 40 | size_t remainder = total_iterations % num_threads; | |
| 57 | |||
| 58 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 40 times.
|
140 | for (int i = 0; i < num_threads; ++i) { |
| 59 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 22 times.
|
100 | size_t start = (i * chunk) + std::min(static_cast<size_t>(i), remainder); |
| 60 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 22 times.
|
100 | size_t end = start + chunk + (std::cmp_less(static_cast<size_t>(i), remainder) ? 1 : 0); |
| 61 | |||
| 62 |
1/2✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
|
100 | if (start < end) { |
| 63 | 100 | threads.emplace_back( | |
| 64 |
1/4✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
200 | [this, start, end, &h, &partial_sums, i]() { partial_sums[i] = CalculateChunkSum(start, end, h); }); |
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 40 times.
|
140 | for (auto &t : threads) { |
| 69 |
1/2✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
|
100 | if (t.joinable()) { |
| 70 |
1/2✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
|
100 | t.join(); |
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | double total_sum = std::accumulate(partial_sums.begin(), partial_sums.end(), 0.0); | ||
| 75 |
1/2✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
|
40 | res_ = total_sum * d_v; |
| 76 | 40 | return true; | |
| 77 | 40 | } | |
| 78 | |||
| 79 | 40 | bool RectMethodSTL::PostProcessingImpl() { | |
| 80 | 40 | GetOutput() = res_; | |
| 81 | 40 | return true; | |
| 82 | } | ||
| 83 | |||
| 84 | 100 | double RectMethodSTL::CalculateChunkSum(size_t start_idx, size_t end_idx, const std::vector<double> &h) { | |
| 85 |
1/2✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
|
100 | if (start_idx >= end_idx) { |
| 86 | return 0.0; | ||
| 87 | } | ||
| 88 | |||
| 89 | 100 | size_t count = end_idx - start_idx; | |
| 90 | size_t dims = local_input_.limits.size(); // число размерностей пространства | ||
| 91 | 100 | std::vector<int> current_indices(dims, 0); | |
| 92 |
1/4✓ Branch 1 taken 100 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
100 | std::vector<double> coords(dims); // создание вектора координат размером dims |
| 93 | |||
| 94 | size_t temp_idx = start_idx; | ||
| 95 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 100 times.
|
400 | for (int dim = static_cast<int>(dims) - 1; dim >= 0; --dim) { |
| 96 | 300 | current_indices[dim] = static_cast<int>(temp_idx % local_input_.n_steps[dim]); | |
| 97 | 300 | temp_idx /= local_input_.n_steps[dim]; | |
| 98 | } | ||
| 99 | |||
| 100 | double chunk_sum = 0.0; | ||
| 101 | |||
| 102 |
2/2✓ Branch 0 taken 91744 times.
✓ Branch 1 taken 100 times.
|
91844 | for (size_t i = 0; i < count; ++i) { |
| 103 |
2/2✓ Branch 0 taken 262520 times.
✓ Branch 1 taken 91744 times.
|
354264 | for (size_t dm = 0; dm < dims; ++dm) { |
| 104 | 262520 | coords[dm] = local_input_.limits[dm].first + ((current_indices[dm] + 0.5) * h[dm]); // реальная координата | |
| 105 | } | ||
| 106 | |||
| 107 | 91744 | chunk_sum += local_input_.func(coords); // вычисление функции в точке | |
| 108 | |||
| 109 |
2/2✓ Branch 0 taken 97704 times.
✓ Branch 1 taken 40 times.
|
97744 | for (int dm = static_cast<int>(dims) - 1; dm >= 0; --dm) { |
| 110 |
2/2✓ Branch 0 taken 6000 times.
✓ Branch 1 taken 91704 times.
|
97704 | current_indices[dm]++; |
| 111 |
2/2✓ Branch 0 taken 6000 times.
✓ Branch 1 taken 91704 times.
|
97704 | if (current_indices[dm] < local_input_.n_steps[dm]) { |
| 112 | break; | ||
| 113 | } | ||
| 114 | 6000 | current_indices[dm] = 0; | |
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | return chunk_sum; | ||
| 119 | } | ||
| 120 | |||
| 121 | } // namespace kutergin_v_multidimensional_integration_rect_method | ||
| 122 |