GCC Code Coverage Report


Directory: ./
File: tasks/goriacheva_k_reduce/mpi/src/ops_mpi.cpp
Date: 2026-01-10 02:40:41
Exec Total Coverage
Lines: 47 47 100.0%
Functions: 5 5 100.0%
Branches: 28 36 77.8%

Line Branch Exec Source
1 #include "goriacheva_k_reduce/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <numeric>
6 #include <vector>
7
8 #include "goriacheva_k_reduce/common/include/common.hpp"
9
10 namespace goriacheva_k_reduce {
11
12
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 GoriachevaKReduceMPI::GoriachevaKReduceMPI(const InType &in) {
13
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 InType tmp(in);
14 GetInput().swap(tmp);
15 SetTypeOfTask(GetStaticTypeOfTask());
16 10 }
17
18 10 bool GoriachevaKReduceMPI::ValidationImpl() {
19 10 return true;
20 }
21
22 10 bool GoriachevaKReduceMPI::PreProcessingImpl() {
23 10 GetOutput().resize(1);
24 10 return true;
25 }
26
27 10 bool GoriachevaKReduceMPI::RunImpl() {
28 10 int rank = 0;
29 10 int size = 0;
30 10 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
31 10 MPI_Comm_size(MPI_COMM_WORLD, &size);
32
33 10 int total_size = 0;
34
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 if (rank == 0) {
35 5 total_size = static_cast<int>(GetInput().size());
36 }
37
38 10 MPI_Bcast(&total_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
39
40
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (total_size == 0) {
41 2 GetOutput()[0] = 0;
42 2 return true;
43 }
44
45 8 int base = total_size / size;
46 8 int rem = total_size % size;
47
48
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
8 int local_size = base + (rank < rem ? 1 : 0);
49 8 local_input_.resize(local_size);
50
51 8 std::vector<int> sendcounts;
52 8 std::vector<int> displs;
53
54
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (rank == 0) {
55
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 sendcounts.resize(size);
56
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 displs.resize(size);
57 int offset = 0;
58
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (int i = 0; i < size; ++i) {
59
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
13 sendcounts[i] = base + (i < rem ? 1 : 0);
60 8 displs[i] = offset;
61 8 offset += sendcounts[i];
62 }
63 }
64
65
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
12 MPI_Scatterv(rank == 0 ? GetInput().data() : nullptr, sendcounts.data(), displs.data(), MPI_INT, local_input_.data(),
66 local_size, MPI_INT, 0, MPI_COMM_WORLD);
67
68 8 int local_sum = std::accumulate(local_input_.begin(), local_input_.end(), 0);
69 8 int global_sum = 0;
70
71
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (rank == 0) {
72 4 global_sum = local_sum;
73
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (int i = 1; i < size; ++i) {
74 4 int tmp = 0;
75
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 MPI_Recv(&tmp, 1, MPI_INT, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
76 4 global_sum += tmp;
77 }
78 } else {
79
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 MPI_Send(&local_sum, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
80 }
81
82
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 MPI_Bcast(&global_sum, 1, MPI_INT, 0, MPI_COMM_WORLD);
83
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 GetOutput()[0] = global_sum;
84
85 return true;
86 }
87
88 10 bool GoriachevaKReduceMPI::PostProcessingImpl() {
89 10 return true;
90 }
91
92 } // namespace goriacheva_k_reduce
93