| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "papulina_y_count_of_letters/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cctype> | ||
| 7 | #include <string> | ||
| 8 | |||
| 9 | #include "papulina_y_count_of_letters/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace papulina_y_count_of_letters { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | PapulinaYCountOfLettersMPI::PapulinaYCountOfLettersMPI(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetInput() = in; | ||
| 16 | 40 | GetOutput() = 0; | |
| 17 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | MPI_Comm_size(MPI_COMM_WORLD, &procNum_); |
| 18 | 40 | } | |
| 19 | 40 | int PapulinaYCountOfLettersMPI::CountOfLetters(const char *s, const int &n) { | |
| 20 | int k = 0; | ||
| 21 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 3 times.
|
40 | if (n <= 0) { |
| 22 | return 0; | ||
| 23 | } | ||
| 24 |
2/2✓ Branch 0 taken 659 times.
✓ Branch 1 taken 37 times.
|
696 | for (int i = 0; i < n; i++) { |
| 25 | 659 | char c = s[i]; | |
| 26 |
2/2✓ Branch 0 taken 340 times.
✓ Branch 1 taken 319 times.
|
659 | if (isalpha(c) != 0) { |
| 27 | 340 | k++; | |
| 28 | } | ||
| 29 | } | ||
| 30 | return k; | ||
| 31 | } | ||
| 32 | 40 | bool PapulinaYCountOfLettersMPI::ValidationImpl() { | |
| 33 | 40 | return procNum_ > 0; | |
| 34 | } | ||
| 35 | |||
| 36 | 40 | bool PapulinaYCountOfLettersMPI::PreProcessingImpl() { | |
| 37 | 40 | return true; | |
| 38 | } | ||
| 39 | |||
| 40 | 40 | bool PapulinaYCountOfLettersMPI::RunImpl() { | |
| 41 | 40 | int proc_rank = 0; | |
| 42 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | int result = 0; |
| 43 | std::string part_of_string; // части строки, которая будет обрабатываться потоком | ||
| 44 | 40 | unsigned int len = 0; // предполагаемая длина обрабатываемой части | |
| 45 | 40 | unsigned int remainder = 0; // остаток, если длина строки не кратна числу потоков | |
| 46 | 40 | unsigned int true_len = 0; // реальная длина обрабатываемой части | |
| 47 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | MPI_Comm_rank(MPI_COMM_WORLD, &proc_rank); |
| 48 | |||
| 49 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
|
40 | if (proc_rank == 0) { |
| 50 | std::string s = GetInput(); | ||
| 51 | |||
| 52 | 20 | len = s.size() / procNum_; | |
| 53 | 20 | remainder = s.size() % procNum_; | |
| 54 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Bcast(&len, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD); |
| 55 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Bcast(&remainder, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD); |
| 56 | |||
| 57 | unsigned int begin_0 = (0 * len) + std::min(static_cast<unsigned int>(0), remainder); | ||
| 58 | 20 | unsigned int end_0 = ((0 + 1) * len) + std::min(static_cast<unsigned int>(0 + 1), remainder); | |
| 59 | 20 | true_len = end_0 - begin_0; | |
| 60 |
4/6✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 19 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
|
20 | part_of_string = (true_len > 0) ? s.substr(begin_0, true_len) : ""; |
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
|
40 | for (int i = 1; i < procNum_; i++) { |
| 63 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | unsigned int begin = (i * len) + std::min(static_cast<unsigned int>(i), remainder); |
| 64 | 20 | unsigned int end = ((i + 1) * len) + std::min(static_cast<unsigned int>(i + 1), remainder); | |
| 65 | 20 | unsigned int pre_true_len = end - begin; // предварительная длина обрабатываемой части | |
| 66 | |||
| 67 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Send(&pre_true_len, 1, MPI_UNSIGNED, i, 0, MPI_COMM_WORLD); |
| 68 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
|
20 | if (end - begin != 0) { |
| 69 |
2/4✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
|
36 | MPI_Send(s.substr(begin, pre_true_len).data(), static_cast<int>(pre_true_len), MPI_CHAR, i, 1, MPI_COMM_WORLD); |
| 70 | } else { | ||
| 71 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | MPI_Send("", 0, MPI_CHAR, i, 1, MPI_COMM_WORLD); |
| 72 | } | ||
| 73 | } | ||
| 74 | } else { | ||
| 75 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Bcast(&len, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD); |
| 76 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Bcast(&remainder, 1, MPI_UNSIGNED, 0, MPI_COMM_WORLD); |
| 77 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Recv(&true_len, 1, MPI_UNSIGNED, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); |
| 78 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
|
20 | if (true_len > 0) { |
| 79 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | part_of_string.resize(true_len); |
| 80 | } | ||
| 81 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Recv(part_of_string.data(), static_cast<int>(true_len), MPI_CHAR, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE); |
| 82 | } | ||
| 83 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | int local_result = CountOfLetters(part_of_string.data(), static_cast<int>(part_of_string.size())); |
| 84 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | MPI_Reduce(&local_result, &result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); |
| 85 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | MPI_Bcast(&result, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 86 | 40 | GetOutput() = result; | |
| 87 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | MPI_Barrier(MPI_COMM_WORLD); |
| 88 | 40 | return true; | |
| 89 | } | ||
| 90 | |||
| 91 | 40 | bool PapulinaYCountOfLettersMPI::PostProcessingImpl() { | |
| 92 | 40 | return GetOutput() >= 0; | |
| 93 | } | ||
| 94 | |||
| 95 | } // namespace papulina_y_count_of_letters | ||
| 96 |