|
| 1 | +#include "potashnik_m_char_freq/mpi/include/ops_mpi.hpp" |
| 2 | + |
| 3 | +#include <mpi.h> |
| 4 | + |
| 5 | +#include <string> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +#include "potashnik_m_char_freq/common/include/common.hpp" |
| 9 | + |
| 10 | +namespace potashnik_m_char_freq { |
| 11 | + |
| 12 | +PotashnikMCharFreqMPI::PotashnikMCharFreqMPI(const InType &in) { |
| 13 | + SetTypeOfTask(GetStaticTypeOfTask()); |
| 14 | + |
| 15 | + int rank = 0; |
| 16 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 17 | + if (rank == 0) { |
| 18 | + GetInput() = in; |
| 19 | + } |
| 20 | + |
| 21 | + GetOutput() = 0; |
| 22 | +} |
| 23 | + |
| 24 | +bool PotashnikMCharFreqMPI::ValidationImpl() { |
| 25 | + int rank = 0; |
| 26 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 27 | + if (rank != 0) { |
| 28 | + return true; |
| 29 | + } |
| 30 | + return !std::get<0>(GetInput()).empty(); |
| 31 | +} |
| 32 | + |
| 33 | +bool PotashnikMCharFreqMPI::PreProcessingImpl() { |
| 34 | + return true; |
| 35 | +} |
| 36 | + |
| 37 | +bool PotashnikMCharFreqMPI::RunImpl() { |
| 38 | + int world_size = 0; |
| 39 | + MPI_Comm_size(MPI_COMM_WORLD, &world_size); |
| 40 | + int rank = 0; |
| 41 | + MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 42 | + |
| 43 | + std::string str; |
| 44 | + char chr = 0; |
| 45 | + int string_size = 0; |
| 46 | + |
| 47 | + if (rank == 0) { |
| 48 | + str = std::get<0>(GetInput()); |
| 49 | + chr = std::get<1>(GetInput()); |
| 50 | + string_size = static_cast<int>(str.size()); |
| 51 | + } |
| 52 | + |
| 53 | + MPI_Bcast(&string_size, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 54 | + MPI_Bcast(&chr, 1, MPI_CHAR, 0, MPI_COMM_WORLD); |
| 55 | + |
| 56 | + int block_size = string_size / world_size; |
| 57 | + int remainder = string_size % world_size; |
| 58 | + |
| 59 | + std::vector<int> local_sizes(world_size, 0); |
| 60 | + std::vector<int> local_start_positions(world_size, 0); |
| 61 | + |
| 62 | + if (rank == 0) { |
| 63 | + for (int i = 0; i < world_size; i++) { |
| 64 | + if (remainder > i) { |
| 65 | + local_sizes[i] = block_size + 1; |
| 66 | + } else { |
| 67 | + local_sizes[i] = block_size; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + for (int i = 1; i < world_size; i++) { |
| 72 | + local_start_positions[i] = local_start_positions[i - 1] + local_sizes[i - 1]; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + int cur_count = 0; |
| 77 | + MPI_Scatter(local_sizes.data(), 1, MPI_INT, &cur_count, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 78 | + |
| 79 | + std::string cur_str(cur_count, '\0'); |
| 80 | + if (rank == 0) { |
| 81 | + std::vector<char> temp_str(str.begin(), str.end()); // Dont work without const |
| 82 | + MPI_Scatterv(temp_str.data(), local_sizes.data(), local_start_positions.data(), MPI_CHAR, cur_str.data(), cur_count, |
| 83 | + MPI_CHAR, 0, MPI_COMM_WORLD); |
| 84 | + ; |
| 85 | + } else { |
| 86 | + MPI_Scatterv(nullptr, nullptr, nullptr, MPI_CHAR, cur_str.data(), cur_count, MPI_CHAR, 0, |
| 87 | + MPI_COMM_WORLD); // Just recieving data |
| 88 | + } |
| 89 | + |
| 90 | + int cur_res = 0; |
| 91 | + for (char c : cur_str) { |
| 92 | + if (c == chr) { |
| 93 | + cur_res++; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + int total_res = 0; |
| 98 | + MPI_Allreduce(&cur_res, &total_res, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 99 | + GetOutput() = total_res; |
| 100 | + |
| 101 | + return true; |
| 102 | +} |
| 103 | + |
| 104 | +bool PotashnikMCharFreqMPI::PostProcessingImpl() { |
| 105 | + return true; |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace potashnik_m_char_freq |
0 commit comments