GCC Code Coverage Report


Directory: ./
File: tasks/safronov_m_sum_values_matrix/mpi/src/ops_mpi.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 93 93 100.0%
Functions: 10 10 100.0%
Branches: 69 90 76.7%

Line Branch Exec Source
1 #include "safronov_m_sum_values_matrix/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <cstddef>
6 #include <vector>
7
8 #include "safronov_m_sum_values_matrix/common/include/common.hpp"
9
10 namespace safronov_m_sum_values_matrix {
11
12
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 SafronovMSumValuesMatrixMPI::SafronovMSumValuesMatrixMPI(const InType &in) {
13 SetTypeOfTask(GetStaticTypeOfTask());
14
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 InType tmp(in);
15 GetInput().swap(tmp);
16 20 }
17
18
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 bool SafronovMSumValuesMatrixMPI::ValidationImpl() {
19
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 if (GetInput().empty()) {
20 return true;
21 }
22 size_t cols = GetInput()[0].size();
23 std::size_t total = 0;
24
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 18 times.
270 for (const auto &row : GetInput()) {
25 252 total += row.size();
26 }
27
3/6
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
18 return GetOutput().empty() && (cols != 0) && ((cols * GetInput().size()) == total);
28 }
29
30
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 bool SafronovMSumValuesMatrixMPI::PreProcessingImpl() {
31 GetOutput().clear();
32 20 return true;
33 }
34
35 18 std::vector<double> SafronovMSumValuesMatrixMPI::SummValues(const int start, const int end) {
36 18 std::vector<double> vec;
37
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 18 times.
149 for (int i = start; i <= end; i++) {
38 131 double summa = 0;
39
2/2
✓ Branch 0 taken 10103 times.
✓ Branch 1 taken 131 times.
10234 for (const auto &row : GetInput()) {
40 10103 summa += row[i];
41 }
42 vec.push_back(summa);
43 }
44 18 return vec;
45 }
46
47 18 std::vector<int> SafronovMSumValuesMatrixMPI::CalculatingInterval(int size_prcs, int rank, int count_column) {
48 18 std::vector<int> vec(2);
49 18 int whole_part = count_column / size_prcs;
50 18 int real_part = count_column % size_prcs;
51 18 int start = rank * whole_part;
52
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 13 times.
18 if ((rank - 1 < real_part) && (rank - 1 != -1)) {
53 5 start += rank;
54
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
13 } else if (rank != 0) {
55 4 start += real_part;
56 }
57 18 int end = start + whole_part - 1;
58
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 13 times.
18 if (rank < real_part) {
59 end += 1;
60 }
61 18 vec[0] = start;
62 18 vec[1] = end;
63 18 return vec;
64 }
65
66 18 std::vector<double> SafronovMSumValuesMatrixMPI::ConversionToVector(int rows, int cols, int rank) {
67 18 std::vector<double> vector(static_cast<std::size_t>(rows) * static_cast<std::size_t>(cols));
68
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (rank == 0) {
69
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 9 times.
135 for (int i = 0; i < rows; i++) {
70
2/2
✓ Branch 0 taken 10103 times.
✓ Branch 1 taken 126 times.
10229 for (int j = 0; j < cols; j++) {
71 10103 vector[(i * cols) + j] = GetInput()[i][j];
72 }
73 }
74 }
75 18 return vector;
76 }
77
78 18 void SafronovMSumValuesMatrixMPI::ConversionToMatrix(const std::vector<double> &vector, int rows, int cols, int rank) {
79
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (rank != 0) {
80
1/2
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 GetInput() = std::vector<std::vector<double>>(rows, std::vector<double>(cols));
81
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 9 times.
135 for (int i = 0; i < rows; i++) {
82
2/2
✓ Branch 0 taken 10103 times.
✓ Branch 1 taken 126 times.
10229 for (int j = 0; j < cols; j++) {
83 10103 GetInput()[i][j] = vector[(i * cols) + j];
84 }
85 }
86 }
87 18 }
88
89 20 bool SafronovMSumValuesMatrixMPI::SendingOutMatrix(int rank) {
90 20 int rows = 0;
91 20 int cols = 0;
92
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (rank == 0) {
93 10 rows = static_cast<int>(GetInput().size());
94
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 cols = (rows != 0) ? static_cast<int>(GetInput()[0].size()) : 0;
95 }
96
97 20 MPI_Bcast(&rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
98 20 MPI_Bcast(&cols, 1, MPI_INT, 0, MPI_COMM_WORLD);
99
100
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 if (rows == 0) {
101 return true;
102 }
103
104 18 std::vector<double> vector = ConversionToVector(rows, cols, rank);
105
106
1/2
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
18 MPI_Bcast(vector.data(), rows * cols, MPI_DOUBLE, 0, MPI_COMM_WORLD);
107
1/2
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
18 ConversionToMatrix(vector, rows, cols, rank);
108
109 return false;
110 }
111
112 20 bool SafronovMSumValuesMatrixMPI::RunImpl() {
113 20 int size = 0;
114 20 int rank = 0;
115 20 MPI_Comm_size(MPI_COMM_WORLD, &size);
116 20 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
117
118 20 bool flag = SendingOutMatrix(rank);
119
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 if (flag) {
120 return true;
121 }
122
123
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (rank == 0) {
124 9 int count_column = static_cast<int>(GetInput()[0].size());
125
126
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 for (int i = 1; i < size; i++) {
127 9 std::vector<int> interval = CalculatingInterval(size, i, count_column);
128
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 MPI_Send(interval.data(), 2, MPI_INT, i, 0, MPI_COMM_WORLD);
129 }
130
131 9 std::vector<int> interval = CalculatingInterval(size, 0, count_column);
132
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 std::vector<double> elems = SummValues(interval[0], interval[1]);
133
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 9 times.
77 for (double &elem : elems) {
134 GetOutput().push_back(elem);
135 }
136
137 MPI_Status status;
138
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 for (int i = 1; i < size; i++) {
139 9 int size_elems = 0;
140
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 MPI_Recv(&size_elems, 1, MPI_INT, i, 1, MPI_COMM_WORLD, &status);
141
1/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
9 std::vector<double> buf(size_elems);
142
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 MPI_Recv(buf.data(), size_elems, MPI_DOUBLE, i, 2, MPI_COMM_WORLD, &status);
143
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 9 times.
72 for (double &elem : buf) {
144 GetOutput().push_back(elem);
145 }
146 }
147
148 } else {
149 MPI_Status status;
150
1/2
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 std::vector<int> buf(2);
151
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 MPI_Recv(buf.data(), 2, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
152
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 std::vector<double> elems = SummValues(buf[0], buf[1]);
153 9 int size_elems = static_cast<int>(elems.size());
154
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 MPI_Send(&size_elems, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
155
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
9 MPI_Send(elems.data(), size_elems, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD);
156 }
157
158 18 int total_size = 0;
159
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (rank == 0) {
160 9 total_size = static_cast<int>(GetOutput().size());
161 }
162
163 18 MPI_Bcast(&total_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
164
165
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
18 if (rank != 0) {
166 9 GetOutput().resize(total_size);
167 }
168
169 18 MPI_Bcast(GetOutput().data(), total_size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
170
171 return true;
172 }
173
174 20 bool SafronovMSumValuesMatrixMPI::PostProcessingImpl() {
175 20 return true;
176 }
177
178 } // namespace safronov_m_sum_values_matrix
179