GCC Code Coverage Report


Directory: ./
File: tasks/karpich_i_integrals_multistep_rectangle/mpi/src/ops_mpi.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 75 75 100.0%
Functions: 6 6 100.0%
Branches: 35 50 70.0%

Line Branch Exec Source
1 #include "karpich_i_integrals_multistep_rectangle/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <cmath>
6
7 #include "karpich_i_integrals_multistep_rectangle/common/include/common.hpp"
8 #include "task/include/task.hpp"
9
10 namespace karpich_i_integrals_multistep_rectangle {
11
12 namespace {
13
14 double IntegrandFunction(double x, double y, double z) {
15 1587600 return (x * y * z) + (x * x) + (y * y) + (z * z);
16 }
17
18 120 double ComputeRectangleIntegralPartial(int n, int step, int start_i, int end_i) {
19 double result = 0.0;
20 120 int divisions = n * step;
21 120 double h = 1.0 / divisions;
22
23
2/2
✓ Branch 0 taken 1260 times.
✓ Branch 1 taken 120 times.
1380 for (int i = start_i; i < end_i; i++) {
24
2/2
✓ Branch 0 taken 40180 times.
✓ Branch 1 taken 1260 times.
41440 for (int j = 0; j < divisions; j++) {
25
2/2
✓ Branch 0 taken 1587600 times.
✓ Branch 1 taken 40180 times.
1627780 for (int k = 0; k < divisions; k++) {
26 1587600 double x = (i + 0.5) * h;
27 1587600 double y = (j + 0.5) * h;
28 1587600 double z = (k + 0.5) * h;
29 1587600 result += IntegrandFunction(x, y, z) * h * h * h;
30 }
31 }
32 }
33
34 120 return result;
35 }
36
37 } // namespace
38
39 40 KarpichIIntegralsMultistepRectangleMPI::KarpichIIntegralsMultistepRectangleMPI(const InType &in) {
40 SetTypeOfTask(GetStaticTypeOfTask());
41 40 GetInput() = in;
42 GetOutput() = 0;
43 40 }
44
45 40 bool KarpichIIntegralsMultistepRectangleMPI::ValidationImpl() {
46
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (GetInput() <= 0) {
47 return false;
48 }
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (GetOutput() != 0) {
50 return false;
51 }
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (GetInput() > 1000) {
53 return false;
54 }
55 if (GetStaticTypeOfTask() != ppc::task::TypeOfTask::kMPI) {
56 return false;
57 }
58 40 int rank = 0;
59 40 int size = 0;
60 40 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
61 40 MPI_Comm_size(MPI_COMM_WORLD, &size);
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (size <= 0) {
63 return false;
64 }
65
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
40 if (rank < 0 || rank >= size) {
66 return false;
67 }
68 return true;
69 }
70
71 40 bool KarpichIIntegralsMultistepRectangleMPI::PreProcessingImpl() {
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (GetInput() <= 0) {
73 return false;
74 }
75 40 int rank = 0;
76 40 int size = 0;
77 40 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
78 40 MPI_Comm_size(MPI_COMM_WORLD, &size);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (size <= 0) {
80 return false;
81 }
82 40 GetOutput() = 0;
83 if (GetOutput() != 0) {
84 return false;
85 }
86 40 int divisions = GetInput() * 3;
87 40 return divisions > 0;
88 }
89
90 40 bool KarpichIIntegralsMultistepRectangleMPI::RunImpl() {
91
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (GetInput() == 0) {
92 return false;
93 }
94
95 40 int rank = 0;
96 40 int size = 0;
97 40 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
98 40 MPI_Comm_size(MPI_COMM_WORLD, &size);
99
100 double total_result = 0.0;
101
102
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 40 times.
160 for (int step = 1; step <= 3; step++) {
103 120 int divisions = GetInput() * step;
104 120 int rows_per_process = divisions / size;
105 120 int remainder = divisions % size;
106
107 120 int start_i = (rank * rows_per_process) + (rank < remainder ? rank : remainder);
108
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 20 times.
120 int end_i = start_i + rows_per_process + (rank < remainder ? 1 : 0);
109
110 120 double local_result = ComputeRectangleIntegralPartial(GetInput(), step, start_i, end_i);
111
112
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 60 times.
120 if (rank == 0) {
113 60 total_result += local_result;
114
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 60 times.
120 for (int proc = 1; proc < size; proc++) {
115 60 double recv_result = 0.0;
116 60 MPI_Recv(&recv_result, 1, MPI_DOUBLE, proc, step, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
117 60 total_result += recv_result;
118 }
119 } else {
120 60 MPI_Send(&local_result, 1, MPI_DOUBLE, 0, step, MPI_COMM_WORLD);
121 }
122 }
123
124 40 OutType final_output = 0;
125
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
40 if (rank == 0) {
126 20 total_result = total_result / 3.0;
127 20 final_output = static_cast<OutType>(std::round(total_result));
128 }
129
130 40 MPI_Bcast(&final_output, 1, MPI_INT, 0, MPI_COMM_WORLD);
131 40 GetOutput() = final_output;
132
133 40 return GetOutput() > 0;
134 }
135
136 40 bool KarpichIIntegralsMultistepRectangleMPI::PostProcessingImpl() {
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (GetOutput() <= 0) {
138 return false;
139 }
140 40 int rank = 0;
141 40 int size = 0;
142 40 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
143 40 MPI_Comm_size(MPI_COMM_WORLD, &size);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (size <= 0) {
145 return false;
146 }
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (GetInput() <= 0) {
148 return false;
149 }
150 auto min_expected = static_cast<OutType>(1);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (GetOutput() < min_expected) {
152 return false;
153 }
154 40 auto max_expected = static_cast<OutType>(GetInput() * GetInput() * GetInput() * 10);
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (GetOutput() > max_expected) {
156 return false;
157 }
158
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
40 if (rank == 0) {
159
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
20 if (GetOutput() == GetInput() && GetInput() > 1) {
160 return false;
161 }
162 }
163 return true;
164 }
165
166 } // namespace karpich_i_integrals_multistep_rectangle
167