GCC Code Coverage Report


Directory: ./
File: tasks/baldin_a_word_count/mpi/src/ops_mpi.cpp
Date: 2025-12-11 15:42:14
Exec Total Coverage
Lines: 52 52 100.0%
Functions: 7 7 100.0%
Branches: 40 52 76.9%

Line Branch Exec Source
1 #include "baldin_a_word_count/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <cctype>
6 #include <cstddef>
7 #include <string>
8 #include <vector>
9
10 #include "baldin_a_word_count/common/include/common.hpp"
11
12 namespace baldin_a_word_count {
13
14
1/2
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
72 BaldinAWordCountMPI::BaldinAWordCountMPI(const InType &in) {
15 SetTypeOfTask(GetStaticTypeOfTask());
16 GetInput() = in;
17 72 GetOutput() = 0;
18 72 }
19
20 72 bool BaldinAWordCountMPI::ValidationImpl() {
21 72 return true;
22 }
23
24 72 bool BaldinAWordCountMPI::PreProcessingImpl() {
25 72 return true;
26 }
27
28 namespace {
29
30 bool IsWordChar(char c) {
31
4/4
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 468 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 14 times.
703 return ((std::isalnum(static_cast<unsigned char>(c)) != 0) || c == '-' || c == '_');
32 }
33
34 2 size_t CountWords(const std::string &text) {
35 size_t count = 0;
36 bool in_word = false;
37
4/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
4 for (char c : text) {
38
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (IsWordChar(c)) {
39
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!in_word) {
40 in_word = true;
41 1 count++;
42 }
43 } else {
44 in_word = false;
45 }
46 }
47 2 return count;
48 }
49
50 66 size_t CountLocalWords(const std::vector<char> &local_buf, int part) {
51 size_t local_cnt = 0;
52 bool in_word = false;
53
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 66 times.
746 for (int i = 0; i < part; i++) {
54
4/4
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 468 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 189 times.
680 if (IsWordChar(local_buf[i])) {
55
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 373 times.
491 if (!in_word) {
56 in_word = true;
57 118 local_cnt++;
58 }
59 } else {
60 in_word = false;
61 }
62 }
63
64
6/6
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 43 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 6 times.
66 if (in_word && (IsWordChar(local_buf[part]))) {
65 17 local_cnt--;
66 }
67
68 66 return local_cnt;
69 }
70
71 } // namespace
72
73 72 bool BaldinAWordCountMPI::RunImpl() {
74 72 int rank = 0;
75 72 int world_size = 0;
76 72 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
77 72 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
78
79 std::string &input = GetInput();
80
81
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 70 times.
72 if (input.empty()) {
82 2 GetOutput() = 0;
83 2 return true;
84 }
85
86
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 66 times.
70 if (static_cast<size_t>(world_size) > input.size()) {
87
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (rank == 0) {
88 2 GetOutput() = CountWords(input);
89 }
90 4 MPI_Bcast(static_cast<void *>(&GetOutput()), 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD);
91 4 return true;
92 }
93
94 66 size_t rem = input.size() % static_cast<size_t>(world_size);
95 66 input.append(static_cast<size_t>(world_size) - rem + 1, ' ');
96 66 size_t part = input.size() / static_cast<size_t>(world_size);
97
98 66 std::vector<int> send_counts(world_size);
99
1/4
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
66 std::vector<int> displs(world_size);
100
101
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 33 times.
66 if (rank == 0) {
102
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 33 times.
99 for (int i = 0; i < world_size; i++) {
103 66 displs[i] = static_cast<int>(i * part);
104 66 send_counts[i] = static_cast<int>(part + 1);
105 }
106 }
107
108
1/4
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
66 std::vector<char> local_buf(part + 1);
109
110
1/2
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
66 MPI_Scatterv(input.data(), send_counts.data(), displs.data(), MPI_CHAR, local_buf.data(), static_cast<int>(part + 1),
111 MPI_CHAR, 0, MPI_COMM_WORLD);
112
113 66 size_t local_cnt = CountLocalWords(local_buf, static_cast<int>(part));
114
115 66 size_t global_cnt = 0;
116
1/2
✓ Branch 1 taken 66 times.
✗ Branch 2 not taken.
66 MPI_Allreduce(&local_cnt, &global_cnt, 1, MPI_UNSIGNED_LONG, MPI_SUM, MPI_COMM_WORLD);
117
1/2
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
66 GetOutput() = global_cnt;
118
119 return true;
120 }
121
122 72 bool BaldinAWordCountMPI::PostProcessingImpl() {
123 72 return true;
124 }
125
126 } // namespace baldin_a_word_count
127