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