GCC Code Coverage Report


Directory: ./
File: tasks/bortsova_a_max_elem_vector/mpi/src/ops_mpi.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 57 57 100.0%
Functions: 5 5 100.0%
Branches: 31 48 64.6%

Line Branch Exec Source
1 #include "bortsova_a_max_elem_vector/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm>
6 #include <cstddef>
7 #include <limits>
8 #include <vector>
9
10 #include "bortsova_a_max_elem_vector/common/include/common.hpp"
11
12 namespace bortsova_a_max_elem_vector {
13
14
1/2
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
88 BortsovaAMaxElemVectorMpi::BortsovaAMaxElemVectorMpi(const InType &in) {
15 SetTypeOfTask(GetStaticTypeOfTask());
16 GetInput() = in;
17 88 GetOutput() = std::numeric_limits<int>::min();
18 88 }
19
20 88 bool BortsovaAMaxElemVectorMpi::ValidationImpl() {
21 88 int rank = 0;
22 88 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
23
24 88 int is_valid = 0;
25
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
88 if (rank == 0) {
26
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 is_valid = !GetInput().data.empty() ? 1 : 0;
27 }
28
29 88 MPI_Bcast(&is_valid, 1, MPI_INT, 0, MPI_COMM_WORLD);
30
31 88 return is_valid == 1;
32 }
33
34 88 bool BortsovaAMaxElemVectorMpi::PreProcessingImpl() {
35 88 int rank = 0;
36 88 int size = 0;
37 88 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
38 88 MPI_Comm_size(MPI_COMM_WORLD, &size);
39
40 88 GetOutput() = std::numeric_limits<int>::min();
41
42 88 int vec_size = 0;
43
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
88 if (rank == 0) {
44 44 vec_size = static_cast<int>(GetInput().data.size());
45 }
46
47 88 MPI_Bcast(&vec_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
48
49 88 return vec_size != 0;
50 }
51
52 88 bool BortsovaAMaxElemVectorMpi::RunImpl() {
53 88 int rank = 0;
54 88 int size = 0;
55 88 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
56 88 MPI_Comm_size(MPI_COMM_WORLD, &size);
57
58 88 std::vector<int> vec;
59 88 int vec_size = 0;
60
61
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
88 if (rank == 0) {
62
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 vec = GetInput().data;
63 44 vec_size = static_cast<int>(vec.size());
64 }
65
66
1/2
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
88 MPI_Bcast(&vec_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
67
68
1/2
✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
88 if (vec_size == 0) {
69 return false;
70 }
71
72 88 int chunk_size = vec_size / size;
73 88 int remainder = vec_size % size;
74
75
1/4
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
88 std::vector<int> sendcounts(size);
76
1/4
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
88 std::vector<int> displs(size);
77
78 int offset = 0;
79
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 88 times.
264 for (int i = 0; i < size; i++) {
80
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 48 times.
304 sendcounts[i] = chunk_size + (i < remainder ? 1 : 0);
81 176 displs[i] = offset;
82 176 offset += sendcounts[i];
83 }
84
85
1/4
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
88 std::vector<int> local_data(sendcounts[rank]);
86
87
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
88 if (rank == 0) {
88
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 MPI_Scatterv(vec.data(), sendcounts.data(), displs.data(), MPI_INT, local_data.data(), sendcounts[rank], MPI_INT, 0,
89 MPI_COMM_WORLD);
90 } else {
91
1/2
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
44 MPI_Scatterv(nullptr, sendcounts.data(), displs.data(), MPI_INT, local_data.data(), sendcounts[rank], MPI_INT, 0,
92 MPI_COMM_WORLD);
93 }
94
95
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 6 times.
88 int local_max = std::numeric_limits<int>::min();
96
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 6 times.
88 if (!local_data.empty()) {
97 82 local_max = local_data[0];
98
2/2
✓ Branch 0 taken 2250752 times.
✓ Branch 1 taken 82 times.
2250834 for (size_t i = 1; i < local_data.size(); i++) {
99 2250752 local_max = std::max(local_data[i], local_max);
100 }
101 }
102
103 88 int global_max = std::numeric_limits<int>::min();
104
1/2
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
88 MPI_Reduce(&local_max, &global_max, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
105
106
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 44 times.
88 if (rank == 0) {
107 44 GetOutput() = global_max;
108 }
109
110 return true;
111 }
112
113 88 bool BortsovaAMaxElemVectorMpi::PostProcessingImpl() {
114 88 return true;
115 }
116
117 } // namespace bortsova_a_max_elem_vector
118