GCC Code Coverage Report


Directory: ./
File: tasks/kruglova_a_max_diff_adjacent/mpi/src/ops_mpi.cpp
Date: 2026-01-10 02:40:41
Exec Total Coverage
Lines: 49 49 100.0%
Functions: 6 6 100.0%
Branches: 33 46 71.7%

Line Branch Exec Source
1 #include "kruglova_a_max_diff_adjacent/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm>
6 #include <cmath>
7 #include <cstddef>
8 #include <vector>
9
10 #include "kruglova_a_max_diff_adjacent/common/include/common.hpp"
11
12 namespace kruglova_a_max_diff_adjacent {
13
14
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 KruglovaAMaxDiffAdjacentMPI::KruglovaAMaxDiffAdjacentMPI(const InType &in) {
15 SetTypeOfTask(GetStaticTypeOfTask());
16
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 GetInput() = in;
17 8 GetOutput() = 0.0F;
18 8 }
19
20 8 bool KruglovaAMaxDiffAdjacentMPI::ValidationImpl() {
21 8 return true;
22 }
23
24 8 bool KruglovaAMaxDiffAdjacentMPI::PreProcessingImpl() {
25 8 return true;
26 }
27
28 6 float KruglovaAMaxDiffAdjacentMPI::LocalMaxDiff(const std::vector<float> &local_vec) {
29
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 float local_max = 0.0F;
30
31
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (local_vec.size() >= 2) {
32
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 5 times.
17 for (size_t i = 1; i < local_vec.size(); ++i) {
33 12 float diff = std::abs(local_vec[i] - local_vec[i - 1]);
34 12 local_max = std::max(diff, local_max);
35 }
36 }
37 6 return local_max;
38 }
39
40 8 bool KruglovaAMaxDiffAdjacentMPI::RunImpl() {
41 8 int rank = 0;
42 8 int size = 0;
43 8 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
44 8 MPI_Comm_size(MPI_COMM_WORLD, &size);
45
46 const auto &vec = GetInput();
47 8 int n = 0;
48
49
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (rank == 0) {
50 4 n = static_cast<int>(vec.size());
51 }
52
53 8 MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
54
55
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (n < 2) {
56
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (rank == 0) {
57 1 GetOutput() = 0.0F;
58 }
59 2 return true;
60 }
61
62 6 std::vector<int> sendcounts(size);
63
1/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
6 std::vector<int> displs(size);
64 6 int base = n / size;
65 6 int rem = n % size;
66
67
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (int proc = 0; proc < size; ++proc) {
68
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 int count = base + (proc < rem ? 1 : 0);
69
70
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 displs[proc] = (proc * base) + std::min(proc, rem);
71
72
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 if (displs[proc] + count < n) {
73 6 sendcounts[proc] = count + 1;
74 } else {
75 6 sendcounts[proc] = count;
76 }
77 }
78
79
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 int local_count = sendcounts[rank];
80
3/6
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
6 std::vector<float> local_vec(local_count);
81
82
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
9 MPI_Scatterv(rank == 0 ? vec.data() : nullptr, sendcounts.data(), displs.data(), MPI_FLOAT, local_vec.data(),
83 local_count, MPI_FLOAT, 0, MPI_COMM_WORLD);
84
85 6 float local_max = 0.0F;
86 6 local_max = LocalMaxDiff(local_vec);
87
88 6 float global_max = 0.0F;
89
90
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MPI_Reduce(&local_max, &global_max, 1, MPI_FLOAT, MPI_MAX, 0, MPI_COMM_WORLD);
91
92
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 MPI_Bcast(&global_max, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
93
94
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 GetOutput() = global_max;
95 return true;
96 }
97
98 8 bool KruglovaAMaxDiffAdjacentMPI::PostProcessingImpl() {
99 8 return true;
100 }
101
102 } // namespace kruglova_a_max_diff_adjacent
103