GCC Code Coverage Report


Directory: ./
File: tasks/fatehov_k_matrix_max_elem/mpi/src/ops_mpi.cpp
Date: 2026-01-10 02:40:41
Exec Total Coverage
Lines: 45 45 100.0%
Functions: 5 5 100.0%
Branches: 31 48 64.6%

Line Branch Exec Source
1 #include "fatehov_k_matrix_max_elem/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm>
6 #include <cmath>
7 #include <cstddef>
8 #include <limits>
9 #include <utility>
10 #include <vector>
11
12 #include "fatehov_k_matrix_max_elem/common/include/common.hpp"
13
14 namespace fatehov_k_matrix_max_elem {
15
16
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 FatehovKMatrixMaxElemMPI::FatehovKMatrixMaxElemMPI(const InType &in) {
17 SetTypeOfTask(GetStaticTypeOfTask());
18 GetInput() = in;
19 6 GetOutput() = 0;
20 6 }
21
22 6 bool FatehovKMatrixMaxElemMPI::ValidationImpl() {
23 auto &data = GetInput();
24
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 return (std::get<0>(data) > 0 && std::get<0>(data) <= kMaxRows) &&
25
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 (std::get<1>(data) > 0 && std::get<1>(data) <= kMaxCols) &&
26
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 (std::get<0>(data) * std::get<1>(data) <= kMaxMatrixSize) &&
27
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 (std::get<2>(data).size() <= kMaxMatrixSize &&
28
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
12 std::get<2>(data).size() == std::get<0>(data) * std::get<1>(data)) &&
29 6 (!std::get<2>(data).empty());
30 }
31
32 6 bool FatehovKMatrixMaxElemMPI::PreProcessingImpl() {
33 6 return true;
34 }
35
36 6 bool FatehovKMatrixMaxElemMPI::RunImpl() {
37 6 int world_rank = 0;
38 6 int world_size = 0;
39 6 MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
40 6 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
41
42 6 size_t rows = 0;
43 6 size_t columns = 0;
44
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (world_rank == 0) {
45 auto &data = GetInput();
46 3 rows = std::get<0>(data);
47 3 columns = std::get<1>(data);
48 }
49
50 6 MPI_Bcast(&rows, 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD);
51 6 MPI_Bcast(&columns, 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD);
52
53 6 size_t total_elems = rows * columns;
54 6 size_t elems_per_proc = total_elems / world_size;
55 6 size_t remainder = total_elems % world_size;
56
57 6 std::vector<int> send_counts(world_size);
58
1/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 std::vector<int> displacements(world_size);
59
60
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (int i = 0; i < world_size; ++i) {
61
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6 times.
16 send_counts[i] = static_cast<int>(elems_per_proc) + (std::cmp_less(i, remainder) ? 1 : 0);
62
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 displacements[i] = (i == 0) ? 0 : (displacements[i - 1] + send_counts[i - 1]);
63 }
64
65
1/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 std::vector<double> local_data(send_counts[world_rank]);
66 const std::vector<double> *full_matrix_ptr = nullptr;
67
68
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (world_rank == 0) {
69 auto &data = GetInput();
70 full_matrix_ptr = &std::get<2>(data);
71 }
72
73
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
9 MPI_Scatterv((world_rank == 0) ? (*full_matrix_ptr).data() : nullptr, send_counts.data(), displacements.data(),
74
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 MPI_DOUBLE, local_data.data(), send_counts[world_rank], MPI_DOUBLE, 0, MPI_COMM_WORLD);
75
76 6 double local_max = -std::numeric_limits<double>::max();
77
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 6 times.
52 for (const auto &value : local_data) {
78 46 local_max = std::max(value, local_max);
79 }
80
81 6 double global_max = NAN;
82
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MPI_Allreduce(&local_max, &global_max, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
83
84
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 GetOutput() = global_max;
85
86 6 return true;
87 }
88
89 6 bool FatehovKMatrixMaxElemMPI::PostProcessingImpl() {
90 6 return true;
91 }
92
93 } // namespace fatehov_k_matrix_max_elem
94