| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "shilin_n_counting_number_sentences_in_line/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <string> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "shilin_n_counting_number_sentences_in_line/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace shilin_n_counting_number_sentences_in_line { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | ShilinNCountingNumberSentencesInLineMPI::ShilinNCountingNumberSentencesInLineMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | 48 | int rank = 0; | |
| 17 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 18 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 | if (rank == 0) { |
| 19 | GetInput() = in; | ||
| 20 | } | ||
| 21 | 48 | GetOutput() = 0; | |
| 22 | 48 | } | |
| 23 | |||
| 24 | 48 | bool ShilinNCountingNumberSentencesInLineMPI::ValidationImpl() { | |
| 25 | 48 | int rank = 0; | |
| 26 | 48 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 27 | |||
| 28 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 | if (rank == 0) { |
| 29 | 24 | return GetOutput() == 0; | |
| 30 | } | ||
| 31 | return true; | ||
| 32 | } | ||
| 33 | |||
| 34 | 48 | bool ShilinNCountingNumberSentencesInLineMPI::PreProcessingImpl() { | |
| 35 | 48 | int rank = 0; | |
| 36 | 48 | int size = 0; | |
| 37 | 48 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 38 | 48 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 39 | |||
| 40 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 | if (rank == 0) { |
| 41 | 24 | GetOutput() = 0; | |
| 42 | } | ||
| 43 | 48 | return true; | |
| 44 | } | ||
| 45 | |||
| 46 | namespace { | ||
| 47 | |||
| 48 | bool IsPunctuation(char c) { | ||
| 49 | return c == '.' || c == '!' || c == '?'; | ||
| 50 | } | ||
| 51 | } // namespace | ||
| 52 | |||
| 53 | ✗ | int ShilinNCountingNumberSentencesInLineMPI::SkipPunctuationSequence(const std::string &input_str, int start_pos, | |
| 54 | int end_pos) { | ||
| 55 | int pos = start_pos; | ||
| 56 |
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 45 times.
✓ Branch 5 taken 25 times.
✓ Branch 6 taken 19 times.
✓ Branch 7 taken 26 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
80 | while (pos < end_pos && IsPunctuation(input_str[static_cast<size_t>(pos)])) { |
| 57 | 24 | ++pos; | |
| 58 | } | ||
| 59 | ✗ | return pos; | |
| 60 | } | ||
| 61 | |||
| 62 | 46 | int ShilinNCountingNumberSentencesInLineMPI::CountSentencesFromPosition(const std::string &input_str, int start_pos, | |
| 63 | int end_pos) { | ||
| 64 | int count = 0; | ||
| 65 |
2/2✓ Branch 0 taken 379 times.
✓ Branch 1 taken 46 times.
|
425 | for (int i = start_pos; i < end_pos; ++i) { |
| 66 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 328 times.
|
379 | if (IsPunctuation(input_str[static_cast<size_t>(i)])) { |
| 67 | 51 | count++; | |
| 68 | 102 | i = SkipPunctuationSequence(input_str, i + 1, end_pos) - 1; | |
| 69 | } | ||
| 70 | } | ||
| 71 | 46 | return count; | |
| 72 | } | ||
| 73 | |||
| 74 | 46 | int ShilinNCountingNumberSentencesInLineMPI::CountSentencesInChunk(const std::string &input_str, int start_pos, | |
| 75 | int end_pos, char left_boundary_char) { | ||
| 76 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | if (start_pos < end_pos && IsPunctuation(left_boundary_char) && |
| 77 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
|
7 | IsPunctuation(input_str[static_cast<size_t>(start_pos)])) { |
| 78 | const int new_start_pos = SkipPunctuationSequence(input_str, start_pos, end_pos); | ||
| 79 | 5 | return CountSentencesFromPosition(input_str, new_start_pos, end_pos); | |
| 80 | } | ||
| 81 | |||
| 82 | 41 | return CountSentencesFromPosition(input_str, start_pos, end_pos); | |
| 83 | } | ||
| 84 | |||
| 85 | 48 | bool ShilinNCountingNumberSentencesInLineMPI::RunImpl() { | |
| 86 | 48 | int rank = 0; | |
| 87 | 48 | int size = 0; | |
| 88 | 48 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 89 | 48 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 90 | |||
| 91 | std::string input_str; | ||
| 92 | 48 | int input_length = 0; | |
| 93 | |||
| 94 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 | if (rank == 0) { |
| 95 | input_str = GetInput(); | ||
| 96 | 24 | input_length = static_cast<int>(input_str.length()); | |
| 97 | } | ||
| 98 | |||
| 99 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | MPI_Bcast(&input_length, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 100 | |||
| 101 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 46 times.
|
48 | if (input_length == 0) { |
| 102 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (rank == 0) { |
| 103 | 1 | GetOutput() = 0; | |
| 104 | } | ||
| 105 | 2 | return true; | |
| 106 | } | ||
| 107 | |||
| 108 | 46 | int chunk_size = input_length / size; | |
| 109 | 46 | int remainder = input_length % size; | |
| 110 | |||
| 111 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | std::vector<int> sendcounts(size); |
| 112 |
1/4✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
46 | std::vector<int> displacements(size); |
| 113 | |||
| 114 |
2/2✓ Branch 0 taken 92 times.
✓ Branch 1 taken 46 times.
|
138 | for (int i = 0; i < size; ++i) { |
| 115 |
2/2✓ Branch 0 taken 74 times.
✓ Branch 1 taken 18 times.
|
166 | sendcounts[i] = chunk_size + (i < remainder ? 1 : 0); |
| 116 | 92 | displacements[i] = (i * chunk_size) + std::min(i, remainder); | |
| 117 | } | ||
| 118 | |||
| 119 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | int local_chunk_size = sendcounts[rank]; |
| 120 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | std::string local_chunk(local_chunk_size, '\0'); |
| 121 | |||
| 122 |
3/4✓ Branch 0 taken 23 times.
✓ Branch 1 taken 23 times.
✓ Branch 3 taken 46 times.
✗ Branch 4 not taken.
|
69 | MPI_Scatterv(rank == 0 ? input_str.data() : nullptr, sendcounts.data(), displacements.data(), MPI_CHAR, |
| 123 | local_chunk.data(), local_chunk_size, MPI_CHAR, 0, MPI_COMM_WORLD); | ||
| 124 | |||
| 125 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | std::vector<char> left_boundary_chars(size); |
| 126 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 23 times.
|
46 | if (rank == 0) { |
| 127 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 23 times.
|
69 | for (int i = 0; i < size; ++i) { |
| 128 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 23 times.
|
46 | int start_pos = (i * chunk_size) + std::min(i, remainder); |
| 129 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 23 times.
|
46 | left_boundary_chars[i] = (start_pos > 0) ? input_str[static_cast<size_t>(start_pos - 1)] : '\0'; |
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | char left_boundary_char = '\0'; |
| 134 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | MPI_Scatter(left_boundary_chars.data(), 1, MPI_CHAR, &left_boundary_char, 1, MPI_CHAR, 0, MPI_COMM_WORLD); |
| 135 | |||
| 136 | 46 | int local_count = CountSentencesInChunk(local_chunk, 0, local_chunk_size, left_boundary_char); | |
| 137 | |||
| 138 | 46 | int global_count = 0; | |
| 139 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | MPI_Reduce(&local_count, &global_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); |
| 140 |
1/2✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
|
46 | MPI_Bcast(&global_count, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 141 | |||
| 142 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46 | GetOutput() = global_count; |
| 143 | return true; | ||
| 144 | } | ||
| 145 | |||
| 146 | 48 | bool ShilinNCountingNumberSentencesInLineMPI::PostProcessingImpl() { | |
| 147 | 48 | return true; | |
| 148 | } | ||
| 149 | |||
| 150 | } // namespace shilin_n_counting_number_sentences_in_line | ||
| 151 |