GCC Code Coverage Report


Directory: ./
File: tasks/pankov_a_string_word_count/mpi/src/ops_mpi.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 53 53 100.0%
Functions: 6 6 100.0%
Branches: 41 50 82.0%

Line Branch Exec Source
1 #include "pankov_a_string_word_count/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <algorithm>
6 #include <cctype>
7 #include <cstddef>
8 #include <string>
9 #include <utility>
10
11 #include "pankov_a_string_word_count/common/include/common.hpp"
12
13 namespace pankov_a_string_word_count {
14
15
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 PankovAStringWordCountMPI::PankovAStringWordCountMPI(const InType &in) {
16 SetTypeOfTask(GetStaticTypeOfTask());
17 GetInput() = in;
18 16 GetOutput() = 0;
19 16 }
20
21 16 bool PankovAStringWordCountMPI::ValidationImpl() {
22 16 return GetOutput() == 0;
23 }
24
25 16 bool PankovAStringWordCountMPI::PreProcessingImpl() {
26 16 GetOutput() = 0;
27 16 return true;
28 }
29
30 namespace {
31
32 12 int CountWordsLocal(const std::string &s, std::size_t start, std::size_t end) {
33 int count = 0;
34 bool in_word = false;
35
36
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 12 times.
152 for (std::size_t i = start; i < end; ++i) {
37 140 auto uc = static_cast<unsigned char>(s[i]);
38
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 49 times.
140 if (std::isspace(uc) == 0) {
39
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 49 times.
91 if (!in_word) {
40 in_word = true;
41 42 ++count;
42 }
43 } else {
44 in_word = false;
45 }
46 }
47
48 12 return count;
49 }
50
51 } // namespace
52
53 16 bool PankovAStringWordCountMPI::RunImpl() {
54 16 int rank = 0;
55 16 int size = 1;
56 16 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
57 16 MPI_Comm_size(MPI_COMM_WORLD, &size);
58
59 std::string s;
60 16 int n = 0;
61
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (rank == 0) {
62 s = GetInput();
63 8 n = static_cast<int>(s.size());
64 }
65
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
66
67
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
16 if (n == 0) {
68 2 GetOutput() = 0;
69
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 MPI_Barrier(MPI_COMM_WORLD);
70 return true;
71 }
72
73
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 if (rank != 0) {
74
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 s.resize(static_cast<std::size_t>(n));
75 }
76
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 MPI_Bcast(s.data(), n, MPI_CHAR, 0, MPI_COMM_WORLD);
77
78 14 const auto str_size = static_cast<std::size_t>(n);
79 14 std::size_t base = str_size / static_cast<std::size_t>(size);
80 14 std::size_t rem = str_size % static_cast<std::size_t>(size);
81
82
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
14 std::size_t start = (rank * base) + static_cast<std::size_t>(std::min(rank, static_cast<int>(rem)));
83
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
22 std::size_t end = start + base + (std::cmp_less(rank, static_cast<int>(rem)) ? 1 : 0);
84
85 14 start = std::min(start, str_size);
86 14 end = std::min(end, str_size);
87
88
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
14 if (rank != 0 && start < str_size) {
89
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
7 if ((std::isspace(static_cast<unsigned char>(s[start])) == 0) &&
90
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 (std::isspace(static_cast<unsigned char>(s[start - 1])) == 0)) {
91
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 2 times.
24 while (start < end && (std::isspace(static_cast<unsigned char>(s[start])) == 0)) {
92 20 ++start;
93 }
94 }
95 }
96
97 14 start = std::min(start, end);
98
99 14 int local_count = 0;
100
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 if (start < end) {
101 12 local_count = CountWordsLocal(s, start, end);
102 }
103
104 14 int global_count = 0;
105
106
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 MPI_Reduce(&local_count, &global_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
107
108
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 MPI_Bcast(&global_count, 1, MPI_INT, 0, MPI_COMM_WORLD);
109
110 14 GetOutput() = global_count;
111
112
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 MPI_Barrier(MPI_COMM_WORLD);
113 return true;
114 }
115
116 16 bool PankovAStringWordCountMPI::PostProcessingImpl() {
117 16 return GetOutput() >= 0;
118 }
119
120 } // namespace pankov_a_string_word_count
121