GCC Code Coverage Report


Directory: ./
File: tasks/barkalova_m_star/mpi/src/ops_mpi.cpp
Date: 2026-01-09 01:27:18
Exec Total Coverage
Lines: 0 53 0.0%
Functions: 0 8 0.0%
Branches: 0 42 0.0%

Line Branch Exec Source
1 #include "barkalova_m_star/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <cstddef>
6 #include <utility>
7 #include <vector>
8
9 #include "barkalova_m_star/common/include/common.hpp"
10
11 namespace barkalova_m_star {
12
13 BarkalovaMStarMPI::BarkalovaMStarMPI(const InType &in) {
14 SetTypeOfTask(GetStaticTypeOfTask());
15 GetInput() = in;
16 GetOutput() = {};
17 }
18
19 bool BarkalovaMStarMPI::ValidationImpl() {
20 int size = 0;
21 MPI_Comm_size(MPI_COMM_WORLD, &size);
22 const auto &input = GetInput();
23 if (input.source < 0 || input.source >= size) {
24 return false;
25 }
26 if (input.dest < 0 || input.dest >= size) {
27 return false;
28 }
29 return true;
30 }
31
32 bool BarkalovaMStarMPI::PreProcessingImpl() {
33 GetOutput() = {};
34 return true;
35 }
36
37 namespace {
38
39 void ToMyself(int rank, int source, const std::vector<int> &source_data, std::vector<int> &output) {
40 if (rank == source) {
41 output = source_data;
42 }
43 }
44
45 void ProcSource(int source, size_t data_size, const std::vector<int> &source_data) {
46 if (source != 0) {
47 MPI_Send(source_data.data(), static_cast<int>(data_size), MPI_INT, 0, 0, MPI_COMM_WORLD);
48 }
49 }
50
51 void ProcDest(int dest, size_t data_size, std::vector<int> &output) {
52 std::vector<int> buff(data_size);
53 if (dest != 0) {
54 MPI_Status status;
55 MPI_Recv(buff.data(), static_cast<int>(data_size), MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
56 }
57 output = std::move(buff);
58 }
59
60 void Center(int source, int dest, size_t data_size, const std::vector<int> &source_data, std::vector<int> &output) {
61 if (source != 0) {
62 std::vector<int> buff(data_size);
63 MPI_Status status;
64 MPI_Recv(buff.data(), static_cast<int>(data_size), MPI_INT, source, 0, MPI_COMM_WORLD, &status);
65 if (dest != 0) {
66 MPI_Send(buff.data(), static_cast<int>(data_size), MPI_INT, dest, 0, MPI_COMM_WORLD);
67 } else {
68 output = std::move(buff);
69 }
70 } else {
71 MPI_Send(source_data.data(), static_cast<int>(data_size), MPI_INT, dest, 0, MPI_COMM_WORLD);
72 }
73 }
74
75 void ToAnother(int rank, int source, int dest, size_t data_size, const std::vector<int> &source_data,
76 std::vector<int> &output) {
77 if (rank == dest || (rank == 0 && dest == 0)) {
78 output.resize(data_size);
79 }
80
81 if (rank == 0) {
82 Center(source, dest, data_size, source_data, output);
83 } else if (rank == source) {
84 ProcSource(source, data_size, source_data);
85 } else if (rank == dest) {
86 ProcDest(dest, data_size, output);
87 }
88 }
89 } // namespace
90
91 bool BarkalovaMStarMPI::RunImpl() {
92 int rank = 0;
93 int size = 0;
94 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
95 MPI_Comm_size(MPI_COMM_WORLD, &size);
96
97 const auto &input = GetInput();
98 int source = input.source;
99 int dest = input.dest;
100
101 if (size == 1) {
102 GetOutput() = GetInput().data;
103 return true;
104 }
105 const std::vector<int> &data = input.data;
106 size_t data_size = data.size();
107
108 if (rank == dest || (rank == 0 && dest == 0)) {
109 GetOutput().resize(data_size);
110 } else {
111 GetOutput() = {};
112 }
113
114 if (source == dest) {
115 ToMyself(rank, source, input.data, GetOutput());
116 } else {
117 ToAnother(rank, source, dest, data_size, input.data, GetOutput());
118 }
119
120 return true;
121 }
122
123 bool BarkalovaMStarMPI::PostProcessingImpl() {
124 return true;
125 }
126
127 } // namespace barkalova_m_star
128