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