GCC Code Coverage Report


Directory: ./
File: tasks/kutergin_v_multidimensional_integration_rect_method/all/src/rect_method_all.cpp
Date: 2026-05-11 08:26:31
Exec Total Coverage
Lines: 77 79 97.5%
Functions: 7 7 100.0%
Branches: 40 58 69.0%

Line Branch Exec Source
1 #include "../include/rect_method_all.hpp"
2
3 #include <mpi.h>
4 #include <omp.h>
5
6 #include <algorithm>
7 #include <cmath>
8 #include <cstddef>
9 #include <functional>
10 #include <utility>
11 #include <vector>
12
13 #include "../../common/include/common.hpp"
14 #include "util/include/util.hpp"
15
16 namespace kutergin_v_multidimensional_integration_rect_method {
17
18
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 RectMethodALL::RectMethodALL(const InType &in) {
19 SetTypeOfTask(GetStaticTypeOfTask());
20
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 GetInput() = in;
21 10 GetOutput() = 0.0;
22 10 }
23
24
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 bool RectMethodALL::ValidationImpl() {
25 const auto &input = GetInput();
26
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 if (input.limits.size() != input.n_steps.size() || input.limits.empty()) {
27 return false;
28 }
29 return std::ranges::all_of(input.n_steps, [](int n) { return n > 0; });
30 }
31
32 10 bool RectMethodALL::PreProcessingImpl() {
33 10 local_input_ = GetInput();
34 10 res_ = 0.0;
35 10 return true;
36 }
37
38 10 bool RectMethodALL::RunImpl() {
39 10 int rank = 0;
40 10 int size = 1;
41 10 bool is_mpi = ppc::util::IsUnderMpirun();
42
43
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (is_mpi) {
44 10 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
45 10 MPI_Comm_size(MPI_COMM_WORLD, &size);
46 }
47
48
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 size_t dims = (rank == 0) ? local_input_.limits.size() : 0;
49
50 10 DistributeData(rank, dims);
51
52 size_t total_iterations = 1;
53 10 std::vector<double> h(dims);
54 double d_v = 1.0;
55
56
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
40 for (size_t i = 0; i < dims; ++i) {
57 30 total_iterations *= local_input_.n_steps[i];
58 30 h[i] = (local_input_.limits[i].second - local_input_.limits[i].first) / local_input_.n_steps[i];
59 30 d_v *= h[i];
60 }
61
62 10 size_t proc_chunk = total_iterations / size;
63 10 size_t proc_remainder = total_iterations % size;
64
65
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 size_t my_proc_count = proc_chunk + (std::cmp_less(rank, proc_remainder) ? 1 : 0);
66
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 size_t my_proc_start = (rank * proc_chunk) + std::min(static_cast<size_t>(rank), proc_remainder);
67
68 10 double local_sum = 0.0;
69
70
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (my_proc_count > 0) {
71
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 int num_threads = ppc::util::GetNumThreads();
72 10 omp_set_num_threads(num_threads);
73
74 10 const auto &func = local_input_.func;
75 10 const auto &n_steps = local_input_.n_steps;
76 10 const auto &limits = local_input_.limits;
77
78 10 #pragma omp parallel default(none) shared(h, dims, my_proc_start, my_proc_count, func, n_steps, limits) \
79 reduction(+ : local_sum)
80 {
81 int tid = omp_get_thread_num();
82 int t_count = omp_get_num_threads();
83
84 size_t thread_chunk = my_proc_count / t_count;
85 size_t thread_remainder = my_proc_count % t_count;
86
87 size_t my_thread_count = thread_chunk + (std::cmp_less(tid, thread_remainder) ? 1 : 0);
88 size_t my_thread_start =
89 my_proc_start + (tid * thread_chunk) + std::min(static_cast<size_t>(tid), thread_remainder);
90
91 local_sum += CalculateChunkSum(my_thread_start, my_thread_start + my_thread_count, h, limits, n_steps, func);
92 }
93 }
94
95
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (is_mpi) {
96 10 double global_sum = 0.0;
97
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
98
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 if (rank == 0) {
99 5 res_ = global_sum * d_v;
100 }
101 } else {
102 res_ = local_sum * d_v;
103 }
104
105 10 return true;
106 }
107
108 10 bool RectMethodALL::PostProcessingImpl() {
109 10 int rank = 0;
110
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 if (ppc::util::IsUnderMpirun()) {
111 10 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
112 }
113
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 if (rank == 0) {
114 5 GetOutput() = res_;
115 }
116 10 return true;
117 }
118
119 10 void RectMethodALL::DistributeData(int rank, size_t &dims) {
120
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (!ppc::util::IsUnderMpirun()) {
121 return;
122 }
123
124 10 int dims_io = static_cast<int>(dims);
125 10 MPI_Bcast(&dims_io, 1, MPI_INT, 0, MPI_COMM_WORLD);
126 10 dims = static_cast<size_t>(dims_io);
127
128
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 if (rank != 0) {
129 5 local_input_.limits.resize(dims);
130 5 local_input_.n_steps.resize(dims);
131 }
132
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
40 for (size_t i = 0; i < dims; ++i) {
133 30 MPI_Bcast(&local_input_.limits[i].first, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
134 30 MPI_Bcast(&local_input_.limits[i].second, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
135 30 MPI_Bcast(&local_input_.n_steps[i], 1, MPI_INT, 0, MPI_COMM_WORLD);
136 }
137 }
138
139 20 double RectMethodALL::CalculateChunkSum(size_t start_idx, size_t end_idx, const std::vector<double> &h,
140 const std::vector<std::pair<double, double>> &limits,
141 const std::vector<int> &n_steps,
142 const std::function<double(const std::vector<double> &)> &func) {
143
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (start_idx >= end_idx) {
144 return 0.0;
145 }
146
147 20 size_t count = end_idx - start_idx;
148 size_t dims = limits.size(); // число размерностей пространства
149 20 std::vector<int> current_indices(dims, 0);
150
1/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
20 std::vector<double> coords(dims); // создание вектора координат размером dims
151
152 size_t temp_idx = start_idx;
153
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 20 times.
80 for (int dim = static_cast<int>(dims) - 1; dim >= 0; --dim) {
154 60 current_indices[dim] = static_cast<int>(temp_idx % n_steps[dim]);
155 60 temp_idx /= n_steps[dim];
156 }
157
158 double chunk_sum = 0.0;
159
160
2/2
✓ Branch 0 taken 11468 times.
✓ Branch 1 taken 20 times.
11488 for (size_t i = 0; i < count; ++i) {
161
2/2
✓ Branch 0 taken 32815 times.
✓ Branch 1 taken 11468 times.
44283 for (size_t dm = 0; dm < dims; ++dm) {
162 32815 coords[dm] = limits[dm].first + ((current_indices[dm] + 0.5) * h[dm]); // реальная координата
163 }
164
165 11468 chunk_sum += func(coords); // вычисление функции в точке
166
167
2/2
✓ Branch 0 taken 12213 times.
✓ Branch 1 taken 5 times.
12218 for (int dm = static_cast<int>(dims) - 1; dm >= 0; --dm) {
168
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 11463 times.
12213 current_indices[dm]++;
169
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 11463 times.
12213 if (current_indices[dm] < n_steps[dm]) {
170 break;
171 }
172 750 current_indices[dm] = 0;
173 }
174 }
175
176 return chunk_sum;
177 }
178
179 } // namespace kutergin_v_multidimensional_integration_rect_method
180