| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "lazareva_a_max_val_matrix/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <limits> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "lazareva_a_max_val_matrix/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace lazareva_a_max_val_matrix { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | LazarevaAMaxValMatrixMPI::LazarevaAMaxValMatrixMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | GetInput() = in; |
| 17 | GetOutput().clear(); | ||
| 18 | 10 | } | |
| 19 | |||
| 20 | 10 | bool LazarevaAMaxValMatrixMPI::ValidationImpl() { | |
| 21 | 10 | int rank = 0; | |
| 22 | 10 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 23 | |||
| 24 | 10 | int is_valid = 0; | |
| 25 | |||
| 26 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 27 | 5 | is_valid = static_cast<int>( | |
| 28 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
|
5 | (GetInput().size() >= 2) && GetOutput().empty() && (GetInput()[0] > 0) && (GetInput()[1] > 0) && |
| 29 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
|
15 | (GetInput()[0] <= std::numeric_limits<int>::max() / GetInput()[1]) && |
| 30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | (GetInput().size() == (2 + (static_cast<size_t>(GetInput()[0]) * static_cast<size_t>(GetInput()[1]))))); |
| 31 | } | ||
| 32 | |||
| 33 | 10 | MPI_Bcast(&is_valid, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 34 | |||
| 35 | 10 | return static_cast<bool>(is_valid); | |
| 36 | } | ||
| 37 | |||
| 38 | 10 | bool LazarevaAMaxValMatrixMPI::PreProcessingImpl() { | |
| 39 | 10 | int rank = 0; | |
| 40 | 10 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 41 | |||
| 42 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 43 | GetOutput().clear(); | ||
| 44 | 5 | n_ = GetInput()[0]; | |
| 45 | 5 | m_ = GetInput()[1]; | |
| 46 | } | ||
| 47 | |||
| 48 | 10 | MPI_Bcast(&n_, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 49 | 10 | MPI_Bcast(&m_, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 52 | 5 | GetOutput().reserve(n_); | |
| 53 | } | ||
| 54 | |||
| 55 | 10 | return true; | |
| 56 | } | ||
| 57 | |||
| 58 | 10 | bool LazarevaAMaxValMatrixMPI::RunImpl() { | |
| 59 | 10 | int rank = 0; | |
| 60 | 10 | int size = 0; | |
| 61 | 10 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 62 | 10 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 63 | |||
| 64 | 10 | std::vector<int> matrix; | |
| 65 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 66 | const auto &input = GetInput(); | ||
| 67 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
10 | matrix = std::vector<int>(input.begin() + 2, input.end()); |
| 68 | } | ||
| 69 | |||
| 70 | 10 | int rows_per_proc = n_ / size; | |
| 71 | 10 | int remainder = n_ % size; | |
| 72 | |||
| 73 |
1/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
10 | std::vector<int> sendcounts(size); |
| 74 |
1/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
10 | std::vector<int> displs(size); |
| 75 | |||
| 76 | int offset = 0; | ||
| 77 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
|
30 | for (int i = 0; i < size; i++) { |
| 78 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
|
20 | int rows = rows_per_proc + (i < remainder ? 1 : 0); |
| 79 | 20 | sendcounts[i] = rows * m_; | |
| 80 | 20 | displs[i] = offset; | |
| 81 | 20 | offset += rows * m_; | |
| 82 | } | ||
| 83 | |||
| 84 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | int local_rows = sendcounts[rank] / m_; |
| 85 |
1/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
10 | std::vector<int> local_matrix(sendcounts[rank]); |
| 86 |
1/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
10 | std::vector<int> local_max(local_rows); |
| 87 | |||
| 88 |
3/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
|
15 | MPI_Scatterv(rank == 0 ? matrix.data() : nullptr, sendcounts.data(), displs.data(), MPI_INT, local_matrix.data(), |
| 89 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | sendcounts[rank], MPI_INT, 0, MPI_COMM_WORLD); |
| 90 | |||
| 91 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 10 times.
|
32 | for (int i = 0; i < local_rows; i++) { |
| 92 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22 | auto row_begin = local_matrix.begin() + static_cast<std::vector<int>::difference_type>(i) * m_; |
| 93 | auto row_end = row_begin + m_; | ||
| 94 | 22 | local_max[i] = *std::max_element(row_begin, row_end); | |
| 95 | } | ||
| 96 | |||
| 97 |
1/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
10 | std::vector<int> recvcounts(size); |
| 98 |
1/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
10 | std::vector<int> recvdispls(size); |
| 99 | |||
| 100 | offset = 0; | ||
| 101 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
|
30 | for (int i = 0; i < size; i++) { |
| 102 | 20 | recvcounts[i] = sendcounts[i] / m_; | |
| 103 | 20 | recvdispls[i] = offset; | |
| 104 | 20 | offset += recvcounts[i]; | |
| 105 | } | ||
| 106 | |||
| 107 | 10 | std::vector<int> global_max; | |
| 108 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 109 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | global_max.resize(n_); |
| 110 | } | ||
| 111 | |||
| 112 |
3/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
|
15 | MPI_Gatherv(local_max.data(), local_rows, MPI_INT, rank == 0 ? global_max.data() : nullptr, recvcounts.data(), |
| 113 | recvdispls.data(), MPI_INT, 0, MPI_COMM_WORLD); | ||
| 114 | |||
| 115 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 116 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | GetOutput() = global_max; |
| 117 | } | ||
| 118 | |||
| 119 | 10 | return true; | |
| 120 | } | ||
| 121 | |||
| 122 | 10 | bool LazarevaAMaxValMatrixMPI::PostProcessingImpl() { | |
| 123 | 10 | int rank = 0; | |
| 124 | 10 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
|
10 | if (rank == 0) { |
| 127 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | return !GetOutput().empty() && (GetOutput().size() == static_cast<size_t>(n_)); |
| 128 | } | ||
| 129 | |||
| 130 | return true; | ||
| 131 | } | ||
| 132 | |||
| 133 | } // namespace lazareva_a_max_val_matrix | ||
| 134 |