GCC Code Coverage Report


Directory: ./
File: tasks/zagryadskov_m_allreduce/seq/src/allreduce.cpp
Date: 2026-01-09 01:27:18
Exec Total Coverage
Lines: 7 78 9.0%
Functions: 1 6 16.7%
Branches: 3 74 4.1%

Line Branch Exec Source
1 #include "zagryadskov_m_allreduce/seq/include/allreduce.hpp"
2
3 #include <mpi.h>
4
5 #include <cstddef>
6 #include <stdexcept>
7
8 #include "zagryadskov_m_allreduce/common/include/common.hpp"
9
10 namespace zagryadskov_m_allreduce {
11
12 ZagryadskovMAllreduceSEQ::ZagryadskovMAllreduceSEQ(const InType &in) {
13 SetTypeOfTask(GetStaticTypeOfTask());
14 int world_rank = 0;
15 int err_code = 0;
16 err_code = MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
17 if (err_code != MPI_SUCCESS) {
18 throw std::runtime_error("MPI_Comm_rank failed");
19 }
20 if (world_rank == 0) {
21 GetInput() = in;
22 }
23 }
24
25 bool ZagryadskovMAllreduceSEQ::ValidationImpl() {
26 bool res = false;
27 int world_rank = 0;
28 int world_size = 0;
29 int err_code = 0;
30 err_code = MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
31 if (err_code != MPI_SUCCESS) {
32 throw std::runtime_error("MPI_Comm_rank failed");
33 }
34 err_code = MPI_Comm_size(MPI_COMM_WORLD, &world_size);
35 if (err_code != MPI_SUCCESS) {
36 throw std::runtime_error("MPI_Comm_rank failed");
37 }
38 if (world_rank == 0) {
39 auto &param1 = std::get<0>(GetInput());
40 int param2 = std::get<1>(GetInput());
41 int param3 = std::get<2>(GetInput());
42
43 res = (!param1.empty()) && (param3 >= 0) && (param3 <= 2) && (param2 > 0) &&
44 (param1.size() >= static_cast<size_t>(param2) * static_cast<size_t>(world_size));
45 } else {
46 res = true;
47 }
48 return res;
49 }
50
51 bool ZagryadskovMAllreduceSEQ::PreProcessingImpl() {
52 int world_rank = 0;
53 int err_code = 0;
54 int count = 0;
55 err_code = MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
56 if (err_code != MPI_SUCCESS) {
57 throw std::runtime_error("MPI_Comm_rank failed");
58 }
59 if (world_rank == 0) {
60 count = std::get<1>(GetInput());
61 }
62
63 err_code = MPI_Bcast(&count, 1, MPI_INT, 0, MPI_COMM_WORLD);
64 if (err_code != MPI_SUCCESS) {
65 throw std::runtime_error("MPI_Bcast failed");
66 }
67 temp_vec_.resize(count);
68
69 err_code =
70 MPI_Scatter(std::get<0>(GetInput()).data(), count, MPI_INT, temp_vec_.data(), count, MPI_INT, 0, MPI_COMM_WORLD);
71 if (err_code != MPI_SUCCESS) {
72 throw std::runtime_error("MPI_Scatter failed");
73 }
74
75 err_code = MPI_Barrier(MPI_COMM_WORLD);
76 if (err_code != MPI_SUCCESS) {
77 throw std::runtime_error("MPI_Barrier failed");
78 }
79 return true;
80 }
81
82 12 MPI_Op ZagryadskovMAllreduceSEQ::GetOp(int iop) {
83 MPI_Op op = MPI_OP_NULL;
84
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
12 switch (iop) {
85 case 0:
86 op = MPI_MAX;
87 break;
88 4 case 1:
89 op = MPI_MIN;
90 4 break;
91 4 case 2:
92 op = MPI_SUM;
93 4 break;
94 default:
95 op = MPI_OP_NULL;
96 break;
97 }
98
99 12 return op;
100 }
101
102 bool ZagryadskovMAllreduceSEQ::RunImpl() {
103 int world_size = 0;
104 int world_rank = 0;
105 int err_code = 0;
106 int iop = 0;
107 err_code = MPI_Comm_size(MPI_COMM_WORLD, &world_size);
108 if (err_code != MPI_SUCCESS) {
109 throw std::runtime_error("MPI_Comm_size failed");
110 }
111 err_code = MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
112 if (err_code != MPI_SUCCESS) {
113 throw std::runtime_error("MPI_Comm_rank failed");
114 }
115 if (world_rank == 0) {
116 iop = std::get<2>(GetInput());
117 }
118 err_code = MPI_Bcast(&iop, 1, MPI_INT, 0, MPI_COMM_WORLD);
119 if (err_code != MPI_SUCCESS) {
120 throw std::runtime_error("MPI_Bcast failed");
121 }
122
123 GetOutput().resize(temp_vec_.size());
124 MPI_Op op = ZagryadskovMAllreduceSEQ::GetOp(iop);
125 MPI_Allreduce(temp_vec_.data(), GetOutput().data(), static_cast<int>(temp_vec_.size()), MPI_INT, op, MPI_COMM_WORLD);
126
127 err_code = MPI_Barrier(MPI_COMM_WORLD);
128 if (err_code != MPI_SUCCESS) {
129 throw std::runtime_error("MPI_Barrier failed");
130 }
131 return true;
132 }
133
134 bool ZagryadskovMAllreduceSEQ::PostProcessingImpl() {
135 bool result = false;
136 int world_rank = 0;
137 MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
138 if (world_rank == 0) {
139 result = !GetOutput().empty();
140 } else {
141 result = true;
142 }
143 return result;
144 }
145
146 } // namespace zagryadskov_m_allreduce
147