| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kotelnikova_a_num_sent_in_line/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cctype> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <string> | ||
| 9 | |||
| 10 | #include "kotelnikova_a_num_sent_in_line/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace kotelnikova_a_num_sent_in_line { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | KotelnikovaANumSentInLineMPI::KotelnikovaANumSentInLineMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | 12 | GetOutput() = static_cast<std::size_t>(0); | |
| 18 | 12 | } | |
| 19 | |||
| 20 | 12 | bool KotelnikovaANumSentInLineMPI::ValidationImpl() { | |
| 21 | 12 | return !GetInput().empty(); | |
| 22 | } | ||
| 23 | |||
| 24 | 12 | bool KotelnikovaANumSentInLineMPI::PreProcessingImpl() { | |
| 25 | 12 | return true; | |
| 26 | } | ||
| 27 | |||
| 28 | 12 | bool KotelnikovaANumSentInLineMPI::RunImpl() { | |
| 29 | 12 | int world_size = 0; | |
| 30 | 12 | int world_rank = 0; | |
| 31 | 12 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 32 | 12 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |
| 33 | |||
| 34 | 12 | int total_length = 0; | |
| 35 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (world_rank == 0) { |
| 36 | 6 | total_length = static_cast<int>(GetInput().length()); | |
| 37 | } | ||
| 38 | 12 | MPI_Bcast(&total_length, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 39 | |||
| 40 | std::string text; | ||
| 41 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (world_rank == 0) { |
| 42 | text = GetInput(); | ||
| 43 | } else { | ||
| 44 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | text.resize(static_cast<std::size_t>(total_length)); |
| 45 | } | ||
| 46 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Bcast(text.data(), total_length, MPI_CHAR, 0, MPI_COMM_WORLD); |
| 47 | |||
| 48 | 12 | int chunk_size = total_length / world_size; | |
| 49 | 12 | int remainder = total_length % world_size; | |
| 50 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | int start = (world_rank * chunk_size) + std::min(world_rank, remainder); |
| 51 |
3/4✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
22 | int end = start + chunk_size + (world_rank < remainder ? 1 : 0); |
| 52 | 12 | end = std::min(end, total_length); | |
| 53 | |||
| 54 | 12 | int local_count = CountLocalSentences(text, start, end, total_length); | |
| 55 | |||
| 56 | 12 | int global_count = 0; | |
| 57 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Allreduce(&local_count, &global_count, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 58 | |||
| 59 | 12 | GetOutput() = static_cast<std::size_t>(global_count); | |
| 60 | 12 | return true; | |
| 61 | } | ||
| 62 | |||
| 63 | 12 | int KotelnikovaANumSentInLineMPI::CountLocalSentences(const std::string &text, int start, int end, int total_length) { | |
| 64 | 12 | int local_count = 0; | |
| 65 | bool local_in_sentence = false; | ||
| 66 | |||
| 67 | 12 | local_in_sentence = BorderControl(text, start); | |
| 68 | |||
| 69 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (start < end) { |
| 70 | 12 | ProcessingPart(text, start, end, local_count, local_in_sentence); | |
| 71 | } | ||
| 72 | |||
| 73 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
|
12 | if (local_in_sentence && end == total_length) { |
| 74 | 2 | local_count++; | |
| 75 | } | ||
| 76 | |||
| 77 | 12 | return local_count; | |
| 78 | } | ||
| 79 | |||
| 80 | 12 | bool KotelnikovaANumSentInLineMPI::BorderControl(const std::string &text, int start) { | |
| 81 | 12 | int pos = start - 1; | |
| 82 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 6 times.
|
92 | while (pos >= 0) { |
| 83 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 2 times.
|
86 | char c = text[static_cast<std::size_t>(pos)]; |
| 84 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 2 times.
|
86 | if (c == '.' || c == '!' || c == '?') { |
| 85 | return false; | ||
| 86 | } | ||
| 87 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 4 times.
|
84 | if (std::isalnum(static_cast<unsigned char>(c)) != 0) { |
| 88 | return true; | ||
| 89 | } | ||
| 90 | 80 | pos--; | |
| 91 | } | ||
| 92 | return false; | ||
| 93 | } | ||
| 94 | |||
| 95 | 12 | void KotelnikovaANumSentInLineMPI::ProcessingPart(const std::string &text, int start, int end, int &local_count, | |
| 96 | bool &local_in_sentence) { | ||
| 97 |
2/2✓ Branch 0 taken 163314 times.
✓ Branch 1 taken 12 times.
|
163326 | for (int i = start; i < end; ++i) { |
| 98 |
2/2✓ Branch 0 taken 1334 times.
✓ Branch 1 taken 161980 times.
|
163314 | char c = text[static_cast<std::size_t>(i)]; |
| 99 | |||
| 100 |
2/2✓ Branch 0 taken 1334 times.
✓ Branch 1 taken 161980 times.
|
163314 | if (c == '.' || c == '!' || c == '?') { |
| 101 |
1/2✓ Branch 0 taken 1334 times.
✗ Branch 1 not taken.
|
1334 | if (local_in_sentence) { |
| 102 | 1334 | local_count++; | |
| 103 | 1334 | local_in_sentence = false; | |
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 |
2/2✓ Branch 0 taken 142392 times.
✓ Branch 1 taken 20922 times.
|
163314 | if (std::isalnum(static_cast<unsigned char>(c)) != 0) { |
| 108 | 142392 | local_in_sentence = true; | |
| 109 | } | ||
| 110 | } | ||
| 111 | 12 | } | |
| 112 | |||
| 113 | 12 | bool KotelnikovaANumSentInLineMPI::PostProcessingImpl() { | |
| 114 | 12 | return true; | |
| 115 | } | ||
| 116 | |||
| 117 | } // namespace kotelnikova_a_num_sent_in_line | ||
| 118 |