GCC Code Coverage Report


Directory: ./
File: tasks/makoveeva_s_number_of_sentence/mpi/src/ops_mpi.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 59 62 95.2%
Functions: 6 8 75.0%
Branches: 49 70 70.0%

Line Branch Exec Source
1 #include "makoveeva_s_number_of_sentence/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <cstddef>
6 #include <string>
7 #include <vector>
8
9 #include "makoveeva_s_number_of_sentence/common/include/common.hpp"
10
11 namespace makoveeva_s_number_of_sentence {
12
13
1/2
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
54 SentencesCounterMPI::SentencesCounterMPI(const InType &in) {
14 SetTypeOfTask(GetStaticTypeOfTask());
15 GetInput() = in;
16 54 GetOutput() = 0;
17 54 }
18
19 54 bool SentencesCounterMPI::ValidationImpl() {
20 54 return true;
21 }
22
23 54 bool SentencesCounterMPI::PreProcessingImpl() {
24 54 return true;
25 }
26
27 bool SentencesCounterMPI::IsSentenceEnding(char character) {
28 return character == '.' || character == '!' || character == '?';
29 }
30
31 int SentencesCounterMPI::SkipRepeatedPunctuation(const std::string &text, int current_position) {
32 79 auto position = static_cast<std::size_t>(current_position);
33
8/12
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 69 times.
✓ Branch 5 taken 29 times.
✓ Branch 6 taken 19 times.
✓ Branch 7 taken 50 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
108 while (position < text.length() && IsSentenceEnding(text[position])) {
34 24 position++;
35 }
36 84 return static_cast<int>(position);
37 }
38
39
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 1 times.
52 int SentencesCounterMPI::ProcessTextSegment(const std::string &text_segment, char previous_char) {
40 int sentence_count = 0;
41 std::size_t index = 0;
42 const auto segment_length = text_segment.length();
43
44
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 1 times.
52 if (segment_length > 0) {
45
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 42 times.
51 char first_char = text_segment[0];
46
47 if (IsSentenceEnding(previous_char) && IsSentenceEnding(first_char)) {
48 5 index = static_cast<std::size_t>(SkipRepeatedPunctuation(text_segment, 0));
49 }
50 }
51
52
2/2
✓ Branch 0 taken 435 times.
✓ Branch 1 taken 52 times.
487 while (index < segment_length) {
53
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 356 times.
435 char current_char = text_segment[index];
54
55 if (IsSentenceEnding(current_char)) {
56 79 sentence_count++;
57 158 index = static_cast<std::size_t>(SkipRepeatedPunctuation(text_segment, static_cast<int>(index + 1)));
58 } else {
59 356 index++;
60 }
61 }
62
63 52 return sentence_count;
64 }
65
66 54 bool SentencesCounterMPI::RunImpl() {
67 54 int rank = 0;
68 54 int size = 0;
69 54 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
70 54 MPI_Comm_size(MPI_COMM_WORLD, &size);
71
72 std::string full_text;
73 54 int text_length = 0;
74
75
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
54 if (rank == 0) {
76 full_text = GetInput();
77 27 text_length = static_cast<int>(full_text.length());
78 }
79
80
1/2
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
54 MPI_Bcast(&text_length, 1, MPI_INT, 0, MPI_COMM_WORLD);
81
82
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 52 times.
54 if (text_length == 0) {
83 2 GetOutput() = 0;
84 2 return true;
85 }
86
87 52 int base_chunk_size = text_length / size;
88 52 int remaining_chars = text_length % size;
89
90
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 std::vector<int> chunk_sizes(static_cast<std::size_t>(size));
91
1/4
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
52 std::vector<int> displacements(static_cast<std::size_t>(size));
92
93 int current_displacement = 0;
94
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 52 times.
156 for (int i = 0; i < size; ++i) {
95
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 22 times.
186 chunk_sizes[static_cast<std::size_t>(i)] = base_chunk_size + (i < remaining_chars ? 1 : 0);
96 104 displacements[static_cast<std::size_t>(i)] = current_displacement;
97 104 current_displacement += chunk_sizes[static_cast<std::size_t>(i)];
98 }
99
100
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 int local_chunk_size = chunk_sizes[static_cast<std::size_t>(rank)];
101
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 std::string local_chunk(static_cast<std::size_t>(local_chunk_size), '\0');
102
103
3/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
✓ Branch 3 taken 52 times.
✗ Branch 4 not taken.
78 MPI_Scatterv(rank == 0 ? full_text.data() : nullptr, chunk_sizes.data(), displacements.data(), MPI_CHAR,
104 local_chunk.data(), local_chunk_size, MPI_CHAR, 0, MPI_COMM_WORLD);
105
106
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 std::vector<char> boundary_chars(static_cast<std::size_t>(size));
107
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 if (rank == 0) {
108
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 26 times.
78 for (int i = 0; i < size; ++i) {
109
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 int chunk_start = displacements[static_cast<std::size_t>(i)];
110 52 boundary_chars[static_cast<std::size_t>(i)] =
111
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
52 (chunk_start > 0) ? full_text[static_cast<std::size_t>(chunk_start - 1)] : '\0';
112 }
113 }
114
115
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 char previous_char = '\0';
116
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 MPI_Scatter(boundary_chars.data(), 1, MPI_CHAR, &previous_char, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
117
118 52 int local_sentence_count = ProcessTextSegment(local_chunk, previous_char);
119
120 52 int total_sentences = 0;
121
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 MPI_Reduce(&local_sentence_count, &total_sentences, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
122
123
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 MPI_Bcast(&total_sentences, 1, MPI_INT, 0, MPI_COMM_WORLD);
124
125
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 GetOutput() = total_sentences;
126 return true;
127 }
128
129 54 bool SentencesCounterMPI::PostProcessingImpl() {
130 54 return true;
131 }
132
133 } // namespace makoveeva_s_number_of_sentence
134