GCC Code Coverage Report


Directory: ./
File: tasks/dergachev_a_max_elem_vec/mpi/src/ops_mpi.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 44 44 100.0%
Functions: 5 5 100.0%
Branches: 20 28 71.4%

Line Branch Exec Source
1 #include "dergachev_a_max_elem_vec/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm>
6 #include <limits>
7 #include <vector>
8
9 #include "dergachev_a_max_elem_vec/common/include/common.hpp"
10
11 namespace dergachev_a_max_elem_vec {
12
13 24 DergachevAMaxElemVecMPI::DergachevAMaxElemVecMPI(const InType &in) {
14 SetTypeOfTask(GetStaticTypeOfTask());
15 24 GetInput() = in;
16 24 GetOutput() = std::numeric_limits<InType>::min();
17 24 }
18
19 24 bool DergachevAMaxElemVecMPI::ValidationImpl() {
20 24 int process_rank = 0;
21 24 MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
22
23
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 if (process_rank == 0) {
24 12 return (GetInput() > 0);
25 }
26 return true;
27 }
28
29 24 bool DergachevAMaxElemVecMPI::PreProcessingImpl() {
30 24 int process_rank = 0;
31 24 int total_processes = 0;
32 24 MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
33 24 MPI_Comm_size(MPI_COMM_WORLD, &total_processes);
34
35
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 if (process_rank == 0) {
36 12 vector_size_ = GetInput();
37
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (vector_size_ <= 0) {
38 return false;
39 }
40 }
41
42 24 MPI_Bcast(&vector_size_, 1, MPI_INT, 0, MPI_COMM_WORLD);
43
44 24 const int base_chunk_size = vector_size_ / total_processes;
45 24 const int remainder = vector_size_ % total_processes;
46
47 24 std::vector<int> send_counts(total_processes);
48
1/4
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
24 std::vector<int> displacements(total_processes);
49
50
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
72 for (int i = 0; i < total_processes; ++i) {
51
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 14 times.
82 send_counts[i] = base_chunk_size + (i < remainder ? 1 : 0);
52 48 displacements[i] = (i * base_chunk_size) + std::min(i, remainder);
53 }
54
55 24 std::vector<InType> full_data;
56
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 if (process_rank == 0) {
57
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 full_data.resize(vector_size_);
58
2/2
✓ Branch 0 taken 50613 times.
✓ Branch 1 taken 12 times.
50625 for (int idx = 0; idx < vector_size_; ++idx) {
59 50613 full_data[idx] = ((idx * 7) % 2000) - 1000;
60 }
61 }
62
63
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 local_data_.resize(send_counts[process_rank]);
64
65
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 MPI_Scatterv(full_data.data(), send_counts.data(), displacements.data(), MPI_INT, local_data_.data(),
66
1/2
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
24 send_counts[process_rank], MPI_INT, 0, MPI_COMM_WORLD);
67
68 return true;
69 }
70
71 24 bool DergachevAMaxElemVecMPI::RunImpl() {
72 24 InType local_maximum = std::numeric_limits<InType>::min();
73
74
2/2
✓ Branch 0 taken 50613 times.
✓ Branch 1 taken 24 times.
50637 for (const auto &value : local_data_) {
75 50613 local_maximum = std::max(value, local_maximum);
76 }
77
78 24 InType global_maximum = std::numeric_limits<InType>::min();
79 24 MPI_Allreduce(&local_maximum, &global_maximum, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
80
81 24 GetOutput() = global_maximum;
82
83 24 return true;
84 }
85
86 24 bool DergachevAMaxElemVecMPI::PostProcessingImpl() {
87 24 int process_rank = 0;
88 24 MPI_Comm_rank(MPI_COMM_WORLD, &process_rank);
89
90 if (process_rank == 0) {
91 return GetOutput() >= std::numeric_limits<InType>::min();
92 }
93 return true;
94 }
95
96 } // namespace dergachev_a_max_elem_vec
97