| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "vlasova_a_elem_matrix_sum/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <tuple> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "vlasova_a_elem_matrix_sum/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace vlasova_a_elem_matrix_sum { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | VlasovaAElemMatrixSumMPI::VlasovaAElemMatrixSumMPI(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | GetOutput() = {}; | ||
| 17 | |||
| 18 | 6 | int rank = 0; | |
| 19 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 20 | |||
| 21 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | std::vector<int> empty_data; |
| 22 | InType processed_input = std::make_tuple(empty_data, std::get<1>(in), std::get<2>(in)); | ||
| 23 | |||
| 24 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 25 | GetInput() = in; | ||
| 26 | } else { | ||
| 27 | GetInput() = processed_input; | ||
| 28 | } | ||
| 29 | 6 | } | |
| 30 | |||
| 31 | 6 | bool VlasovaAElemMatrixSumMPI::ValidationImpl() { | |
| 32 | 6 | int rank = 0; | |
| 33 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 34 | |||
| 35 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank != 0) { |
| 36 | return true; | ||
| 37 | } | ||
| 38 | |||
| 39 | 3 | int rows = std::get<1>(GetInput()); | |
| 40 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | int cols = std::get<2>(GetInput()); |
| 41 | const auto &matrix_data = std::get<0>(GetInput()); | ||
| 42 | |||
| 43 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | return matrix_data.size() == static_cast<size_t>(rows) * static_cast<size_t>(cols) && rows > 0 && cols > 0; |
| 44 | } | ||
| 45 | |||
| 46 | 6 | bool VlasovaAElemMatrixSumMPI::PreProcessingImpl() { | |
| 47 | 6 | int rank = 0; | |
| 48 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 49 | |||
| 50 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 51 | 3 | int rows = std::get<1>(GetInput()); | |
| 52 | 3 | GetOutput().resize(rows, 0); | |
| 53 | } | ||
| 54 | 6 | return true; | |
| 55 | } | ||
| 56 | |||
| 57 | 6 | bool VlasovaAElemMatrixSumMPI::RunImpl() { | |
| 58 | 6 | int rank = 0; | |
| 59 | 6 | int size = 0; | |
| 60 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 61 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 62 | |||
| 63 | 6 | std::vector<int> dimensions(2, 0); | |
| 64 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 65 | 3 | dimensions[0] = std::get<1>(GetInput()); | |
| 66 | 3 | dimensions[1] = std::get<2>(GetInput()); | |
| 67 | } | ||
| 68 | |||
| 69 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Bcast(dimensions.data(), 2, MPI_INT, 0, MPI_COMM_WORLD); |
| 70 | 6 | const int total_rows = dimensions[0]; | |
| 71 | 6 | const int total_cols = dimensions[1]; | |
| 72 | |||
| 73 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (total_rows == 0 || total_cols == 0) { |
| 74 | return true; | ||
| 75 | } | ||
| 76 | |||
| 77 | 6 | const int base_rows_per_process = total_rows / size; | |
| 78 | 6 | const int remaining_rows = total_rows % size; | |
| 79 | |||
| 80 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | const int local_row_count = base_rows_per_process + (rank < remaining_rows ? 1 : 0); |
| 81 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | const int row_offset = (rank * base_rows_per_process) + std::min(rank, remaining_rows); |
| 82 | |||
| 83 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<int> local_matrix_data(static_cast<size_t>(local_row_count) * static_cast<size_t>(total_cols)); |
| 84 | |||
| 85 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<int> send_counts(size); |
| 86 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<int> displacements(size); |
| 87 | |||
| 88 | int current_displacement = 0; | ||
| 89 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (int proc = 0; proc < size; ++proc) { |
| 90 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | int proc_rows = base_rows_per_process + (proc < remaining_rows ? 1 : 0); |
| 91 | 12 | send_counts[proc] = proc_rows * total_cols; | |
| 92 | 12 | displacements[proc] = current_displacement; | |
| 93 | 12 | current_displacement += send_counts[proc]; | |
| 94 | } | ||
| 95 | |||
| 96 |
3/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
|
6 | const auto &global_data = rank == 0 ? std::get<0>(GetInput()) : std::vector<int>(); |
| 97 | |||
| 98 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Scatterv(global_data.data(), send_counts.data(), displacements.data(), MPI_INT, local_matrix_data.data(), |
| 99 | local_row_count * total_cols, MPI_INT, 0, MPI_COMM_WORLD); | ||
| 100 | |||
| 101 |
1/4✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
6 | std::vector<int> local_sums(total_rows, 0); |
| 102 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 6 times.
|
53 | for (int i = 0; i < local_row_count; ++i) { |
| 103 | 47 | int global_row_index = row_offset + i; | |
| 104 |
2/2✓ Branch 0 taken 1345 times.
✓ Branch 1 taken 47 times.
|
1392 | for (int j = 0; j < total_cols; ++j) { |
| 105 | 1345 | local_sums[global_row_index] += local_matrix_data[(i * total_cols) + j]; | |
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 |
2/6✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
6 | std::vector<int> final_sums(total_rows); |
| 110 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MPI_Allreduce(local_sums.data(), final_sums.data(), total_rows, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 111 | |||
| 112 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | GetOutput() = final_sums; |
| 113 | return true; | ||
| 114 | } | ||
| 115 | |||
| 116 | 6 | bool VlasovaAElemMatrixSumMPI::PostProcessingImpl() { | |
| 117 | 6 | return true; | |
| 118 | } | ||
| 119 | |||
| 120 | } // namespace vlasova_a_elem_matrix_sum | ||
| 121 |