GCC Code Coverage Report


Directory: ./
File: tasks/morozova_s_matrix_max_value/mpi/src/ops_mpi.cpp
Date: 2026-01-10 02:40:41
Exec Total Coverage
Lines: 41 43 95.3%
Functions: 5 5 100.0%
Branches: 23 42 54.8%

Line Branch Exec Source
1 #include "morozova_s_matrix_max_value/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm>
6 #include <cstddef>
7 #include <vector>
8
9 #include "morozova_s_matrix_max_value/common/include/common.hpp"
10
11 namespace morozova_s_matrix_max_value {
12
13
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MorozovaSMatrixMaxValueMPI::MorozovaSMatrixMaxValueMPI(const InType &in) : BaseTask() {
14 SetTypeOfTask(GetStaticTypeOfTask());
15
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 GetInput() = InType(in);
16 6 GetOutput() = 0;
17 6 }
18
19
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 bool MorozovaSMatrixMaxValueMPI::ValidationImpl() {
20 const auto &matrix = GetInput();
21
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (matrix.empty()) {
22 return true;
23 }
24
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (matrix[0].empty()) {
25 return true;
26 }
27 const size_t cols = matrix[0].size();
28 return std::ranges::all_of(matrix, [cols](const auto &row) { return row.size() == cols; });
29 }
30
31 6 bool MorozovaSMatrixMaxValueMPI::PreProcessingImpl() {
32 6 return true;
33 }
34
35 6 bool MorozovaSMatrixMaxValueMPI::RunImpl() {
36 6 int rank = 0;
37 6 int size = 1;
38 6 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
39 6 MPI_Comm_size(MPI_COMM_WORLD, &size);
40 const auto &matrix = GetInput();
41
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (matrix.empty() || matrix[0].empty()) {
42 GetOutput() = 0;
43 return true;
44 }
45 6 const int rows = static_cast<int>(matrix.size());
46 6 const int cols = static_cast<int>(matrix[0].size());
47 6 const int total = rows * cols;
48 6 std::vector<int> flat;
49
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (rank == 0) {
50
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 flat.reserve(total);
51
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
15 for (const auto &row : matrix) {
52 12 flat.insert(flat.end(), row.begin(), row.end());
53 }
54 }
55
1/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 std::vector<int> counts(size);
56
1/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 std::vector<int> displs(size);
57 6 const int base = total / size;
58 6 const int rest = total % size;
59
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (int i = 0, off = 0; i < size; ++i) {
60 12 counts[i] = base + static_cast<int>(i < rest);
61 12 displs[i] = off;
62 12 off += counts[i];
63 }
64
1/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 std::vector<int> local(counts[rank]);
65
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
9 MPI_Scatterv(rank == 0 ? flat.data() : nullptr, counts.data(), displs.data(), MPI_INT, local.data(), counts[rank],
66 MPI_INT, 0, MPI_COMM_WORLD);
67
68 6 int local_max = local[0];
69
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 6 times.
56 for (int v : local) {
70 50 local_max = std::max(local_max, v);
71 }
72 6 int global_max = 0;
73
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MPI_Allreduce(&local_max, &global_max, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
74 6 GetOutput() = global_max;
75 return true;
76 }
77
78 6 bool MorozovaSMatrixMaxValueMPI::PostProcessingImpl() {
79 6 return true;
80 }
81
82 } // namespace morozova_s_matrix_max_value
83