GCC Code Coverage Report


Directory: ./
File: tasks/batkov_f_vector_sum/mpi/src/ops_mpi.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 48 48 100.0%
Functions: 5 5 100.0%
Branches: 23 36 63.9%

Line Branch Exec Source
1 #include "batkov_f_vector_sum/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <cstddef>
6 #include <limits>
7 #include <vector>
8
9 #include "batkov_f_vector_sum/common/include/common.hpp"
10
11 namespace batkov_f_vector_sum {
12
13
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 BatkovFVectorSumMPI::BatkovFVectorSumMPI(const InType &in) {
14 SetTypeOfTask(GetStaticTypeOfTask());
15
16 8 int rank = 0;
17 8 int mpi_size = 0;
18
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
19
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
20
21 8 m_rank_ = static_cast<size_t>(rank);
22 8 m_mpi_size_ = static_cast<size_t>(mpi_size);
23
24
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (m_rank_ == 0) {
25
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 GetInput() = in;
26 } else {
27 4 GetInput() = std::vector<int>();
28 }
29
30 8 GetOutput() = 0;
31 8 }
32
33 8 bool BatkovFVectorSumMPI::ValidationImpl() {
34 8 return GetOutput() == 0;
35 }
36
37 8 bool BatkovFVectorSumMPI::PreProcessingImpl() {
38 8 return true;
39 }
40
41 8 bool BatkovFVectorSumMPI::RunImpl() {
42 8 size_t input_size = 0;
43
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (m_rank_ == 0) {
44 4 input_size = GetInput().size();
45 }
46
47 8 MPI_Bcast(&input_size, 1, MPI_UNSIGNED_LONG_LONG, 0, MPI_COMM_WORLD);
48
49
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (input_size == 0) {
50 2 GetOutput() = 0;
51 2 return true;
52 }
53
54 6 const size_t base_chunk = input_size / m_mpi_size_;
55 6 const size_t remaining_elements = input_size % m_mpi_size_;
56
57 size_t chunk_size = base_chunk;
58
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (m_rank_ < remaining_elements) {
59 1 chunk_size = base_chunk + 1;
60 }
61 6 std::vector<int> local_data(chunk_size);
62
63
1/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 std::vector<int> send_counts(m_mpi_size_);
64
1/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 std::vector<int> displs(m_mpi_size_);
65 size_t disp = 0;
66
67
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (size_t i = 0; i < m_mpi_size_; ++i) {
68 size_t chunk = base_chunk;
69
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 if (i < remaining_elements) {
70 2 chunk = base_chunk + 1;
71 }
72
73 12 int int_chunk = static_cast<int>(chunk);
74 12 int int_disp = static_cast<int>(disp);
75
76 if (int_chunk > std::numeric_limits<int>::max() || int_disp > std::numeric_limits<int>::max()) {
77 return false;
78 }
79
80 12 send_counts[i] = int_chunk;
81 12 displs[i] = int_disp;
82 12 disp += chunk;
83 }
84
85
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MPI_Scatterv(GetInput().data(), send_counts.data(), displs.data(), MPI_INT, local_data.data(),
86 static_cast<int>(chunk_size), MPI_INT, 0, MPI_COMM_WORLD);
87
88 6 int local_sum = 0;
89
2/2
✓ Branch 0 taken 399 times.
✓ Branch 1 taken 6 times.
405 for (int val : local_data) {
90 399 local_sum += val;
91 }
92
93 6 int global_sum = 0;
94
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MPI_Allreduce(&local_sum, &global_sum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
95
96
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 GetOutput() = global_sum;
97 return true;
98 }
99
100 8 bool BatkovFVectorSumMPI::PostProcessingImpl() {
101 8 return true;
102 }
103
104 } // namespace batkov_f_vector_sum
105