| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "perepelkin_i_string_diff_char_count/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <functional> | ||
| 8 | #include <numeric> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "perepelkin_i_string_diff_char_count/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace perepelkin_i_string_diff_char_count { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | PerepelkinIStringDiffCharCountMPI::PerepelkinIStringDiffCharCountMPI(const InType &in) { |
| 16 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | MPI_Comm_rank(MPI_COMM_WORLD, &proc_rank_); |
| 17 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | MPI_Comm_size(MPI_COMM_WORLD, &proc_num_); |
| 18 | |||
| 19 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 20 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | if (proc_rank_ == 0) { |
| 21 | GetInput() = in; | ||
| 22 | } | ||
| 23 | 24 | GetOutput() = 0; | |
| 24 | 24 | } | |
| 25 | |||
| 26 | 24 | bool PerepelkinIStringDiffCharCountMPI::ValidationImpl() { | |
| 27 | 24 | return (GetOutput() == 0); | |
| 28 | } | ||
| 29 | |||
| 30 | 24 | bool PerepelkinIStringDiffCharCountMPI::PreProcessingImpl() { | |
| 31 | 24 | return true; | |
| 32 | } | ||
| 33 | |||
| 34 | 24 | bool PerepelkinIStringDiffCharCountMPI::RunImpl() { | |
| 35 | 24 | size_t min_len = 0; | |
| 36 | 24 | size_t max_len = 0; | |
| 37 | |||
| 38 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | if (proc_rank_ == 0) { |
| 39 | const auto &[s1, s2] = GetInput(); | ||
| 40 | 12 | min_len = std::min(s1.size(), s2.size()); | |
| 41 | 12 | max_len = std::max(s1.size(), s2.size()); | |
| 42 | } | ||
| 43 | |||
| 44 | 24 | MPI_Bcast(&min_len, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 45 | 24 | MPI_Bcast(&max_len, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 46 | |||
| 47 | 24 | std::vector<char> local_s1; | |
| 48 | 24 | std::vector<char> local_s2; | |
| 49 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | DistributeData(min_len, local_s1, local_s2); |
| 50 | |||
| 51 | 24 | int local_diff = std::transform_reduce(local_s1.begin(), local_s1.end(), local_s2.begin(), 0, std::plus<>(), | |
| 52 | 24 | std::not_equal_to<>()); | |
| 53 | |||
| 54 | 24 | int global_diff = 0; | |
| 55 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | MPI_Allreduce(&local_diff, &global_diff, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 56 | |||
| 57 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
|
24 | GetOutput() = global_diff + static_cast<int>(max_len - min_len); |
| 58 | 24 | return true; | |
| 59 | } | ||
| 60 | |||
| 61 | 24 | void PerepelkinIStringDiffCharCountMPI::DistributeData(size_t min_len, std::vector<char> &local_s1, | |
| 62 | std::vector<char> &local_s2) { | ||
| 63 | 24 | const int base_size = static_cast<int>(min_len / proc_num_); | |
| 64 | 24 | const int remainder = static_cast<int>(min_len % proc_num_); | |
| 65 | |||
| 66 | 24 | std::vector<int> counts(proc_num_); | |
| 67 |
1/4✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
24 | std::vector<int> displacements(proc_num_); |
| 68 | |||
| 69 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | if (proc_rank_ == 0) { |
| 70 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
|
36 | for (int i = 0, offset = 0; i < proc_num_; i++) { |
| 71 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 7 times.
|
41 | counts[i] = base_size + (i < remainder ? 1 : 0); |
| 72 | 24 | displacements[i] = offset; | |
| 73 | 24 | offset += counts[i]; | |
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 7 times.
|
24 | const int local_size = base_size + (proc_rank_ < remainder ? 1 : 0); |
| 78 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | local_s1.resize(local_size); |
| 79 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | local_s2.resize(local_size); |
| 80 | |||
| 81 | const char *s1_data = nullptr; | ||
| 82 | const char *s2_data = nullptr; | ||
| 83 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | if (proc_rank_ == 0) { |
| 84 | const auto &[s1, s2] = GetInput(); | ||
| 85 | s1_data = s1.data(); | ||
| 86 | s2_data = s2.data(); | ||
| 87 | } | ||
| 88 | |||
| 89 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | MPI_Scatterv(s1_data, counts.data(), displacements.data(), MPI_CHAR, local_s1.data(), local_size, MPI_CHAR, 0, |
| 90 | MPI_COMM_WORLD); | ||
| 91 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | MPI_Scatterv(s2_data, counts.data(), displacements.data(), MPI_CHAR, local_s2.data(), local_size, MPI_CHAR, 0, |
| 92 | MPI_COMM_WORLD); | ||
| 93 | 24 | } | |
| 94 | |||
| 95 | 24 | bool PerepelkinIStringDiffCharCountMPI::PostProcessingImpl() { | |
| 96 | 24 | return true; | |
| 97 | } | ||
| 98 | |||
| 99 | } // namespace perepelkin_i_string_diff_char_count | ||
| 100 |