GCC Code Coverage Report


Directory: ./
File: tasks/vector_scalar_product/mpi/src/ops_mpi.cpp
Date: 2026-01-10 02:40:41
Exec Total Coverage
Lines: 45 45 100.0%
Functions: 7 7 100.0%
Branches: 27 40 67.5%

Line Branch Exec Source
1 #include "vector_scalar_product/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <cstddef>
6 #include <numeric>
7 #include <vector>
8
9 #include "vector_scalar_product/common/include/common.hpp"
10
11 namespace vector_scalar_product {
12 namespace {
13 6 std::vector<int> BuildCounts(int total, int parts) {
14 6 std::vector<int> counts(parts, 0);
15 6 const int base = total / parts;
16 6 int remainder = total % parts;
17
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (int i = 0; i < parts; ++i) {
18
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 8 times.
20 counts[i] = base + (remainder > 0 ? 1 : 0);
19
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
12 if (remainder > 0) {
20 4 --remainder;
21 }
22 }
23 6 return counts;
24 }
25
26 6 std::vector<int> BuildDisplacements(const std::vector<int> &counts) {
27 6 std::vector<int> displs(counts.size(), 0);
28
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 for (std::size_t i = 1; i < counts.size(); ++i) {
29 6 displs[i] = displs[i - 1] + counts[i - 1];
30 }
31 6 return displs;
32 }
33 } // namespace
34
35
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 VectorScalarProductMpi::VectorScalarProductMpi(const InType &in) {
36 SetTypeOfTask(GetStaticTypeOfTask());
37 GetInput() = in;
38 6 GetOutput() = 0.0;
39 6 }
40
41
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 bool VectorScalarProductMpi::ValidationImpl() {
42 const auto &lhs = GetInput().lhs;
43 const auto &rhs = GetInput().rhs;
44
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 return !lhs.empty() && lhs.size() == rhs.size();
45 }
46
47 6 bool VectorScalarProductMpi::PreProcessingImpl() {
48 6 MPI_Comm_rank(MPI_COMM_WORLD, &rank_);
49 6 MPI_Comm_size(MPI_COMM_WORLD, &world_size_);
50
51 const auto &lhs = GetInput().lhs;
52 const auto &rhs = GetInput().rhs;
53 6 int global_size = 0;
54
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (rank_ == 0) {
55 3 global_size = static_cast<int>(lhs.size());
56 }
57 6 MPI_Bcast(&global_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
58
59 // Prepare scattering meta information
60 6 auto counts = BuildCounts(global_size, world_size_);
61
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 auto displs = BuildDisplacements(counts);
62
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 const int local_count = counts[rank_];
63
64
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 local_lhs_.assign(static_cast<std::size_t>(local_count), 0.0);
65
1/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 local_rhs_.assign(static_cast<std::size_t>(local_count), 0.0);
66
67
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 const double *lhs_ptr = rank_ == 0 ? lhs.data() : nullptr;
68
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 const double *rhs_ptr = rank_ == 0 ? rhs.data() : nullptr;
69
70
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MPI_Scatterv(lhs_ptr, counts.data(), displs.data(), MPI_DOUBLE, local_lhs_.data(), local_count, MPI_DOUBLE, 0,
71 MPI_COMM_WORLD);
72
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MPI_Scatterv(rhs_ptr, counts.data(), displs.data(), MPI_DOUBLE, local_rhs_.data(), local_count, MPI_DOUBLE, 0,
73 MPI_COMM_WORLD);
74
75 6 local_sum_ = 0.0;
76
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 result_ = 0.0;
77 6 return true;
78 }
79
80 6 bool VectorScalarProductMpi::RunImpl() {
81 6 local_sum_ = std::inner_product(local_lhs_.begin(), local_lhs_.end(), local_rhs_.begin(), 0.0);
82
83 6 return MPI_Reduce(&local_sum_, &result_, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD) == MPI_SUCCESS;
84 }
85
86 6 bool VectorScalarProductMpi::PostProcessingImpl() {
87 6 MPI_Bcast(&result_, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
88 6 GetOutput() = result_;
89 6 return true;
90 }
91
92 } // namespace vector_scalar_product
93