| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "dergynov_s_integrals_multistep_rectangle/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <atomic> | ||
| 5 | #include <cmath> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <functional> | ||
| 9 | #include <thread> | ||
| 10 | #include <utility> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "dergynov_s_integrals_multistep_rectangle/common/include/common.hpp" | ||
| 14 | |||
| 15 | namespace dergynov_s_integrals_multistep_rectangle { | ||
| 16 | namespace { | ||
| 17 | |||
| 18 | 20960 | bool ComputeSlice(const std::function<double(const std::vector<double> &)> &function, | |
| 19 | const std::vector<std::pair<double, double>> &borders, const std::vector<double> &step_sizes, | ||
| 20 | size_t inner_dimensions, int64_t inner_cells, int steps_per_dimension, int outer_index, | ||
| 21 | std::vector<int> &indices, std::vector<double> &point, double &result) { | ||
| 22 | 20960 | indices[inner_dimensions] = outer_index; | |
| 23 | 20960 | point[inner_dimensions] = | |
| 24 | 20960 | borders[inner_dimensions].first + ((static_cast<double>(outer_index) + 0.5) * step_sizes[inner_dimensions]); | |
| 25 | |||
| 26 |
2/2✓ Branch 0 taken 10069600 times.
✓ Branch 1 taken 20960 times.
|
10090560 | for (int64_t cell = 0; cell < inner_cells; ++cell) { |
| 27 |
2/2✓ Branch 0 taken 19081600 times.
✓ Branch 1 taken 10069600 times.
|
29151200 | for (size_t dim_index = 0; dim_index < inner_dimensions; ++dim_index) { |
| 28 | 19081600 | point[dim_index] = | |
| 29 | 19081600 | borders[dim_index].first + ((static_cast<double>(indices[dim_index]) + 0.5) * step_sizes[dim_index]); | |
| 30 | } | ||
| 31 | |||
| 32 | double func_value = function(point); | ||
| 33 |
1/2✓ Branch 0 taken 10069600 times.
✗ Branch 1 not taken.
|
10069600 | if (!std::isfinite(func_value)) { |
| 34 | return false; | ||
| 35 | } | ||
| 36 | 10069600 | result += func_value; | |
| 37 | |||
| 38 |
2/2✓ Branch 0 taken 10163200 times.
✓ Branch 1 taken 20960 times.
|
10184160 | for (size_t position = 0; position < inner_dimensions; ++position) { |
| 39 |
2/2✓ Branch 0 taken 114560 times.
✓ Branch 1 taken 10048640 times.
|
10163200 | if (++indices[position] < steps_per_dimension) { |
| 40 | break; | ||
| 41 | } | ||
| 42 | 114560 | indices[position] = 0; | |
| 43 | } | ||
| 44 | } | ||
| 45 | return true; | ||
| 46 | } | ||
| 47 | |||
| 48 | 608 | void ThreadWorker(const std::function<double(const std::vector<double> &)> &function, | |
| 49 | const std::vector<std::pair<double, double>> &borders, const std::vector<double> &step_sizes, | ||
| 50 | size_t total_dimensions, size_t inner_dimensions, int64_t inner_cells, int steps_per_dimension, | ||
| 51 | int start_outer, int end_outer, double &partial_result, std::atomic<bool> &error_flag) { | ||
| 52 | 608 | std::vector<int> indices(total_dimensions, 0); | |
| 53 |
1/4✓ Branch 1 taken 608 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
608 | std::vector<double> point(total_dimensions); |
| 54 | double local_sum = 0.0; | ||
| 55 | |||
| 56 |
3/4✓ Branch 0 taken 20960 times.
✓ Branch 1 taken 608 times.
✓ Branch 2 taken 20960 times.
✗ Branch 3 not taken.
|
21568 | for (int outer_idx = start_outer; outer_idx < end_outer && !error_flag.load(); ++outer_idx) { |
| 57 | 20960 | double slice_sum = 0.0; | |
| 58 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 20960 times.
✓ Branch 3 taken 20960 times.
✗ Branch 4 not taken.
|
20960 | if (!ComputeSlice(function, borders, step_sizes, inner_dimensions, inner_cells, steps_per_dimension, outer_idx, |
| 59 | indices, point, slice_sum)) { | ||
| 60 | error_flag.store(true); | ||
| 61 | ✗ | return; | |
| 62 | } | ||
| 63 | 20960 | local_sum += slice_sum; | |
| 64 | } | ||
| 65 | |||
| 66 |
1/2✓ Branch 0 taken 608 times.
✗ Branch 1 not taken.
|
608 | partial_result = local_sum; |
| 67 | } | ||
| 68 | |||
| 69 | } // namespace | ||
| 70 | |||
| 71 |
1/2✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
|
152 | DergynovSIntegralsMultistepRectangleSTL::DergynovSIntegralsMultistepRectangleSTL(const InType &in) { |
| 72 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 73 | GetInput() = in; | ||
| 74 | 152 | GetOutput() = 0.0; | |
| 75 | 152 | } | |
| 76 | |||
| 77 |
1/2✓ Branch 0 taken 152 times.
✗ Branch 1 not taken.
|
152 | bool DergynovSIntegralsMultistepRectangleSTL::ValidationImpl() { |
| 78 | const auto &[function, borders, steps_count] = GetInput(); | ||
| 79 | |||
| 80 |
1/2✓ Branch 0 taken 152 times.
✗ Branch 1 not taken.
|
152 | if (!function) { |
| 81 | return false; | ||
| 82 | } | ||
| 83 |
1/2✓ Branch 0 taken 152 times.
✗ Branch 1 not taken.
|
152 | if (steps_count <= 0) { |
| 84 | return false; | ||
| 85 | } | ||
| 86 |
1/2✓ Branch 0 taken 152 times.
✗ Branch 1 not taken.
|
152 | if (borders.empty()) { |
| 87 | return false; | ||
| 88 | } | ||
| 89 | |||
| 90 | return std::ranges::all_of(borders, [](const auto &border) { | ||
| 91 |
3/6✓ Branch 0 taken 264 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 264 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 264 times.
✗ Branch 5 not taken.
|
264 | return std::isfinite(border.first) && std::isfinite(border.second) && border.first < border.second; |
| 92 | }); | ||
| 93 | } | ||
| 94 | |||
| 95 | 152 | bool DergynovSIntegralsMultistepRectangleSTL::PreProcessingImpl() { | |
| 96 | 152 | GetOutput() = 0.0; | |
| 97 | 152 | return true; | |
| 98 | } | ||
| 99 | |||
| 100 | 152 | bool DergynovSIntegralsMultistepRectangleSTL::RunImpl() { | |
| 101 | const auto &[function, borders, steps_count] = GetInput(); | ||
| 102 | 152 | const size_t total_dimensions = borders.size(); | |
| 103 | |||
| 104 | 152 | std::vector<double> step_sizes(total_dimensions); | |
| 105 | double cell_volume = 1.0; | ||
| 106 | |||
| 107 |
2/2✓ Branch 0 taken 264 times.
✓ Branch 1 taken 152 times.
|
416 | for (size_t dim_index = 0; dim_index < total_dimensions; ++dim_index) { |
| 108 |
1/2✓ Branch 0 taken 264 times.
✗ Branch 1 not taken.
|
264 | step_sizes[dim_index] = (borders[dim_index].second - borders[dim_index].first) / static_cast<double>(steps_count); |
| 109 |
2/4✓ Branch 0 taken 264 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 264 times.
✗ Branch 3 not taken.
|
264 | if (!(step_sizes[dim_index] > 0.0) || !std::isfinite(step_sizes[dim_index])) { |
| 110 | return false; | ||
| 111 | } | ||
| 112 | 264 | cell_volume *= step_sizes[dim_index]; | |
| 113 | } | ||
| 114 | |||
| 115 | 152 | const size_t inner_dimensions = total_dimensions - 1; | |
| 116 | 152 | int64_t inner_cells = 1; | |
| 117 |
2/2✓ Branch 0 taken 112 times.
✓ Branch 1 taken 152 times.
|
264 | for (size_t dim_index = 0; dim_index < inner_dimensions; ++dim_index) { |
| 118 | 112 | inner_cells *= steps_count; | |
| 119 | } | ||
| 120 | |||
| 121 | 152 | int hardware_threads = static_cast<int>(std::thread::hardware_concurrency()); | |
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 152 times.
|
152 | if (hardware_threads <= 0) { |
| 123 | ✗ | hardware_threads = 4; | |
| 124 | } | ||
| 125 | const int actual_threads = std::min(hardware_threads, steps_count); | ||
| 126 | |||
| 127 |
1/4✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
152 | std::vector<double> partial_results(actual_threads, 0.0); |
| 128 | 152 | std::atomic<bool> error_flag{false}; | |
| 129 | 152 | std::vector<std::thread> worker_threads; | |
| 130 |
1/2✓ Branch 1 taken 152 times.
✗ Branch 2 not taken.
|
152 | worker_threads.reserve(actual_threads); |
| 131 | |||
| 132 |
2/2✓ Branch 0 taken 608 times.
✓ Branch 1 taken 152 times.
|
760 | for (int thread_id = 0; thread_id < actual_threads; ++thread_id) { |
| 133 | 608 | int chunk_size = steps_count / actual_threads; | |
| 134 | 608 | int remainder = steps_count % actual_threads; | |
| 135 |
1/2✓ Branch 0 taken 608 times.
✗ Branch 1 not taken.
|
608 | int start_index = (thread_id * chunk_size) + std::min(thread_id, remainder); |
| 136 |
1/2✓ Branch 0 taken 608 times.
✗ Branch 1 not taken.
|
608 | int end_index = start_index + chunk_size + (thread_id < remainder ? 1 : 0); |
| 137 | |||
| 138 |
1/2✓ Branch 1 taken 608 times.
✗ Branch 2 not taken.
|
608 | worker_threads.emplace_back([&, start_index, end_index, thread_id]() { |
| 139 | 608 | ThreadWorker(function, borders, step_sizes, total_dimensions, inner_dimensions, inner_cells, steps_count, | |
| 140 | 608 | start_index, end_index, partial_results[thread_id], error_flag); | |
| 141 | 608 | }); | |
| 142 | } | ||
| 143 | |||
| 144 |
2/2✓ Branch 0 taken 608 times.
✓ Branch 1 taken 152 times.
|
760 | for (auto &worker : worker_threads) { |
| 145 |
1/2✓ Branch 1 taken 608 times.
✗ Branch 2 not taken.
|
608 | worker.join(); |
| 146 | } | ||
| 147 | |||
| 148 |
1/2✓ Branch 0 taken 152 times.
✗ Branch 1 not taken.
|
152 | if (error_flag.load()) { |
| 149 | return false; | ||
| 150 | } | ||
| 151 | |||
| 152 | double total_sum = 0.0; | ||
| 153 |
2/2✓ Branch 0 taken 608 times.
✓ Branch 1 taken 152 times.
|
760 | for (double partial : partial_results) { |
| 154 | 608 | total_sum += partial; | |
| 155 | } | ||
| 156 | |||
| 157 | 152 | GetOutput() = total_sum * cell_volume; | |
| 158 | 152 | return std::isfinite(GetOutput()); | |
| 159 | 152 | } | |
| 160 | |||
| 161 | 152 | bool DergynovSIntegralsMultistepRectangleSTL::PostProcessingImpl() { | |
| 162 | 152 | return std::isfinite(GetOutput()); | |
| 163 | } | ||
| 164 | |||
| 165 | } // namespace dergynov_s_integrals_multistep_rectangle | ||
| 166 |