GCC Code Coverage Report


Directory: ./
File: tasks/zagryadskov_m_radix_sort_double_simple_merge/mpi/src/radix_sort_double_simple_merge.cpp
Date: 2026-01-09 01:27:18
Exec Total Coverage
Lines: 75 81 92.6%
Functions: 6 6 100.0%
Branches: 44 86 51.2%

Line Branch Exec Source
1 #include "zagryadskov_m_radix_sort_double_simple_merge/mpi/include/radix_sort_double_simple_merge.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm>
6 #include <cstddef>
7 #include <cstdint>
8 #include <stdexcept>
9 #include <vector>
10
11 #include "zagryadskov_m_radix_sort_double_simple_merge/common/include/common.hpp"
12 #include "zagryadskov_m_radix_sort_double_simple_merge/seq/include/radix_sort_double_simple_merge.hpp"
13
14 namespace zagryadskov_m_radix_sort_double_simple_merge {
15
16
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 ZagryadskovMRadixSortDoubleSimpleMergeMPI::ZagryadskovMRadixSortDoubleSimpleMergeMPI(const InType &in) {
17 SetTypeOfTask(GetStaticTypeOfTask());
18 6 int world_rank = 0;
19 int err_code = 0;
20
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 err_code = MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
21
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (err_code != MPI_SUCCESS) {
22 throw std::runtime_error("MPI_Comm_rank failed");
23 }
24
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (world_rank == 0) {
25
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 GetInput() = in;
26 }
27 6 }
28
29 6 bool ZagryadskovMRadixSortDoubleSimpleMergeMPI::ValidationImpl() {
30 bool res = false;
31 6 int world_rank = 0;
32 int err_code = 0;
33 6 err_code = MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (err_code != MPI_SUCCESS) {
35 throw std::runtime_error("MPI_Comm_rank failed");
36 }
37
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (world_rank == 0) {
38 3 res = !GetInput().empty();
39 } else {
40 res = true;
41 }
42 6 return res;
43 }
44
45 6 bool ZagryadskovMRadixSortDoubleSimpleMergeMPI::PreProcessingImpl() {
46 6 return true;
47 }
48
49 6 void ZagryadskovMRadixSortDoubleSimpleMergeMPI::MyMPIMerge(std::vector<double> &data) {
50 6 int rank = 0;
51 6 int size = 0;
52 6 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
53 6 MPI_Comm_size(MPI_COMM_WORLD, &size);
54
55 6 std::vector<double> tmp;
56
57
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (int step = 1; step < size; step <<= 1) {
58
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if ((rank % (2 * step)) == step) {
59 3 uint64_t n = data.size();
60
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 MPI_Send(&n, 1, MPI_UINT64_T, rank - step, 0, MPI_COMM_WORLD);
61
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 MPI_Send(data.data(), static_cast<int>(n), MPI_DOUBLE, rank - step, 1, MPI_COMM_WORLD);
62
63 return;
64 }
65
66 3 int partner = rank + step;
67
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (partner < size) {
68 3 uint64_t recv_size = 0;
69
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 MPI_Recv(&recv_size, 1, MPI_UINT64_T, partner, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
70
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 tmp.resize(data.size() + recv_size);
71
1/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
3 std::vector<double> recvbuf(recv_size);
72
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 MPI_Recv(recvbuf.data(), static_cast<int>(recv_size), MPI_DOUBLE, partner, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
73 3 std::ranges::merge(data.begin(), data.end(), recvbuf.begin(), recvbuf.end(), tmp.begin());
74 data.swap(tmp);
75 }
76 }
77 }
78
79 6 bool ZagryadskovMRadixSortDoubleSimpleMergeMPI::RunImpl() {
80 6 int world_size = 0;
81 6 int world_rank = 0;
82 int err_code = 0;
83 bool res = true;
84 6 err_code = MPI_Comm_size(MPI_COMM_WORLD, &world_size);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (err_code != MPI_SUCCESS) {
86 throw std::runtime_error("MPI_Comm_size failed");
87 }
88 6 err_code = MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (err_code != MPI_SUCCESS) {
90 throw std::runtime_error("MPI_Comm_rank failed");
91 }
92 6 size_t data_size = 0;
93 6 auto world_size_st = static_cast<size_t>(world_size);
94 6 std::vector<double> data;
95 double *in_data = nullptr;
96
1/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 std::vector<int> sendcounts(world_size_st);
97
1/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 std::vector<int> displs(world_size_st);
98
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (world_rank == 0) {
99 3 data_size = GetInput().size();
100 in_data = GetInput().data();
101 }
102
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 err_code = MPI_Bcast(&data_size, 1, MPI_UNSIGNED_LONG_LONG, 0, MPI_COMM_WORLD);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (err_code != MPI_SUCCESS) {
104 throw std::runtime_error("MPI_Bcast failed");
105 }
106 6 size_t data_by_process = data_size / world_size_st;
107 MPI_Datatype datatype = MPI_DOUBLE;
108
109
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (size_t rk = 0; rk < world_size_st; ++rk) {
110
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 sendcounts[rk] = static_cast<int>(data_by_process + static_cast<size_t>(rk < (data_size % world_size_st)));
111
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (rk > 0) {
112 6 displs[rk] = displs[rk - 1] + sendcounts[rk - 1];
113 }
114 }
115
116
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 data.resize(sendcounts[world_rank]);
117
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 err_code = MPI_Scatterv(in_data, sendcounts.data(), displs.data(), datatype, data.data(), sendcounts[world_rank],
118 datatype, 0, MPI_COMM_WORLD);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (err_code != MPI_SUCCESS) {
120 throw std::runtime_error("MPI_Scatterv failed");
121 }
122
123
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 ZagryadskovMRadixSortDoubleSimpleMergeSEQ::RadixSortLSD(data.data(), data.size());
124
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MyMPIMerge(data);
125
126
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (world_rank == 0) {
127
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 GetOutput() = data;
128 3 res = !GetOutput().empty();
129 } else {
130 res = true;
131 }
132 6 return res;
133 }
134
135 6 bool ZagryadskovMRadixSortDoubleSimpleMergeMPI::PostProcessingImpl() {
136 6 int world_rank = 0;
137 6 MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
138 6 int size = 0;
139
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (world_rank == 0) {
140 3 size = static_cast<int>(GetOutput().size());
141 }
142 6 MPI_Bcast(&size, 1, MPI_INT, 0, MPI_COMM_WORLD);
143 6 GetOutput().resize(size);
144 6 MPI_Bcast(GetOutput().data(), size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
145 6 return !GetOutput().empty();
146 }
147
148 } // namespace zagryadskov_m_radix_sort_double_simple_merge
149