| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "volkov_a_count_word_line/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "volkov_a_count_word_line/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace volkov_a_count_word_line { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | bool IsTokenChar(char c) { | ||
| 16 | 511 | const bool is_alpha = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); | |
| 17 | 511 | const bool is_digit = (c >= '0' && c <= '9'); | |
| 18 | 511 | const bool is_special = (c == '-' || c == '_'); | |
| 19 |
2/2✓ Branch 0 taken 172 times.
✓ Branch 1 taken 316 times.
|
488 | return is_alpha || is_digit || is_special; |
| 20 | } | ||
| 21 | |||
| 22 | 56 | int CountWordsInChunk(const std::vector<char> &data, int valid_size) { | |
| 23 | int count = 0; | ||
| 24 | bool in_word = false; | ||
| 25 | |||
| 26 |
2/2✓ Branch 0 taken 488 times.
✓ Branch 1 taken 56 times.
|
544 | for (int i = 0; i < valid_size; ++i) { |
| 27 |
4/4✓ Branch 0 taken 172 times.
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 156 times.
|
488 | if (IsTokenChar(data[i])) { |
| 28 |
2/2✓ Branch 0 taken 91 times.
✓ Branch 1 taken 241 times.
|
332 | if (!in_word) { |
| 29 | in_word = true; | ||
| 30 | 91 | count++; | |
| 31 | } | ||
| 32 | } else { | ||
| 33 | in_word = false; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 |
6/8✓ Branch 0 taken 22 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 18 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
|
56 | if (in_word && static_cast<size_t>(valid_size) < data.size() && IsTokenChar(data[valid_size])) { |
| 38 | 18 | count--; | |
| 39 | } | ||
| 40 | |||
| 41 | 56 | return count; | |
| 42 | } | ||
| 43 | |||
| 44 | 1 | int CountWordsSeq(const std::string &str) { | |
| 45 | int count = 0; | ||
| 46 | bool in_word = false; | ||
| 47 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
2 | for (char c : str) { |
| 48 | ✗ | if (IsTokenChar(c)) { | |
| 49 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!in_word) { |
| 50 | in_word = true; | ||
| 51 | 1 | count++; | |
| 52 | } | ||
| 53 | } else { | ||
| 54 | in_word = false; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | 1 | return count; | |
| 58 | } | ||
| 59 | |||
| 60 | } // namespace | ||
| 61 | |||
| 62 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | VolkovACountWordLineMPI::VolkovACountWordLineMPI(const InType &in) { |
| 63 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 64 | GetInput() = in; | ||
| 65 | 60 | GetOutput() = 0; | |
| 66 | 60 | } | |
| 67 | |||
| 68 | 60 | bool VolkovACountWordLineMPI::ValidationImpl() { | |
| 69 | 60 | return true; | |
| 70 | } | ||
| 71 | |||
| 72 | 60 | bool VolkovACountWordLineMPI::PreProcessingImpl() { | |
| 73 | 60 | return true; | |
| 74 | } | ||
| 75 | |||
| 76 | 60 | bool VolkovACountWordLineMPI::RunImpl() { | |
| 77 | 60 | int rank = 0; | |
| 78 | 60 | int world_size = 0; | |
| 79 | 60 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 80 | 60 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 81 | |||
| 82 | std::string &input_str = GetInput(); | ||
| 83 | |||
| 84 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 58 times.
|
60 | if (input_str.empty()) { |
| 85 | 2 | GetOutput() = 0; | |
| 86 | 2 | return true; | |
| 87 | } | ||
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 56 times.
|
58 | if (static_cast<size_t>(world_size) > input_str.size()) { |
| 90 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (rank == 0) { |
| 91 | 1 | GetOutput() = CountWordsSeq(input_str); | |
| 92 | } | ||
| 93 | 2 | MPI_Bcast(&GetOutput(), 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 94 | 2 | return true; | |
| 95 | } | ||
| 96 | |||
| 97 | 56 | size_t remainder = input_str.size() % static_cast<size_t>(world_size); | |
| 98 | 56 | size_t padding = (static_cast<size_t>(world_size) - remainder) % static_cast<size_t>(world_size); | |
| 99 | 56 | input_str.append(padding + static_cast<size_t>(world_size), ' '); | |
| 100 | |||
| 101 | 56 | int chunk_size = static_cast<int>(input_str.size() / static_cast<size_t>(world_size)); | |
| 102 | |||
| 103 | 56 | std::vector<int> send_counts(world_size); | |
| 104 |
1/4✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
56 | std::vector<int> displs(world_size); |
| 105 | |||
| 106 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
|
56 | if (rank == 0) { |
| 107 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 28 times.
|
84 | for (int i = 0; i < world_size; ++i) { |
| 108 | 56 | send_counts[i] = chunk_size + 1; | |
| 109 | 56 | displs[i] = i * chunk_size; | |
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 |
2/6✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 56 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
56 | std::vector<char> local_data(chunk_size + 1); |
| 114 | |||
| 115 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | MPI_Scatterv(input_str.data(), send_counts.data(), displs.data(), MPI_CHAR, local_data.data(), chunk_size + 1, |
| 116 | MPI_CHAR, 0, MPI_COMM_WORLD); | ||
| 117 | |||
| 118 | 56 | int local_words = CountWordsInChunk(local_data, chunk_size); | |
| 119 | 56 | int total_words = 0; | |
| 120 | |||
| 121 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | MPI_Allreduce(&local_words, &total_words, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 122 | |||
| 123 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | GetOutput() = total_words; |
| 124 | |||
| 125 | return true; | ||
| 126 | } | ||
| 127 | |||
| 128 | 60 | bool VolkovACountWordLineMPI::PostProcessingImpl() { | |
| 129 | 60 | return true; | |
| 130 | } | ||
| 131 | |||
| 132 | } // namespace volkov_a_count_word_line | ||
| 133 |