| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "agafonov_i_sentence_count/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cctype> | ||
| 7 | #include <string> | ||
| 8 | |||
| 9 | #include "agafonov_i_sentence_count/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace agafonov_i_sentence_count { | ||
| 12 | |||
| 13 | ✗ | SentenceCountMPI::SentenceCountMPI(const InType &in) { | |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetInput() = in; | ||
| 16 | ✗ | } | |
| 17 | |||
| 18 | ✗ | bool SentenceCountMPI::ValidationImpl() { | |
| 19 | ✗ | return true; | |
| 20 | } | ||
| 21 | |||
| 22 | ✗ | bool SentenceCountMPI::PreProcessingImpl() { | |
| 23 | ✗ | return true; | |
| 24 | } | ||
| 25 | |||
| 26 | ✗ | int SentenceCountMPI::CalculateStart(int rank, int chunk, int rem) { | |
| 27 | ✗ | return (rank * chunk) + std::min(rank, rem); | |
| 28 | } | ||
| 29 | |||
| 30 | ✗ | int SentenceCountMPI::CalculateEnd(int rank, int chunk, int rem) { | |
| 31 | ✗ | int start = (rank * chunk) + std::min(rank, rem); | |
| 32 | ✗ | return start + chunk + (rank < rem ? 1 : 0); | |
| 33 | } | ||
| 34 | |||
| 35 | ✗ | int SentenceCountMPI::CountSentences(const std::string &text, int start, int end, int total_length) { | |
| 36 | int count = 0; | ||
| 37 | ✗ | bool in_word = (start > 0 && std::isalnum(static_cast<unsigned char>(text[start - 1])) != 0); | |
| 38 | |||
| 39 | ✗ | for (int i = start; i < end; ++i) { | |
| 40 | ✗ | auto c = static_cast<unsigned char>(text[i]); | |
| 41 | ✗ | if (std::isalnum(c) != 0) { | |
| 42 | in_word = true; | ||
| 43 | ✗ | continue; | |
| 44 | } | ||
| 45 | ✗ | if (in_word && (c == '.' || c == '!' || c == '?')) { | |
| 46 | ✗ | if (c == '.' && i + 1 < total_length && text[i + 1] == '.') { | |
| 47 | ✗ | continue; | |
| 48 | } | ||
| 49 | ✗ | count++; | |
| 50 | in_word = false; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | ✗ | return count; | |
| 54 | } | ||
| 55 | |||
| 56 | ✗ | bool SentenceCountMPI::RunImpl() { | |
| 57 | ✗ | int world_size = 0; | |
| 58 | ✗ | int world_rank = 0; | |
| 59 | ✗ | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 60 | ✗ | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |
| 61 | |||
| 62 | ✗ | int total_length = (world_rank == 0) ? static_cast<int>(GetInput().length()) : 0; | |
| 63 | ✗ | MPI_Bcast(&total_length, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 64 | |||
| 65 | ✗ | if (total_length == 0) { | |
| 66 | ✗ | GetOutput() = 0; | |
| 67 | ✗ | return true; | |
| 68 | } | ||
| 69 | |||
| 70 | ✗ | std::string text = (world_rank == 0) ? GetInput() : std::string(total_length, ' '); | |
| 71 | ✗ | MPI_Bcast(const_cast<char *>(text.data()), total_length, MPI_CHAR, 0, MPI_COMM_WORLD); | |
| 72 | |||
| 73 | ✗ | int start = CalculateStart(world_rank, world_size, total_length); | |
| 74 | int end = CalculateEnd(world_rank, world_size, total_length); | ||
| 75 | |||
| 76 | ✗ | int local_count = CountSentences(text, start, end, total_length); | |
| 77 | ✗ | int global_count = 0; | |
| 78 | ✗ | MPI_Allreduce(&local_count, &global_count, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); | |
| 79 | |||
| 80 | ✗ | if (std::isalnum(static_cast<unsigned char>(text[total_length - 1])) != 0) { | |
| 81 | ✗ | global_count++; | |
| 82 | } | ||
| 83 | |||
| 84 | ✗ | GetOutput() = global_count; | |
| 85 | return true; | ||
| 86 | } | ||
| 87 | |||
| 88 | ✗ | bool SentenceCountMPI::PostProcessingImpl() { | |
| 89 | ✗ | return true; | |
| 90 | } | ||
| 91 | |||
| 92 | } // namespace agafonov_i_sentence_count | ||
| 93 |