| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "../include/rect_method_omp.hpp" | ||
| 2 | |||
| 3 | #include <omp.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "../../common/include/common.hpp" | ||
| 11 | #include "util/include/util.hpp" | ||
| 12 | |||
| 13 | namespace kutergin_v_multidimensional_integration_rect_method { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | RectMethodOMP::RectMethodOMP(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | GetInput() = in; |
| 18 | 20 | GetOutput() = 0.0; | |
| 19 | 20 | } | |
| 20 | |||
| 21 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | bool RectMethodOMP::ValidationImpl() { |
| 22 | const auto &input = GetInput(); | ||
| 23 |
2/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
|
20 | if (input.limits.size() != input.n_steps.size() || input.limits.empty()) { |
| 24 | return false; | ||
| 25 | } | ||
| 26 | return std::ranges::all_of(input.n_steps, [](int n) { return n > 0; }); | ||
| 27 | } | ||
| 28 | |||
| 29 | 20 | bool RectMethodOMP::PreProcessingImpl() { | |
| 30 | 20 | local_input_ = GetInput(); | |
| 31 | 20 | res_ = 0.0; | |
| 32 | 20 | return true; | |
| 33 | } | ||
| 34 | |||
| 35 | 20 | bool RectMethodOMP::RunImpl() { | |
| 36 | size_t dims = local_input_.limits.size(); // число размерностей пространства | ||
| 37 | |||
| 38 | // вычисление числа шагов и общего числа итераций | ||
| 39 | size_t total_iterations = 1; | ||
| 40 | 20 | std::vector<double> h(dims); | |
| 41 | double d_v = 1.0; | ||
| 42 | |||
| 43 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
|
80 | for (size_t i = 0; i < dims; i++) { |
| 44 | 60 | total_iterations *= local_input_.n_steps[i]; | |
| 45 | 60 | h[i] = (local_input_.limits[i].second - local_input_.limits[i].first) / local_input_.n_steps[i]; | |
| 46 | 60 | d_v *= h[i]; | |
| 47 | } | ||
| 48 | |||
| 49 | double total_sum = 0.0; | ||
| 50 | |||
| 51 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | omp_set_num_threads(ppc::util::GetNumThreads()); // установка числа потоков |
| 52 | |||
| 53 | // параллельный регион | ||
| 54 | 20 | #pragma omp parallel default(none) shared(total_iterations, h) \ | |
| 55 | reduction(+ : total_sum) // reduction(+:total_sum) автоматически соберет сумму со всех потоков | ||
| 56 | { | ||
| 57 | size_t tid = omp_get_thread_num(); // индекс потока | ||
| 58 | int threads = omp_get_num_threads(); // число потоков | ||
| 59 | |||
| 60 | size_t chunk = total_iterations / threads; | ||
| 61 | size_t remainder = total_iterations % threads; | ||
| 62 | |||
| 63 | // вычисление стартового индекса и количество работы для текущего потока | ||
| 64 | size_t my_start = (tid * chunk) + std::min(tid, remainder); | ||
| 65 | size_t my_count = chunk + (tid < remainder ? 1 : 0); | ||
| 66 | |||
| 67 | total_sum += CalculateChunkSum(my_start, my_count, h); | ||
| 68 | } | ||
| 69 | |||
| 70 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | res_ = total_sum * d_v; |
| 71 | 20 | return true; | |
| 72 | } | ||
| 73 | |||
| 74 | 20 | bool RectMethodOMP::PostProcessingImpl() { | |
| 75 | 20 | GetOutput() = res_; | |
| 76 | 20 | return true; | |
| 77 | } | ||
| 78 | |||
| 79 | 50 | double RectMethodOMP::CalculateChunkSum(size_t start_idx, size_t count, const std::vector<double> &h) { | |
| 80 |
1/2✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
|
50 | if (count == 0) { |
| 81 | return 0.0; | ||
| 82 | } | ||
| 83 | |||
| 84 | size_t dims = local_input_.limits.size(); // число размерностей пространства | ||
| 85 | 50 | std::vector<int> current_indices(dims, 0); | |
| 86 |
1/4✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
50 | std::vector<double> coords(dims); // создание вектора координат размером dims |
| 87 | |||
| 88 | size_t temp_idx = start_idx; // временный линейный индекс | ||
| 89 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 50 times.
|
200 | for (int dim = static_cast<int>(dims) - 1; dim >= 0; --dim) { |
| 90 | 150 | current_indices[dim] = static_cast<int>(temp_idx % local_input_.n_steps[dim]); // текущий индекс в измерении d | |
| 91 | 150 | temp_idx /= local_input_.n_steps[dim]; // переход к следующему измерению | |
| 92 | } | ||
| 93 | |||
| 94 | double chunk_sum = 0.0; | ||
| 95 | |||
| 96 |
2/2✓ Branch 0 taken 45872 times.
✓ Branch 1 taken 50 times.
|
45922 | for (size_t i = 0; i < count; ++i) { |
| 97 |
2/2✓ Branch 0 taken 131260 times.
✓ Branch 1 taken 45872 times.
|
177132 | for (size_t dm = 0; dm < dims; ++dm) { |
| 98 | 131260 | coords[dm] = local_input_.limits[dm].first + ((current_indices[dm] + 0.5) * h[dm]); // реальная координата | |
| 99 | } | ||
| 100 | |||
| 101 | 45872 | chunk_sum += local_input_.func(coords); // вычисление функции в точке | |
| 102 | |||
| 103 |
2/2✓ Branch 0 taken 48852 times.
✓ Branch 1 taken 20 times.
|
48872 | for (int dm = static_cast<int>(dims) - 1; dm >= 0; --dm) { |
| 104 |
2/2✓ Branch 0 taken 3000 times.
✓ Branch 1 taken 45852 times.
|
48852 | current_indices[dm]++; |
| 105 |
2/2✓ Branch 0 taken 3000 times.
✓ Branch 1 taken 45852 times.
|
48852 | if (current_indices[dm] < local_input_.n_steps[dm]) { |
| 106 | break; | ||
| 107 | } | ||
| 108 | 3000 | current_indices[dm] = 0; | |
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | return chunk_sum; | ||
| 113 | } | ||
| 114 | |||
| 115 | } // namespace kutergin_v_multidimensional_integration_rect_method | ||
| 116 |