GCC Code Coverage Report


Directory: ./
File: tasks/scalar_product/mpi/src/ops_mpi.cpp
Date: 2026-01-10 02:40:41
Exec Total Coverage
Lines: 35 35 100.0%
Functions: 5 5 100.0%
Branches: 25 40 62.5%

Line Branch Exec Source
1 #include "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 "scalar_product/common/include/common.hpp"
10
11 namespace scalar_product {
12
13
1/2
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
38 ScalarProductMPI::ScalarProductMPI(const InType &in) {
14 SetTypeOfTask(GetStaticTypeOfTask());
15 GetInput() = in;
16 38 GetOutput() = 0;
17 38 }
18
19
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 bool ScalarProductMPI::ValidationImpl() {
20 const auto &vector_a = GetInput().first;
21 const auto &vector_b = GetInput().second;
22
2/4
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
38 return vector_a.size() == vector_b.size() && !vector_a.empty();
23 }
24
25 38 bool ScalarProductMPI::PreProcessingImpl() {
26 38 MPI_Comm_rank(MPI_COMM_WORLD, &rank_);
27 38 MPI_Comm_size(MPI_COMM_WORLD, &world_size_);
28
29 const auto &vector_a = GetInput().first;
30 const auto &vector_b = GetInput().second;
31
32 38 int global_size = 0;
33
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 19 times.
38 if (rank_ == 0) {
34 19 global_size = static_cast<int>(vector_a.size());
35 }
36 38 MPI_Bcast(&global_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
37
38 38 std::vector<int> counts(world_size_);
39
1/4
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
38 std::vector<int> displase(world_size_, 0);
40
41 38 const int base = global_size / world_size_;
42 38 int remain = global_size % world_size_;
43
44
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 38 times.
114 for (int i = 0; i < world_size_; ++i) {
45
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 20 times.
132 counts[i] = base + (i < remain ? 1 : 0);
46 }
47
48
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 38 times.
76 for (int i = 1; i < world_size_; ++i) {
49 38 displase[i] = displase[i - 1] + counts[i - 1];
50 }
51
1/2
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
38 const int local_count = counts[rank_];
52
53
1/2
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
38 local_vector_a_.assign(static_cast<std::size_t>(local_count), 0);
54
3/6
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
38 local_vector_b_.assign(static_cast<std::size_t>(local_count), 0);
55
56
3/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 19 times.
✓ Branch 3 taken 38 times.
✗ Branch 4 not taken.
57 MPI_Scatterv(rank_ == 0 ? vector_a.data() : nullptr, counts.data(), displase.data(), MPI_INT, local_vector_a_.data(),
57 local_count, MPI_INT, 0, MPI_COMM_WORLD);
58
3/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 19 times.
✓ Branch 3 taken 38 times.
✗ Branch 4 not taken.
57 MPI_Scatterv(rank_ == 0 ? vector_b.data() : nullptr, counts.data(), displase.data(), MPI_INT, local_vector_b_.data(),
59 local_count, MPI_INT, 0, MPI_COMM_WORLD);
60
61 38 local_sum_ = 0;
62
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 result_ = 0;
63 38 return true;
64 }
65
66 38 bool ScalarProductMPI::RunImpl() {
67 38 local_sum_ = std::inner_product(local_vector_a_.begin(), local_vector_a_.end(), local_vector_b_.begin(), 0);
68 38 return MPI_Reduce(&local_sum_, &result_, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD) == MPI_SUCCESS;
69 }
70
71 38 bool ScalarProductMPI::PostProcessingImpl() {
72 38 MPI_Bcast(&result_, 1, MPI_INT, 0, MPI_COMM_WORLD);
73 38 GetOutput() = result_;
74 38 return true;
75 }
76
77 } // namespace scalar_product
78