| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "sakharov_a_num_of_letters/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cctype> | ||
| 7 | #include <string> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "sakharov_a_num_of_letters/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace sakharov_a_num_of_letters { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | SakharovANumberOfLettersMPI::SakharovANumberOfLettersMPI(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 | GetInput() = in; | ||
| 18 | 12 | GetOutput() = 0; | |
| 19 | 12 | } | |
| 20 | |||
| 21 | 12 | bool SakharovANumberOfLettersMPI::ValidationImpl() { | |
| 22 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | return std::cmp_equal(std::get<1>(GetInput()).size(), std::get<0>(GetInput())); |
| 23 | } | ||
| 24 | |||
| 25 | 12 | bool SakharovANumberOfLettersMPI::PreProcessingImpl() { | |
| 26 | 12 | return true; | |
| 27 | } | ||
| 28 | |||
| 29 | 12 | bool SakharovANumberOfLettersMPI::RunImpl() { | |
| 30 | 12 | int world_size = 0; | |
| 31 | 12 | int world_rank = 0; | |
| 32 | |||
| 33 | 12 | MPI_Comm_size(MPI_COMM_WORLD, &world_size); | |
| 34 | 12 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | |
| 35 | |||
| 36 | 12 | int size_of_string = 0; | |
| 37 | const char *send_buffer = nullptr; | ||
| 38 | |||
| 39 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (world_rank == 0) { |
| 40 | 6 | size_of_string = std::get<0>(GetInput()); | |
| 41 | const std::string &string_of_letters = std::get<1>(GetInput()); | ||
| 42 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
6 | if (size_of_string > 0) { |
| 43 | send_buffer = string_of_letters.data(); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | 12 | MPI_Bcast(&size_of_string, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 48 | |||
| 49 | 12 | int base_chunk = size_of_string / world_size; | |
| 50 | 12 | int remainder = size_of_string % world_size; | |
| 51 | |||
| 52 | 12 | std::vector<int> send_counts(world_size); | |
| 53 |
1/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
12 | std::vector<int> displs(world_size); |
| 54 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
|
36 | for (int rank = 0; rank < world_size; rank++) { |
| 55 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 2 times.
|
46 | send_counts[rank] = base_chunk + (rank < remainder ? 1 : 0); |
| 56 | 24 | displs[rank] = (rank * base_chunk) + std::min(rank, remainder); | |
| 57 | } | ||
| 58 | |||
| 59 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | int local_count = send_counts[world_rank]; |
| 60 | std::string local_string; | ||
| 61 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | local_string.resize(local_count); |
| 62 | |||
| 63 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | char *local_buffer = local_count > 0 ? local_string.data() : nullptr; |
| 64 | |||
| 65 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Scatterv(send_buffer, send_counts.data(), displs.data(), MPI_CHAR, local_buffer, local_count, MPI_CHAR, 0, |
| 66 | MPI_COMM_WORLD); | ||
| 67 | |||
| 68 | 12 | int local_letters = 0; | |
| 69 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 12 times.
|
41 | for (int i = 0; i < local_count; i++) { |
| 70 | 29 | char c = local_buffer[i]; | |
| 71 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 14 times.
|
29 | if (std::isalpha(static_cast<unsigned char>(c)) != 0) { |
| 72 | 15 | local_letters++; | |
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | 12 | int global_letters = 0; | |
| 77 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Allreduce(&local_letters, &global_letters, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 78 | |||
| 79 | 12 | GetOutput() = global_letters; | |
| 80 | |||
| 81 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Barrier(MPI_COMM_WORLD); |
| 82 | 12 | return true; | |
| 83 | } | ||
| 84 | |||
| 85 | 12 | bool SakharovANumberOfLettersMPI::PostProcessingImpl() { | |
| 86 | 12 | return true; | |
| 87 | } | ||
| 88 | |||
| 89 | } // namespace sakharov_a_num_of_letters | ||
| 90 |