| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "luzan_e_matrix_horis_rib_mult_sheme/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_horis_rib_mult_sheme/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace luzan_e_matrix_horis_rib_mult_sheme { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | LuzanEMatrixHorisRibMultShemeMPI::LuzanEMatrixHorisRibMultShemeMPI(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 LuzanEMatrixHorisRibMultShemeMPI::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 | bool res = true; | ||
| 35 | 9 | int height = std::get<1>(GetInput()); | |
| 36 | 9 | int width = std::get<2>(GetInput()); | |
| 37 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | int vec_height = std::get<4>(GetInput()); |
| 38 | |||
| 39 | // matrix check | ||
| 40 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | res = std::get<0>(GetInput()).size() == static_cast<size_t>(height) * static_cast<size_t>(width) && height > 0 && |
| 41 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | width > 0; |
| 42 | |||
| 43 | // vec check | ||
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | res = res && std::get<3>(GetInput()).size() == static_cast<size_t>(vec_height); |
| 45 | |||
| 46 | // matrix & vec sizes cmp. | ||
| 47 | 9 | res = res && (width == vec_height); | |
| 48 | 9 | return res; | |
| 49 | } | ||
| 50 | |||
| 51 | 18 | bool LuzanEMatrixHorisRibMultShemeMPI::PreProcessingImpl() { | |
| 52 | 18 | int rank = 0; | |
| 53 | 18 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 54 | |||
| 55 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | if (rank != 0) { |
| 56 | return true; | ||
| 57 | } | ||
| 58 | |||
| 59 | 9 | int height = std::get<1>(GetInput()); | |
| 60 | |||
| 61 | 9 | GetOutput().resize(height); | |
| 62 |
2/2✓ Branch 0 taken 3122 times.
✓ Branch 1 taken 9 times.
|
3131 | for (int cell = 0; cell < height; cell++) { |
| 63 | 3122 | GetOutput()[cell] = 0; | |
| 64 | } | ||
| 65 | |||
| 66 | return true; | ||
| 67 | } | ||
| 68 | |||
| 69 | 18 | bool LuzanEMatrixHorisRibMultShemeMPI::RunImpl() { | |
| 70 | 18 | int height = 0; | |
| 71 | 18 | int width = 0; | |
| 72 | 18 | int vec_len = 0; | |
| 73 | std::vector<int> mat(0); | ||
| 74 | std::vector<int> vec(0); | ||
| 75 | |||
| 76 | 18 | int rank = 0; | |
| 77 | 18 | int size = 0; | |
| 78 | 18 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 79 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 80 | |||
| 81 | // sharing matrix sizes | ||
| 82 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | if (rank == 0) { |
| 83 | // mat = std::get<0>(GetInput()); | ||
| 84 | 9 | height = std::get<1>(GetInput()); | |
| 85 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | width = std::get<2>(GetInput()); |
| 86 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | vec = std::get<3>(GetInput()); |
| 87 | 9 | vec_len = std::get<4>(GetInput()); | |
| 88 | } | ||
| 89 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Bcast(&height, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 90 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Bcast(&width, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 91 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Bcast(&vec_len, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 92 | |||
| 93 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | vec.resize(vec_len); |
| 94 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Bcast(vec.data(), vec_len, MPI_INT, 0, MPI_COMM_WORLD); |
| 95 | |||
| 96 | // calculating shifts & rows_per_proc (only about rows rigth now) | ||
| 97 | 18 | int rest = height % size; | |
| 98 |
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); |
| 99 |
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 |
| 100 | |||
| 101 | 18 | shift[0] = 0; | |
| 102 | int accumulator = 0; | ||
| 103 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
|
54 | for (int i = 0; i < size; i++) { |
| 104 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 28 times.
|
36 | if (rest != 0) { |
| 105 | 8 | per_proc[i]++; | |
| 106 | 8 | rest--; | |
| 107 | } | ||
| 108 | 36 | shift[i] = accumulator; | |
| 109 | 36 | accumulator = per_proc[i] + shift[i]; | |
| 110 | } | ||
| 111 | |||
| 112 | // preparing to recieve data | ||
| 113 |
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)); |
| 114 |
1/4✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
18 | std::vector<int> shift_elem(size); |
| 115 |
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_elem(size, height / size); // rows per proc |
| 116 | |||
| 117 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 18 times.
|
54 | for (int i = 0; i < size; i++) { |
| 118 | 36 | per_proc_elem[i] = per_proc[i] * width; | |
| 119 | 36 | shift_elem[i] = shift[i] * width; | |
| 120 | } | ||
| 121 | |||
| 122 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Scatterv(std::get<0>(GetInput()).data(), per_proc_elem.data(), shift_elem.data(), MPI_INT, recv.data(), |
| 123 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | per_proc_elem[rank], MPI_INT, 0, MPI_COMM_WORLD); |
| 124 | |||
| 125 |
1/4✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
18 | std::vector<int> prod(static_cast<size_t>(per_proc[rank]), 0); // sums |
| 126 | 18 | int rows_to_calc = static_cast<int>(per_proc[rank]); | |
| 127 |
2/2✓ Branch 0 taken 3122 times.
✓ Branch 1 taken 18 times.
|
3140 | for (int row = 0; row < rows_to_calc; row++) { |
| 128 | 3122 | int row_step = row * width; | |
| 129 | int sum = 0; | ||
| 130 |
2/2✓ Branch 0 taken 1020920 times.
✓ Branch 1 taken 3122 times.
|
1024042 | for (int col = 0; col < width; col++) { |
| 131 | 1020920 | sum += recv[row_step + col] * vec[col]; | |
| 132 | } | ||
| 133 | 3122 | prod[row] = sum; | |
| 134 | } | ||
| 135 | |||
| 136 |
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_prod(height); |
| 137 | |||
| 138 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Gatherv(prod.data(), rows_to_calc, MPI_INT, fin_prod.data(), per_proc.data(), shift.data(), MPI_INT, 0, |
| 139 | MPI_COMM_WORLD); | ||
| 140 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | MPI_Bcast(fin_prod.data(), height, MPI_INT, 0, MPI_COMM_WORLD); |
| 141 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | GetOutput() = fin_prod; |
| 142 | 18 | return true; | |
| 143 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | } |
| 144 | |||
| 145 | 18 | bool LuzanEMatrixHorisRibMultShemeMPI::PostProcessingImpl() { | |
| 146 | 18 | return true; | |
| 147 | } | ||
| 148 | |||
| 149 | } // namespace luzan_e_matrix_horis_rib_mult_sheme | ||
| 150 |