| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "gusev_d_sentence_count/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "gusev_d_sentence_count/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace gusev_d_sentence_count { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | bool IsTerminator(char c) { | ||
| 16 | return c == '.' || c == '!' || c == '?'; | ||
| 17 | } | ||
| 18 | |||
| 19 | 30 | size_t CountSentencesInChunk(const std::vector<char> &local_chunk, int chunk_size) { | |
| 20 | size_t sentence_count = 0; | ||
| 21 | |||
| 22 |
2/2✓ Branch 0 taken 268 times.
✓ Branch 1 taken 30 times.
|
298 | for (int i = 0; i < chunk_size; ++i) { |
| 23 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 230 times.
|
268 | if (IsTerminator(local_chunk[i])) { |
| 24 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 12 times.
|
38 | if (!IsTerminator(local_chunk[i + 1])) { |
| 25 | 26 | sentence_count++; | |
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
| 29 | 30 | return sentence_count; | |
| 30 | } | ||
| 31 | |||
| 32 | } // namespace | ||
| 33 | |||
| 34 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | GusevDSentenceCountMPI::GusevDSentenceCountMPI(const InType &in) { |
| 35 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 36 | GetInput() = in; | ||
| 37 | 32 | GetOutput() = 0; | |
| 38 | 32 | } | |
| 39 | |||
| 40 | 32 | bool GusevDSentenceCountMPI::ValidationImpl() { | |
| 41 | 32 | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | 32 | bool GusevDSentenceCountMPI::PreProcessingImpl() { | |
| 45 | 32 | return true; | |
| 46 | } | ||
| 47 | |||
| 48 | 32 | bool GusevDSentenceCountMPI::RunImpl() { | |
| 49 | 32 | int rank = 0; | |
| 50 | 32 | int comm_size = 0; | |
| 51 | 32 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 52 | 32 | MPI_Comm_size(MPI_COMM_WORLD, &comm_size); | |
| 53 | |||
| 54 | std::string &text_data = GetInput(); | ||
| 55 | |||
| 56 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 30 times.
|
32 | if (text_data.empty()) { |
| 57 | 2 | GetOutput() = 0; | |
| 58 | 2 | return true; | |
| 59 | } | ||
| 60 | |||
| 61 | 30 | int chunk_size = 0; | |
| 62 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (rank == 0) { |
| 63 | size_t length = text_data.size(); | ||
| 64 | |||
| 65 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 11 times.
|
15 | if (length % comm_size != 0) { |
| 66 | 4 | size_t pad = comm_size - (length % comm_size); | |
| 67 | 4 | text_data.resize(length + pad, ' '); | |
| 68 | length = text_data.size(); | ||
| 69 | } | ||
| 70 | |||
| 71 | 15 | text_data.push_back(' '); | |
| 72 | |||
| 73 | 15 | chunk_size = static_cast<int>(length / comm_size); | |
| 74 | } | ||
| 75 | |||
| 76 | 30 | MPI_Bcast(&chunk_size, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 77 | |||
| 78 | 30 | std::vector<char> local_buffer(chunk_size + 1); | |
| 79 |
1/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
30 | std::vector<int> counts(comm_size, chunk_size + 1); |
| 80 |
1/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
30 | std::vector<int> offsets(comm_size); |
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (rank == 0) { |
| 83 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 15 times.
|
45 | for (int i = 0; i < comm_size; ++i) { |
| 84 | 30 | offsets[i] = i * chunk_size; | |
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | MPI_Scatterv(text_data.data(), counts.data(), offsets.data(), MPI_CHAR, local_buffer.data(), chunk_size + 1, MPI_CHAR, |
| 89 | 0, MPI_COMM_WORLD); | ||
| 90 | |||
| 91 | 30 | size_t local_res = CountSentencesInChunk(local_buffer, chunk_size); | |
| 92 | |||
| 93 | 30 | size_t total_res = 0; | |
| 94 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | MPI_Reduce(&local_res, &total_res, 1, MPI_UNSIGNED_LONG, MPI_SUM, 0, MPI_COMM_WORLD); |
| 95 | |||
| 96 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (rank == 0) { |
| 97 | 15 | GetOutput() = total_res; | |
| 98 | } | ||
| 99 | |||
| 100 | return true; | ||
| 101 | } | ||
| 102 | |||
| 103 | 32 | bool GusevDSentenceCountMPI::PostProcessingImpl() { | |
| 104 | 32 | return true; | |
| 105 | } | ||
| 106 | |||
| 107 | } // namespace gusev_d_sentence_count | ||
| 108 |