GCC Code Coverage Report


Directory: ./
File: tasks/posternak_a_count_different_char_in_two_lines/mpi/src/ops_mpi.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 56 56 100.0%
Functions: 5 5 100.0%
Branches: 33 52 63.5%

Line Branch Exec Source
1 #include "posternak_a_count_different_char_in_two_lines/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm>
6 #include <cmath>
7 #include <string>
8 #include <utility>
9 #include <vector>
10
11 #include "posternak_a_count_different_char_in_two_lines/common/include/common.hpp"
12
13 namespace posternak_a_count_different_char_in_two_lines {
14
15
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 PosternakACountDifferentCharInTwoLinesMPI::PosternakACountDifferentCharInTwoLinesMPI(const InType &in) {
16 SetTypeOfTask(GetStaticTypeOfTask());
17 GetInput() = in;
18 40 GetOutput() = 0;
19 40 }
20
21 40 bool PosternakACountDifferentCharInTwoLinesMPI::ValidationImpl() {
22 std::pair<std::string, std::string> &lines = GetInput();
23 std::string s1 = lines.first;
24 std::string s2 = lines.second;
25
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
80 return !s1.empty() && !s2.empty();
26 }
27
28 40 bool PosternakACountDifferentCharInTwoLinesMPI::PreProcessingImpl() {
29 40 return true;
30 }
31
32 40 bool PosternakACountDifferentCharInTwoLinesMPI::RunImpl() {
33 40 int rank = 0;
34 40 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
35
36 40 int size = 0;
37 40 MPI_Comm_size(MPI_COMM_WORLD, &size);
38
39 std::string s1;
40 std::string s2;
41
42 40 int s1_len = 0;
43 40 int s2_len = 0;
44
45
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
40 if (rank == 0) {
46 std::pair<std::string, std::string> &lines = GetInput();
47
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 s1 = lines.first;
48
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 s2 = lines.second;
49 20 s1_len = static_cast<int>(s1.length());
50 20 s2_len = static_cast<int>(s2.length());
51 }
52
53
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 MPI_Bcast(&s1_len, 1, MPI_INT, 0, MPI_COMM_WORLD);
54
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 MPI_Bcast(&s2_len, 1, MPI_INT, 0, MPI_COMM_WORLD);
55
56 40 std::vector<std::string> s1_proc_parts;
57 40 std::vector<std::string> s2_proc_parts;
58
59
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
40 if (rank == 0) {
60 int min_len = std::min(s1_len, s2_len);
61
62 20 int local_len = min_len / size;
63 20 int remainder = min_len % size;
64
65 int start = 0;
66
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 20 times.
60 for (int i = 0; i < size; i++) {
67 int part_len = local_len;
68
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
40 if (i == size - 1) {
69 20 part_len += remainder;
70 }
71
72
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 s1_proc_parts.push_back(s1.substr(start, part_len));
73
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 s2_proc_parts.push_back(s2.substr(start, part_len));
74 40 start += part_len;
75 }
76
77
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
40 for (int proc = 1; proc < size; proc++) {
78
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 int part_len = static_cast<int>(s1_proc_parts[proc].length());
79
80
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 MPI_Send(&part_len, 1, MPI_INT, proc, 0, MPI_COMM_WORLD);
81
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 MPI_Send(s1_proc_parts[proc].c_str(), part_len, MPI_CHAR, proc, 1, MPI_COMM_WORLD);
82
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 MPI_Send(s2_proc_parts[proc].c_str(), part_len, MPI_CHAR, proc, 2, MPI_COMM_WORLD);
83 }
84
85 s1 = s1_proc_parts[0];
86 s2 = s2_proc_parts[0];
87 } else {
88 20 int part_len = 0;
89
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 MPI_Recv(&part_len, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
90
91
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 s1.resize(part_len);
92
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 s2.resize(part_len);
93
94
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 MPI_Recv(s1.data(), part_len, MPI_CHAR, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
95
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 MPI_Recv(s2.data(), part_len, MPI_CHAR, 0, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
96 }
97
98 40 int process_count = 0;
99 40 int part_len = static_cast<int>(s1.length());
100
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 40 times.
188 for (int i = 0; i < part_len; i++) {
101
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 118 times.
148 if (s1[i] != s2[i]) {
102 30 process_count++;
103 }
104 }
105
106 40 int count = 0;
107
1/2
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
40 MPI_Allreduce(&process_count, &count, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
108
109 40 count += std::abs(s1_len - s2_len);
110 40 GetOutput() = count;
111
112 40 return true;
113 40 }
114
115 40 bool PosternakACountDifferentCharInTwoLinesMPI::PostProcessingImpl() {
116 40 return true;
117 }
118
119 } // namespace posternak_a_count_different_char_in_two_lines
120