| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "moskaev_v_max_value_elem_matrix/mpi/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 "moskaev_v_max_value_elem_matrix/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace moskaev_v_max_value_elem_matrix { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | MoskaevVMaxValueElemMatrixMPI::MoskaevVMaxValueElemMatrixMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | GetInput() = InType(in); |
| 17 | 14 | GetOutput() = 0; | |
| 18 | 14 | } | |
| 19 | |||
| 20 | 14 | bool MoskaevVMaxValueElemMatrixMPI::ValidationImpl() { | |
| 21 | 14 | return (GetOutput() == 0); | |
| 22 | } | ||
| 23 | |||
| 24 | 14 | bool MoskaevVMaxValueElemMatrixMPI::PreProcessingImpl() { | |
| 25 | 14 | return true; | |
| 26 | } | ||
| 27 | |||
| 28 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
|
14 | bool MoskaevVMaxValueElemMatrixMPI::RunImpl() { |
| 29 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
|
14 | if (GetInput().empty()) { |
| 30 | 2 | GetOutput() = 0; | |
| 31 | 2 | return true; | |
| 32 | } | ||
| 33 | 12 | int rank = 0; | |
| 34 | 12 | int size = 0; | |
| 35 | 12 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 36 | 12 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 37 | |||
| 38 | 12 | int total_rows = 0; | |
| 39 | 12 | int cols = 0; | |
| 40 | 12 | std::vector<int> flat_matrix; | |
| 41 | |||
| 42 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank == 0) { |
| 43 | const auto &matrix = GetInput(); | ||
| 44 | 6 | total_rows = static_cast<int>(matrix.size()); | |
| 45 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (total_rows > 0) { |
| 46 | 6 | cols = static_cast<int>(matrix[0].size()); | |
| 47 | |||
| 48 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | flat_matrix.reserve(static_cast<size_t>(total_rows) * static_cast<size_t>(cols)); |
| 49 |
2/2✓ Branch 0 taken 189 times.
✓ Branch 1 taken 6 times.
|
195 | for (const auto &row : matrix) { |
| 50 | 189 | flat_matrix.insert(flat_matrix.end(), row.begin(), row.end()); | |
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Bcast(&total_rows, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 56 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Bcast(&cols, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 57 | |||
| 58 |
2/4✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
|
12 | if (total_rows == 0 || cols == 0) { |
| 59 | ✗ | GetOutput() = 0; | |
| 60 | ✗ | return true; | |
| 61 | } | ||
| 62 | |||
| 63 | 12 | int rows_per_process = total_rows / size; | |
| 64 | 12 | int remainder = total_rows % size; | |
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | int my_rows = rows_per_process + (rank < remainder ? 1 : 0); |
| 67 | 12 | int my_elements = my_rows * cols; | |
| 68 | |||
| 69 |
1/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
12 | std::vector<int> local_data(my_elements, 0); |
| 70 | |||
| 71 |
1/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
12 | std::vector<int> sendcounts(size); |
| 72 |
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); |
| 73 | |||
| 74 | int offset = 0; | ||
| 75 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
|
36 | for (int i = 0; i < size; i++) { |
| 76 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
|
24 | int rows_for_i = rows_per_process + (i < remainder ? 1 : 0); |
| 77 | 24 | sendcounts[i] = rows_for_i * cols; | |
| 78 | 24 | displs[i] = offset * cols; | |
| 79 | 24 | offset += rows_for_i; | |
| 80 | } | ||
| 81 | |||
| 82 |
3/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
18 | MPI_Scatterv(rank == 0 ? flat_matrix.data() : nullptr, sendcounts.data(), displs.data(), MPI_INT, local_data.data(), |
| 83 | my_elements, MPI_INT, 0, MPI_COMM_WORLD); | ||
| 84 | |||
| 85 | 12 | int local_max = INT_MIN; | |
| 86 |
2/2✓ Branch 0 taken 13071 times.
✓ Branch 1 taken 12 times.
|
13083 | for (int element : local_data) { |
| 87 | 13071 | local_max = std::max(local_max, element); | |
| 88 | } | ||
| 89 | |||
| 90 | 12 | int global_max = 0; | |
| 91 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Allreduce(&local_max, &global_max, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD); |
| 92 | |||
| 93 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | GetOutput() = global_max; |
| 94 | |||
| 95 | return true; | ||
| 96 | } | ||
| 97 | |||
| 98 | 14 | bool MoskaevVMaxValueElemMatrixMPI::PostProcessingImpl() { | |
| 99 | 14 | return true; | |
| 100 | } | ||
| 101 | |||
| 102 | } // namespace moskaev_v_max_value_elem_matrix | ||
| 103 |