| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "luzan_e_matrix_rows_sum/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <tuple> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "luzan_e_matrix_rows_sum/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace luzan_e_matrix_rows_sum { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | LuzanEMatrixRowsSumMPI::LuzanEMatrixRowsSumMPI(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetOutput() = {}; | ||
| 16 | |||
| 17 | // saving matrix only if it's rank=0 | ||
| 18 | 18 | int rank = 0; | |
| 19 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 20 | |||
| 21 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | if (rank == 0) { |
| 22 | GetInput() = in; | ||
| 23 | } else { | ||
| 24 | 9 | GetInput() = {}; | |
| 25 | } | ||
| 26 | 18 | } | |
| 27 | |||
| 28 | 18 | bool LuzanEMatrixRowsSumMPI::ValidationImpl() { | |
| 29 | 18 | int rank = 0; | |
| 30 | 18 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 31 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | if (rank != 0) { |
| 32 | return true; | ||
| 33 | } | ||
| 34 | |||
| 35 | 9 | int height = std::get<1>(GetInput()); | |
| 36 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | int width = std::get<2>(GetInput()); |
| 37 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | return std::get<0>(GetInput()).size() == static_cast<size_t>(height) * static_cast<size_t>(width) && height > 0 && |
| 38 | 9 | width > 0; | |
| 39 | } | ||
| 40 | |||
| 41 | 18 | bool LuzanEMatrixRowsSumMPI::PreProcessingImpl() { | |
| 42 | 18 | int rank = 0; | |
| 43 | 18 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 44 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | if (rank != 0) { |
| 45 | return true; | ||
| 46 | } | ||
| 47 | |||
| 48 | 9 | int height = std::get<1>(GetInput()); | |
| 49 | 9 | GetOutput().resize(height); | |
| 50 |
2/2✓ Branch 0 taken 3122 times.
✓ Branch 1 taken 9 times.
|
3131 | for (int row = 0; row < height; row++) { |
| 51 | 3122 | GetOutput()[row] = 0; | |
| 52 | } | ||
| 53 | return true; | ||
| 54 | } | ||
| 55 | |||
| 56 | 18 | bool LuzanEMatrixRowsSumMPI::RunImpl() { | |
| 57 | // mpi things | ||
| 58 | 18 | int height = 0; | |
| 59 | 18 | int width = 0; | |
| 60 | 18 | std::tuple_element_t<0, InType> mat; | |
| 61 | |||
| 62 | 18 | int rank = 0; | |
| 63 | 18 | int size = 0; | |
| 64 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 65 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 66 | |||
| 67 | // getting input matrix on rank=0 | ||
| 68 | // getting & sharing matrix sizes | ||
| 69 |
1/4✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
18 | std::vector<int> dim(2, 0); |
| 70 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | if (rank == 0) { |
| 71 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | mat = std::get<0>(GetInput()); |
| 72 | 9 | height = std::get<1>(GetInput()); | |
| 73 | 9 | width = std::get<2>(GetInput()); | |
| 74 | } | ||
| 75 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Bcast(&height, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 76 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Bcast(&width, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 77 | |||
| 78 | // calcilating shifts & rows_per_proc (only about rows rigth now) | ||
| 79 | 18 | int rest = height % size; | |
| 80 |
1/4✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
18 | std::vector<int> shift(size, 0); |
| 81 |
1/4✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
18 | std::vector<int> per_proc(size, height / size); // rows per proc |
| 82 | |||
| 83 | int accumulator = 0; | ||
| 84 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
|
54 | for (int i = 0; i < size; i++) { |
| 85 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 28 times.
|
36 | if (rest > 0) { |
| 86 | 8 | per_proc[i]++; | |
| 87 | 8 | rest--; | |
| 88 | } | ||
| 89 | 36 | shift[i] = accumulator; | |
| 90 | 36 | accumulator = per_proc[i] + shift[i]; | |
| 91 | } | ||
| 92 | |||
| 93 | // preparing to recieve data | ||
| 94 |
1/4✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
18 | std::vector<int> recv(static_cast<size_t>(per_proc[rank] * width)); |
| 95 | |||
| 96 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
|
54 | for (int i = 0; i < size; i++) { |
| 97 | 36 | per_proc[i] *= width; // now it's about elements | |
| 98 | 36 | shift[i] *= width; | |
| 99 | } | ||
| 100 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Scatterv(mat.data(), per_proc.data(), shift.data(), MPI_INT, recv.data(), per_proc[rank], MPI_INT, 0, |
| 101 | MPI_COMM_WORLD); | ||
| 102 | mat.clear(); // no need anymore | ||
| 103 | |||
| 104 | // calculating | ||
| 105 |
1/4✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
18 | std::vector<int> rows_sum(static_cast<size_t>(per_proc[rank] / width)); // sums |
| 106 | 18 | int rows_to_calc = static_cast<int>(per_proc[rank] / width); | |
| 107 |
2/2✓ Branch 0 taken 3122 times.
✓ Branch 1 taken 18 times.
|
3140 | for (int row = 0; row < rows_to_calc; row++) { |
| 108 |
2/2✓ Branch 0 taken 1020920 times.
✓ Branch 1 taken 3122 times.
|
1024042 | for (int col = 0; col < width; col++) { |
| 109 | 1020920 | rows_sum[row] += recv[(row * width) + col]; | |
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
|
54 | for (int i = 0; i < size; i++) { |
| 114 | 36 | per_proc[i] /= width; // back to rows | |
| 115 | 36 | shift[i] /= width; | |
| 116 | } | ||
| 117 | |||
| 118 |
2/6✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
18 | std::vector<int> fin_sum(height); |
| 119 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Gatherv(rows_sum.data(), rows_to_calc, MPI_INT, fin_sum.data(), per_proc.data(), shift.data(), MPI_INT, 0, |
| 120 | MPI_COMM_WORLD); | ||
| 121 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Bcast(fin_sum.data(), height, MPI_INT, 0, MPI_COMM_WORLD); |
| 122 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | GetOutput() = fin_sum; |
| 123 | 18 | return true; | |
| 124 | } | ||
| 125 | |||
| 126 | 18 | bool LuzanEMatrixRowsSumMPI::PostProcessingImpl() { | |
| 127 | 18 | return true; | |
| 128 | } | ||
| 129 | |||
| 130 | } // namespace luzan_e_matrix_rows_sum | ||
| 131 |