GCC Code Coverage Report


Directory: ./
File: tasks/kosolapov_v_max_values_in_col_matrix/mpi/src/ops_mpi.cpp
Date: 2026-01-10 02:40:41
Exec Total Coverage
Lines: 67 67 100.0%
Functions: 7 7 100.0%
Branches: 49 72 68.1%

Line Branch Exec Source
1 #include "kosolapov_v_max_values_in_col_matrix/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm>
6 #include <cstddef>
7 #include <limits>
8 #include <vector>
9
10 #include "kosolapov_v_max_values_in_col_matrix/common/include/common.hpp"
11
12 namespace kosolapov_v_max_values_in_col_matrix {
13
14
1/2
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
26 KosolapovVMaxValuesInColMatrixMPI::KosolapovVMaxValuesInColMatrixMPI(const InType &in) {
15 SetTypeOfTask(GetStaticTypeOfTask());
16
2/4
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✓ Branch 6 taken 26 times.
✗ Branch 7 not taken.
26 GetInput() = InType(in);
17 GetOutput() = {};
18 26 }
19
20 26 bool KosolapovVMaxValuesInColMatrixMPI::ValidationImpl() {
21 26 int rank = 0;
22 26 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
23
24
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 if (rank == 0) {
25 const auto &matrix = GetInput();
26
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (matrix.empty()) {
27 return false;
28 }
29
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 13 times.
67 for (size_t i = 0; i < matrix.size() - 1; i++) {
30
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 54 times.
54 if ((matrix[i].size() != matrix[i + 1].size()) || (matrix[i].empty())) {
31 return false;
32 }
33 }
34 }
35 26 return (GetOutput().empty());
36 }
37
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 bool KosolapovVMaxValuesInColMatrixMPI::PreProcessingImpl() {
39 GetOutput().clear();
40
41 26 int rank = 0;
42 26 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
43
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 if (rank == 0) {
44 const auto &matrix = GetInput();
45
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 if (!matrix.empty() && !matrix[0].empty()) {
46 13 GetOutput().resize(matrix[0].size());
47 }
48 }
49 26 return true;
50 }
51
52 26 bool KosolapovVMaxValuesInColMatrixMPI::RunImpl() {
53 26 int processes_count = 0;
54 26 int rank = 0;
55 26 MPI_Comm_size(MPI_COMM_WORLD, &processes_count);
56 26 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
57
58 26 std::vector<std::vector<int>> local_matrix;
59 26 int rows = 0;
60 26 int columns = 0;
61
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 if (rank == 0) {
62 const auto &matrix = GetInput();
63 13 rows = static_cast<int>(matrix.size());
64 13 columns = static_cast<int>(matrix[0].size());
65 }
66
1/2
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
26 MPI_Bcast(&rows, 1, MPI_INT, 0, MPI_COMM_WORLD);
67
1/2
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
26 MPI_Bcast(&columns, 1, MPI_INT, 0, MPI_COMM_WORLD);
68
69 26 const int rows_per_proc = rows / processes_count;
70 26 const int remainder = rows % processes_count;
71
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 7 times.
26 const int start = (rank * rows_per_proc) + std::min(rank, remainder);
72
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 7 times.
26 const int end = start + rows_per_proc + (rank < remainder ? 1 : 0);
73 26 const int local_rows = end - start;
74
75
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 if (rank == 0) {
76
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
13 DistributeDataFromRoot(local_matrix, start, local_rows, columns, processes_count, rows_per_proc, remainder);
77 } else {
78
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
13 local_matrix.resize(local_rows);
79
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 13 times.
43 for (int i = 0; i < local_rows; i++) {
80
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 local_matrix[i].resize(columns);
81
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 MPI_Recv(local_matrix[i].data(), columns, MPI_INT, 0, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
82 }
83 }
84
1/2
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
26 auto local_maxs = CalculateLocalMax(local_matrix, columns);
85
1/4
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
26 std::vector<int> global_maxs(columns);
86
1/2
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
26 MPI_Allreduce(local_maxs.data(), global_maxs.data(), columns, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
87
1/2
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
26 GetOutput() = global_maxs;
88 26 return true;
89 26 }
90
91 26 bool KosolapovVMaxValuesInColMatrixMPI::PostProcessingImpl() {
92 26 return true;
93 }
94
95 13 void KosolapovVMaxValuesInColMatrixMPI::DistributeDataFromRoot(std::vector<std::vector<int>> &local_matrix, int start,
96 int local_rows, int columns, int processes_count,
97 int rows_per_proc, int remainder) {
98 const auto &matrix = GetInput();
99 13 local_matrix.resize(local_rows);
100
101
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 13 times.
50 for (int i = 0; i < local_rows; i++) {
102 37 local_matrix[i] = matrix[start + i];
103 }
104
105
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 for (int proc = 1; proc < processes_count; proc++) {
106
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 const int proc_start = (proc * rows_per_proc) + std::min(proc, remainder);
107
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 const int proc_end = proc_start + rows_per_proc + (proc < remainder ? 1 : 0);
108 13 const int proc_rows_count = proc_end - proc_start;
109
110
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 13 times.
43 for (int i = 0; i < proc_rows_count; i++) {
111 30 MPI_Send(matrix[proc_start + i].data(), columns, MPI_INT, proc, i, MPI_COMM_WORLD);
112 }
113 }
114 13 }
115 26 std::vector<int> KosolapovVMaxValuesInColMatrixMPI::CalculateLocalMax(const std::vector<std::vector<int>> &matrix,
116 const int columns) {
117 26 std::vector<int> local_maxs(columns, std::numeric_limits<int>::min());
118
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 26 times.
93 for (const auto &row : matrix) {
119
2/2
✓ Branch 0 taken 603 times.
✓ Branch 1 taken 67 times.
670 for (int i = 0; i < columns; i++) {
120
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 279 times.
927 local_maxs[i] = std::max(row[i], local_maxs[i]);
121 }
122 }
123 26 return local_maxs;
124 }
125 } // namespace kosolapov_v_max_values_in_col_matrix
126