| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "petrov_e_find_max_in_columns_matrix/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cfloat> | ||
| 7 | #include <cmath> | ||
| 8 | #include <limits> | ||
| 9 | #include <type_traits> | ||
| 10 | #include <utility> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "petrov_e_find_max_in_columns_matrix/common/include/common.hpp" | ||
| 14 | |||
| 15 | namespace petrov_e_find_max_in_columns_matrix { | ||
| 16 | |||
| 17 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | PetrovEFindMaxInColumnsMatrixMPI::PetrovEFindMaxInColumnsMatrixMPI(const InType &in) { |
| 18 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 19 | GetInput() = in; | ||
| 20 | GetOutput() = {}; | ||
| 21 | 20 | } | |
| 22 | |||
| 23 | 20 | bool PetrovEFindMaxInColumnsMatrixMPI::ValidationImpl() { | |
| 24 |
2/4✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
|
20 | return (std::get<0>(GetInput()) * std::get<1>(GetInput()) == static_cast<int>(std::get<2>(GetInput()).size())) && |
| 25 | 20 | (GetOutput().empty()); | |
| 26 | } | ||
| 27 | |||
| 28 | 20 | bool PetrovEFindMaxInColumnsMatrixMPI::PreProcessingImpl() { | |
| 29 | 20 | return (std::get<0>(GetInput()) * std::get<1>(GetInput()) == static_cast<int>(std::get<2>(GetInput()).size())); | |
| 30 | } | ||
| 31 | |||
| 32 | 20 | bool PetrovEFindMaxInColumnsMatrixMPI::RunImpl() { | |
| 33 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if ((std::get<0>(GetInput()) * std::get<1>(GetInput()) != static_cast<int>(std::get<2>(GetInput()).size()))) { |
| 34 | return false; | ||
| 35 | } | ||
| 36 | |||
| 37 | auto &n = std::get<0>(GetInput()); | ||
| 38 | auto &m = std::get<1>(GetInput()); | ||
| 39 | auto &matrix = std::get<2>(GetInput()); | ||
| 40 | OutType &res = GetOutput(); | ||
| 41 | using MatrixElemType = std::remove_reference_t<decltype(matrix[0])>; | ||
| 42 | MPI_Datatype mpi_matrix_elem_type = petrov_e_find_max_in_columns_matrix::GetMPIDatatype<MatrixElemType>(); | ||
| 43 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if (mpi_matrix_elem_type == MPI_DATATYPE_NULL) { |
| 44 | return false; | ||
| 45 | } | ||
| 46 | 20 | int proc_num = 0; | |
| 47 | 20 | int proc_rank = 0; | |
| 48 | 20 | MPI_Comm_size(MPI_COMM_WORLD, &proc_num); | |
| 49 | 20 | MPI_Comm_rank(MPI_COMM_WORLD, &proc_rank); | |
| 50 | 20 | int i = 0; | |
| 51 | int j = 0; | ||
| 52 | 20 | MatrixElemType max = NAN; | |
| 53 | 20 | res.resize(m); | |
| 54 | 20 | int col_num_per_proc = m / proc_num; | |
| 55 | 20 | int col_num_wo_proc = m % proc_num; | |
| 56 | int flag = 0; | ||
| 57 | |||
| 58 | 20 | std::vector<int> start(proc_num); | |
| 59 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<int> end(proc_num); |
| 60 | |||
| 61 | 20 | int proc_start = 0; | |
| 62 | 20 | int proc_end = 0; | |
| 63 | |||
| 64 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (proc_rank == 0) { |
| 65 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
|
30 | for (i = 0; i < proc_num; i++) { |
| 66 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 6 times.
|
20 | if (i < col_num_wo_proc) { |
| 67 | flag = 1; | ||
| 68 | } else { | ||
| 69 | flag = 0; | ||
| 70 | } | ||
| 71 | 20 | start[i] = (i * col_num_per_proc) + std::min(i, col_num_wo_proc); | |
| 72 | 20 | end[i] = start[i] + col_num_per_proc + flag; | |
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Scatter(start.data(), 1, MPI_INT, &proc_start, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 77 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Scatter(end.data(), 1, MPI_INT, &proc_end, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 78 | |||
| 79 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | OutType proc_res(m, std::numeric_limits<MatrixElemType>::lowest()); |
| 80 | |||
| 81 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 20 times.
|
48 | for (i = proc_start; std::cmp_less(i, proc_end); i++) { |
| 82 | 28 | max = matrix[static_cast<int>(i * n)]; | |
| 83 |
2/2✓ Branch 0 taken 61 times.
✓ Branch 1 taken 28 times.
|
89 | for (j = 1; j < n; j++) { |
| 84 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 47 times.
|
75 | max = std::max(matrix[(i * n) + j], max); |
| 85 | } | ||
| 86 | 28 | proc_res[i] = max; | |
| 87 | } | ||
| 88 | |||
| 89 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | MPI_Allreduce(proc_res.data(), res.data(), m, mpi_matrix_elem_type, MPI_MAX, MPI_COMM_WORLD); |
| 90 | |||
| 91 | return true; | ||
| 92 | } | ||
| 93 | |||
| 94 | 20 | bool PetrovEFindMaxInColumnsMatrixMPI::PostProcessingImpl() { | |
| 95 | 20 | return true; | |
| 96 | } | ||
| 97 | |||
| 98 | } // namespace petrov_e_find_max_in_columns_matrix | ||
| 99 |