| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "../include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <climits> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "../../common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace shekhirev_v_char_freq_mpi { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | CharFreqMPI::CharFreqMPI(const shekhirev_v_char_freq_seq::InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetInput() = in; | ||
| 17 | 14 | GetOutput() = 0; | |
| 18 | 14 | } | |
| 19 | |||
| 20 | 14 | bool CharFreqMPI::ValidationImpl() { | |
| 21 | 14 | int rank = 0; | |
| 22 | 14 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 23 | |||
| 24 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank == 0) { |
| 25 | 7 | return GetInput().str.size() <= static_cast<size_t>(INT_MAX); | |
| 26 | } | ||
| 27 | return true; | ||
| 28 | } | ||
| 29 | |||
| 30 | 14 | bool CharFreqMPI::PreProcessingImpl() { | |
| 31 | 14 | return true; | |
| 32 | } | ||
| 33 | |||
| 34 | 14 | bool CharFreqMPI::RunImpl() { | |
| 35 | 14 | int rank = 0; | |
| 36 | 14 | int size = 0; | |
| 37 | 14 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 38 | 14 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 39 | |||
| 40 | 14 | int total_len = 0; | |
| 41 | 14 | char target = 0; | |
| 42 | |||
| 43 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank == 0) { |
| 44 | 7 | total_len = static_cast<int>(GetInput().str.size()); | |
| 45 | 7 | target = GetInput().target; | |
| 46 | } | ||
| 47 | |||
| 48 | 14 | MPI_Bcast(&total_len, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 49 | 14 | MPI_Bcast(&target, 1, MPI_CHAR, 0, MPI_COMM_WORLD); | |
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
|
14 | if (total_len == 0) { |
| 52 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (rank == 0) { |
| 53 | 1 | GetOutput() = 0; | |
| 54 | } | ||
| 55 | 2 | return true; | |
| 56 | } | ||
| 57 | |||
| 58 | 12 | const int delta = total_len / size; | |
| 59 | 12 | const int remainder = total_len % size; | |
| 60 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5 times.
|
12 | int my_count = delta + (rank < remainder ? 1 : 0); |
| 61 | |||
| 62 | 12 | std::vector<char> local_data(my_count); | |
| 63 | |||
| 64 |
1/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
12 | std::vector<int> send_counts(size); |
| 65 |
1/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
12 | std::vector<int> displs(size); |
| 66 | |||
| 67 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 68 | int current_displ = 0; | ||
| 69 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (int i = 0; i < size; ++i) { |
| 70 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5 times.
|
19 | send_counts[i] = delta + (i < remainder ? 1 : 0); |
| 71 | 12 | displs[i] = current_displ; | |
| 72 | 12 | current_displ += send_counts[i]; | |
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | const char *sendbuf = (rank == 0) ? GetInput().str.data() : nullptr; |
| 77 | |||
| 78 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Scatterv(sendbuf, send_counts.data(), displs.data(), MPI_CHAR, local_data.data(), my_count, MPI_CHAR, 0, |
| 79 | MPI_COMM_WORLD); | ||
| 80 | |||
| 81 | 12 | int local_res = static_cast<int>(std::count(local_data.begin(), local_data.end(), target)); | |
| 82 | |||
| 83 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 84 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Reduce(MPI_IN_PLACE, &local_res, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); |
| 85 | 6 | GetOutput() = local_res; | |
| 86 | } else { | ||
| 87 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Reduce(&local_res, nullptr, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); |
| 88 | } | ||
| 89 | |||
| 90 | return true; | ||
| 91 | } | ||
| 92 | |||
| 93 | 14 | bool CharFreqMPI::PostProcessingImpl() { | |
| 94 | 14 | return true; | |
| 95 | } | ||
| 96 | |||
| 97 | } // namespace shekhirev_v_char_freq_mpi | ||
| 98 |