GCC Code Coverage Report


Directory: ./
File: tasks/sosnina_a_diff_count/mpi/src/ops_mpi.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 55 55 100.0%
Functions: 5 5 100.0%
Branches: 23 28 82.1%

Line Branch Exec Source
1 #include "sosnina_a_diff_count/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm>
6 #include <array>
7 #include <cstddef>
8 #include <utility>
9
10 #include "sosnina_a_diff_count/common/include/common.hpp"
11
12 namespace sosnina_a_diff_count {
13
14
1/2
✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
128 SosninaADiffCountMPI::SosninaADiffCountMPI(InType in) {
15 SetTypeOfTask(GetStaticTypeOfTask());
16 GetOutput() = 0;
17 128 int rank = 0;
18
1/2
✓ Branch 1 taken 128 times.
✗ Branch 2 not taken.
128 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
19
20
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 64 times.
128 if (rank == 0) {
21 64 str1_ = std::move(in.first);
22 64 str2_ = std::move(in.second);
23 }
24 128 }
25
26 128 bool SosninaADiffCountMPI::ValidationImpl() {
27 128 int mpi_initialized = 0;
28 128 MPI_Initialized(&mpi_initialized);
29
30
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 if (mpi_initialized == 0) {
31 return false;
32 }
33
34 128 int size = 1;
35 128 MPI_Comm_size(MPI_COMM_WORLD, &size);
36
37 128 return size >= 1;
38 }
39
40 128 bool SosninaADiffCountMPI::PreProcessingImpl() {
41 128 int rank = 0;
42 128 int size = 1;
43 128 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
44 128 MPI_Comm_size(MPI_COMM_WORLD, &size);
45
46 128 std::array<int, 2> lengths{};
47
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 64 times.
128 if (rank == 0) {
48 64 lengths[0] = static_cast<int>(str1_.size());
49 64 lengths[1] = static_cast<int>(str2_.size());
50 }
51
52 128 MPI_Bcast(lengths.data(), 2, MPI_INT, 0, MPI_COMM_WORLD);
53
54
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 64 times.
128 if (rank != 0) {
55 64 str1_.resize(lengths[0]);
56 64 str2_.resize(lengths[1]);
57 }
58
59 128 MPI_Bcast(str1_.data(), lengths[0], MPI_CHAR, 0, MPI_COMM_WORLD);
60 128 MPI_Bcast(str2_.data(), lengths[1], MPI_CHAR, 0, MPI_COMM_WORLD);
61
62 128 return true;
63 }
64
65 128 bool SosninaADiffCountMPI::RunImpl() {
66 128 int rank = 0;
67 128 int size = 1;
68 128 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
69 128 MPI_Comm_size(MPI_COMM_WORLD, &size);
70
71 128 std::size_t str1_len = str1_.size();
72
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 112 times.
128 std::size_t str2_len = str2_.size();
73 std::size_t total_len = std::max(str1_len, str2_len);
74
75
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 112 times.
128 if (total_len == 0) {
76 16 diff_counter_ = 0;
77 16 return true;
78 }
79
80 112 std::size_t chunk_size = total_len / size;
81 112 std::size_t remainder = total_len % size;
82
83
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 std::size_t start = (rank * chunk_size) + std::min(static_cast<std::size_t>(rank), remainder);
84
1/2
✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
112 std::size_t end = start + chunk_size;
85 if (std::cmp_less(rank, remainder)) {
86 32 end += 1;
87 }
88
89 // подсчёт несовпадений на своём отрезке
90 112 int local_diff_count = 0;
91
2/2
✓ Branch 0 taken 360 times.
✓ Branch 1 taken 112 times.
472 for (std::size_t i = start; i < end && i < total_len; i++) {
92
6/6
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 126 times.
✓ Branch 2 taken 220 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 94 times.
✓ Branch 5 taken 126 times.
360 if (i >= str1_len || i >= str2_len || str1_[i] != str2_[i]) {
93 234 local_diff_count++;
94 }
95 }
96
97 112 MPI_Reduce(&local_diff_count, &diff_counter_, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
98 112 MPI_Bcast(&diff_counter_, 1, MPI_INT, 0, MPI_COMM_WORLD);
99 112 GetOutput() = diff_counter_;
100
101 112 return true;
102 }
103
104 128 bool SosninaADiffCountMPI::PostProcessingImpl() {
105 128 return true;
106 }
107
108 } // namespace sosnina_a_diff_count
109