GCC Code Coverage Report


Directory: ./
File: tasks/kamalagin_a_vec_mat_mult/mpi/src/ops_mpi.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 65 66 98.5%
Functions: 6 6 100.0%
Branches: 45 76 59.2%

Line Branch Exec Source
1 #include "kamalagin_a_vec_mat_mult/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <cstddef>
6 #include <cstdint>
7 #include <vector>
8
9 #include "kamalagin_a_vec_mat_mult/common/include/common.hpp"
10
11 namespace kamalagin_a_vec_mat_mult {
12
13 namespace {
14
15 16 void BuildCountsDispls(int n, int size, std::vector<int> *counts, std::vector<int> *displs) {
16 16 counts->assign(size, 0);
17 16 displs->assign(size, 0);
18
19 16 const int base = n / size;
20 16 const int rem = n % size;
21
22 int offset = 0;
23
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 for (int proc = 0; proc < size; ++proc) {
24
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 10 times.
32 const int cnt = base + ((proc < rem) ? 1 : 0);
25 32 (*counts)[proc] = cnt;
26 32 (*displs)[proc] = offset;
27 32 offset += cnt;
28 }
29 16 }
30
31 } // namespace
32
33
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 KamalaginAVecMatMultMPI::KamalaginAVecMatMultMPI(const InType &in) {
34 SetTypeOfTask(GetStaticTypeOfTask());
35 GetInput() = in;
36 GetOutput().clear();
37 16 }
38
39 16 bool KamalaginAVecMatMultMPI::ValidationImpl() {
40 const auto &[n, m, a_flat, x] = GetInput();
41
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 if (n < 0 || m < 0) {
42 return false;
43 }
44
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (static_cast<std::size_t>(n) * static_cast<std::size_t>(m) != a_flat.size()) {
45 return false;
46 }
47
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (static_cast<std::size_t>(m) != x.size()) {
48 return false;
49 }
50 return true;
51 }
52
53 16 bool KamalaginAVecMatMultMPI::PreProcessingImpl() {
54 const auto &[n, m, a_flat, x] = GetInput();
55 (void)m;
56 (void)a_flat;
57 (void)x;
58 16 GetOutput().assign(static_cast<std::size_t>(n), 0);
59 16 return true;
60 }
61
62 16 bool KamalaginAVecMatMultMPI::RunImpl() {
63 16 int rank = 0;
64 16 int size = 1;
65 16 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
66 16 MPI_Comm_size(MPI_COMM_WORLD, &size);
67
68 16 int n = 0;
69 16 int m = 0;
70 16 std::vector<int> a_root;
71 16 std::vector<int> x;
72
73
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (rank == 0) {
74 const auto &input = GetInput();
75 8 n = std::get<0>(input);
76
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 m = std::get<1>(input);
77
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 a_root = std::get<2>(input);
78
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 x = std::get<3>(input);
79 }
80
81
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
82
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 MPI_Bcast(&m, 1, MPI_INT, 0, MPI_COMM_WORLD);
83
84
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 if (m > 0) {
85
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (rank != 0) {
86
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 x.resize(static_cast<std::size_t>(m));
87 }
88
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 MPI_Bcast(x.data(), m, MPI_INT, 0, MPI_COMM_WORLD);
89 }
90
91 16 std::vector<int> rows_counts;
92 16 std::vector<int> rows_displs;
93
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 BuildCountsDispls(n, size, &rows_counts, &rows_displs);
94
95
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 const int local_rows = rows_counts[rank];
96
1/4
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
16 std::vector<int> a_local(static_cast<std::size_t>(local_rows) * static_cast<std::size_t>(m));
97
98
1/4
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
16 std::vector<int> sendcounts_a(static_cast<std::size_t>(size));
99
1/4
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
16 std::vector<int> displs_a(static_cast<std::size_t>(size));
100
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 for (int proc = 0; proc < size; ++proc) {
101 32 sendcounts_a[proc] = rows_counts[proc] * m;
102 32 displs_a[proc] = rows_displs[proc] * m;
103 }
104
105
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
24 MPI_Scatterv(rank == 0 ? a_root.data() : nullptr, sendcounts_a.data(), displs_a.data(), MPI_INT, a_local.data(),
106 local_rows * m, MPI_INT, 0, MPI_COMM_WORLD);
107
108
1/4
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
16 std::vector<int> local_y(static_cast<std::size_t>(local_rows), 0);
109
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 16 times.
295 for (int i = 0; i < local_rows; ++i) {
110 std::int64_t sum = 0;
111
2/2
✓ Branch 0 taken 2606 times.
✓ Branch 1 taken 279 times.
2885 for (int j = 0; j < m; ++j) {
112 2606 sum +=
113 2606 static_cast<std::int64_t>(a_local[(i * m) + j]) * static_cast<std::int64_t>(x[static_cast<std::size_t>(j)]);
114 }
115 279 local_y[static_cast<std::size_t>(i)] = static_cast<int>(sum);
116 }
117
118
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 const std::vector<int> recvcounts_y = rows_counts;
119
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 const std::vector<int> displs_y = rows_displs;
120
121
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
24 MPI_Gatherv(local_y.data(), local_rows, MPI_INT, rank == 0 ? GetOutput().data() : nullptr, recvcounts_y.data(),
122 displs_y.data(), MPI_INT, 0, MPI_COMM_WORLD);
123
124
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 if (n > 0) {
125
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 MPI_Bcast(GetOutput().data(), n, MPI_INT, 0, MPI_COMM_WORLD);
126 }
127
128 16 return true;
129 }
130
131 16 bool KamalaginAVecMatMultMPI::PostProcessingImpl() {
132 16 return true;
133 }
134
135 } // namespace kamalagin_a_vec_mat_mult
136