GCC Code Coverage Report


Directory: ./
File: tasks/ashihmin_d_calculate_integrals_by_simpson/mpi/src/ops_mpi.cpp
Date: 2026-01-10 02:40:41
Exec Total Coverage
Lines: 65 69 94.2%
Functions: 7 8 87.5%
Branches: 43 58 74.1%

Line Branch Exec Source
1 #include "ashihmin_d_calculate_integrals_by_simpson/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm>
6 #include <cmath>
7 #include <cstddef>
8 #include <vector>
9
10 #include "ashihmin_d_calculate_integrals_by_simpson/common/include/common.hpp"
11
12 namespace ashihmin_d_calculate_integrals_by_simpson {
13
14 namespace {
15
16 double Function(const std::vector<double> &coordinates) {
17 double total = 0.0;
18
2/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 726 times.
✓ Branch 3 taken 363 times.
1089 for (double value : coordinates) {
19 726 total += value * value;
20 }
21 return total;
22 }
23
24 double CalculateWeightCoefficient(const std::vector<int> &indices, int partitions) {
25 double weight_coefficient = 1.0;
26
2/2
✓ Branch 0 taken 726 times.
✓ Branch 1 taken 363 times.
1089 for (int node_index : indices) {
27
2/2
✓ Branch 0 taken 594 times.
✓ Branch 1 taken 132 times.
726 if (node_index == 0 || node_index == partitions) {
28 weight_coefficient *= 1.0;
29
2/2
✓ Branch 0 taken 264 times.
✓ Branch 1 taken 330 times.
594 } else if (node_index % 2 == 0) {
30 264 weight_coefficient *= 2.0;
31 } else {
32 330 weight_coefficient *= 4.0;
33 }
34 }
35 return weight_coefficient;
36 }
37
38 363 std::vector<double> CalculatePoint(const std::vector<int> &indices, const std::vector<double> &step_sizes,
39 const std::vector<double> &left_bounds) {
40 363 std::vector<double> point(indices.size());
41
2/2
✓ Branch 0 taken 726 times.
✓ Branch 1 taken 363 times.
1089 for (std::size_t dim = 0; dim < indices.size(); ++dim) {
42 726 point[dim] = left_bounds[dim] + (indices[dim] * step_sizes[dim]);
43 }
44 363 return point;
45 }
46
47 6 double ComputeLocalSum(int start_index, int end_index, int dimensions, int partitions,
48 const std::vector<double> &step_sizes, const std::vector<double> &left_bounds) {
49 double local_sum = 0.0;
50
51 6 std::vector<int> indices(static_cast<std::size_t>(dimensions), 0);
52
53
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 6 times.
39 for (int first_index = start_index; first_index <= end_index; ++first_index) {
54 33 indices[0] = first_index;
55
56 int inner_points_count = 1;
57
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 33 times.
66 for (int dim = 1; dim < dimensions; ++dim) {
58 33 inner_points_count *= (partitions + 1);
59 }
60
61
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 33 times.
396 for (int inner_index = 0; inner_index < inner_points_count; ++inner_index) {
62 int temp_index = inner_index;
63
2/2
✓ Branch 0 taken 363 times.
✓ Branch 1 taken 363 times.
726 for (int dim = 1; dim < dimensions; ++dim) {
64 363 indices[dim] = temp_index % (partitions + 1);
65 363 temp_index /= (partitions + 1);
66 }
67
68 const double weight = CalculateWeightCoefficient(indices, partitions);
69
1/2
✓ Branch 1 taken 363 times.
✗ Branch 2 not taken.
363 const auto point = CalculatePoint(indices, step_sizes, left_bounds);
70
1/2
✓ Branch 0 taken 363 times.
✗ Branch 1 not taken.
363 local_sum += weight * Function(point);
71 }
72 }
73
74 6 return local_sum;
75 }
76
77 } // namespace
78
79
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 AshihminDCalculateIntegralsBySimpsonMPI::AshihminDCalculateIntegralsBySimpsonMPI(const InType &input) {
80 SetTypeOfTask(GetStaticTypeOfTask());
81 GetInput() = input;
82 6 GetOutput() = 0.0;
83 6 }
84
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 bool AshihminDCalculateIntegralsBySimpsonMPI::ValidationImpl() {
86 const auto &input = GetInput();
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (input.left_bounds.size() != input.right_bounds.size()) {
88 return false;
89 }
90
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (input.partitions <= 0 || input.partitions % 2 != 0) {
91 return false;
92 }
93
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (std::size_t index = 0; index < input.left_bounds.size(); ++index) {
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (input.right_bounds[index] <= input.left_bounds[index]) {
95 return false;
96 }
97 }
98 return true;
99 }
100
101 6 bool AshihminDCalculateIntegralsBySimpsonMPI::PreProcessingImpl() {
102 6 GetOutput() = 0.0;
103 6 return true;
104 }
105
106 double AshihminDCalculateIntegralsBySimpsonMPI::IntegrandFunction(const std::vector<double> &coordinates) {
107 return Function(coordinates);
108 }
109
110 6 bool AshihminDCalculateIntegralsBySimpsonMPI::RunImpl() {
111 6 int process_rank = 0;
112 6 int process_count = 1;
113 6 MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
114 6 MPI_Comm_size(MPI_COMM_WORLD, &process_count);
115
116 const auto &input = GetInput();
117 6 const int dimensions = static_cast<int>(input.left_bounds.size());
118 6 const int partitions = input.partitions;
119
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (dimensions == 0) {
121 GetOutput() = 0.0;
122 return true;
123 }
124
125 6 std::vector<double> step_sizes(static_cast<std::size_t>(dimensions));
126
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (int dim = 0; dim < dimensions; ++dim) {
127 12 step_sizes[static_cast<std::size_t>(dim)] = (input.right_bounds[dim] - input.left_bounds[dim]) / partitions;
128 }
129
130 6 const int total_nodes = partitions + 1;
131 6 const int chunk_size = total_nodes / process_count;
132 6 const int remainder = total_nodes % process_count;
133
134
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 int start_index = (process_rank * chunk_size) + std::min(process_rank, remainder);
135 6 int end_index = start_index + chunk_size - 1;
136
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (process_rank < remainder) {
137 end_index += 1;
138 }
139
140 6 double local_sum = 0.0;
141
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (start_index <= end_index) {
142
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 local_sum = ComputeLocalSum(start_index, end_index, dimensions, partitions, step_sizes, input.left_bounds);
143 }
144
145 6 double global_sum = 0.0;
146
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
147
148 double volume_element = 1.0;
149
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (double step : step_sizes) {
150 12 volume_element *= step;
151 }
152
153
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (process_rank == 0) {
154 3 GetOutput() = global_sum * volume_element / std::pow(3.0, dimensions);
155 }
156
157
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MPI_Bcast(&GetOutput(), 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
158 return true;
159 }
160
161 6 bool AshihminDCalculateIntegralsBySimpsonMPI::PostProcessingImpl() {
162 6 return true;
163 }
164
165 } // namespace ashihmin_d_calculate_integrals_by_simpson
166