| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "morozova_s_matrix_max_value/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "morozova_s_matrix_max_value/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace morozova_s_matrix_max_value { | ||
| 12 | |||
| 13 | ✗ | MorozovaSMatrixMaxValueMPI::MorozovaSMatrixMaxValueMPI(const InType &in) : BaseTask() { | |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | ✗ | GetInput() = InType(in); | |
| 16 | ✗ | GetOutput() = 0; | |
| 17 | ✗ | } | |
| 18 | |||
| 19 | ✗ | bool MorozovaSMatrixMaxValueMPI::ValidationImpl() { | |
| 20 | const auto &matrix = GetInput(); | ||
| 21 | ✗ | if (matrix.empty()) { | |
| 22 | return true; | ||
| 23 | } | ||
| 24 | ✗ | if (matrix[0].empty()) { | |
| 25 | return true; | ||
| 26 | } | ||
| 27 | const size_t cols = matrix[0].size(); | ||
| 28 | return std::ranges::all_of(matrix, [cols](const auto &row) { return row.size() == cols; }); | ||
| 29 | } | ||
| 30 | |||
| 31 | ✗ | bool MorozovaSMatrixMaxValueMPI::PreProcessingImpl() { | |
| 32 | ✗ | return true; | |
| 33 | } | ||
| 34 | |||
| 35 | ✗ | bool MorozovaSMatrixMaxValueMPI::RunImpl() { | |
| 36 | ✗ | int rank = 0; | |
| 37 | ✗ | int size = 1; | |
| 38 | ✗ | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 39 | ✗ | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 40 | const auto &matrix = GetInput(); | ||
| 41 | ✗ | if (matrix.empty() || matrix[0].empty()) { | |
| 42 | ✗ | GetOutput() = 0; | |
| 43 | ✗ | return true; | |
| 44 | } | ||
| 45 | ✗ | const int rows = static_cast<int>(matrix.size()); | |
| 46 | ✗ | const int cols = static_cast<int>(matrix[0].size()); | |
| 47 | ✗ | const int total = rows * cols; | |
| 48 | ✗ | std::vector<int> flat; | |
| 49 | ✗ | if (rank == 0) { | |
| 50 | ✗ | flat.reserve(total); | |
| 51 | ✗ | for (const auto &row : matrix) { | |
| 52 | ✗ | flat.insert(flat.end(), row.begin(), row.end()); | |
| 53 | } | ||
| 54 | } | ||
| 55 | ✗ | std::vector<int> counts(size); | |
| 56 | ✗ | std::vector<int> displs(size); | |
| 57 | ✗ | const int base = total / size; | |
| 58 | ✗ | const int rest = total % size; | |
| 59 | ✗ | for (int i = 0, off = 0; i < size; ++i) { | |
| 60 | ✗ | counts[i] = base + static_cast<int>(i < rest); | |
| 61 | ✗ | displs[i] = off; | |
| 62 | ✗ | off += counts[i]; | |
| 63 | } | ||
| 64 | ✗ | std::vector<int> local(counts[rank]); | |
| 65 | ✗ | MPI_Scatterv(rank == 0 ? flat.data() : nullptr, counts.data(), displs.data(), MPI_INT, local.data(), counts[rank], | |
| 66 | MPI_INT, 0, MPI_COMM_WORLD); | ||
| 67 | |||
| 68 | ✗ | int local_max = local[0]; | |
| 69 | ✗ | for (int v : local) { | |
| 70 | ✗ | local_max = std::max(local_max, v); | |
| 71 | } | ||
| 72 | ✗ | int global_max = 0; | |
| 73 | ✗ | MPI_Allreduce(&local_max, &global_max, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD); | |
| 74 | ✗ | GetOutput() = global_max; | |
| 75 | return true; | ||
| 76 | } | ||
| 77 | |||
| 78 | ✗ | bool MorozovaSMatrixMaxValueMPI::PostProcessingImpl() { | |
| 79 | ✗ | return true; | |
| 80 | } | ||
| 81 | |||
| 82 | } // namespace morozova_s_matrix_max_value | ||
| 83 |