| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "sabirov_s_min_val_matrix/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "sabirov_s_min_val_matrix/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace sabirov_s_min_val_matrix { | ||
| 13 | |||
| 14 | 24 | SabirovSMinValMatrixMPI::SabirovSMinValMatrixMPI(const InType &in) { | |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | 24 | GetInput() = in; | |
| 17 | GetOutput().clear(); | ||
| 18 | 24 | } | |
| 19 | |||
| 20 | 24 | bool SabirovSMinValMatrixMPI::ValidationImpl() { | |
| 21 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | return (GetInput() > 0) && (GetOutput().empty()); |
| 22 | } | ||
| 23 | |||
| 24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | bool SabirovSMinValMatrixMPI::PreProcessingImpl() { |
| 25 | GetOutput().clear(); | ||
| 26 | 24 | GetOutput().reserve(GetInput()); | |
| 27 | 24 | return true; | |
| 28 | } | ||
| 29 | |||
| 30 | 24 | bool SabirovSMinValMatrixMPI::RunImpl() { | |
| 31 | 24 | InType n = GetInput(); | |
| 32 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (n == 0) { |
| 33 | return false; | ||
| 34 | } | ||
| 35 | |||
| 36 | 24 | int rank = 0; | |
| 37 | 24 | int size = 0; | |
| 38 | 24 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 39 | 24 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 40 | |||
| 41 | // Распределяем строки между процессами | ||
| 42 | 24 | int rows_per_proc = n / size; | |
| 43 | 24 | int remainder = n % size; | |
| 44 | |||
| 45 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 7 times.
|
24 | int start_row = (rank * rows_per_proc) + std::min(rank, remainder); |
| 46 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 7 times.
|
24 | int num_local_rows = rows_per_proc + (rank < remainder ? 1 : 0); |
| 47 | 24 | int end_row = start_row + num_local_rows; | |
| 48 | |||
| 49 | // Каждый процесс находит минимумы для своих строк | ||
| 50 | 24 | std::vector<InType> local_mins; | |
| 51 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | local_mins.reserve(num_local_rows); |
| 52 | |||
| 53 | auto generate_value = [](int64_t i, int64_t j) -> InType { | ||
| 54 | constexpr int64_t kA = 1103515245LL; | ||
| 55 | constexpr int64_t kC = 12345LL; | ||
| 56 | constexpr int64_t kM = 2147483648LL; | ||
| 57 | 359299 | int64_t seed = ((i % kM) * (100000007LL % kM) + (j % kM) * (1000000009LL % kM)) % kM; | |
| 58 | 359299 | seed = (seed ^ 42LL) % kM; | |
| 59 | 359299 | int64_t val = ((kA % kM) * (seed % kM) + kC) % kM; | |
| 60 | 359299 | return static_cast<InType>((val % 2000001LL) - 1000000LL); | |
| 61 | }; | ||
| 62 | |||
| 63 |
2/2✓ Branch 0 taken 1125 times.
✓ Branch 1 taken 24 times.
|
1149 | for (InType i = start_row; i < end_row; i++) { |
| 64 | 1125 | InType min_val = generate_value(static_cast<int64_t>(i), 0); | |
| 65 |
2/2✓ Branch 0 taken 358174 times.
✓ Branch 1 taken 1125 times.
|
359299 | for (InType j = 1; j < n; j++) { |
| 66 | 358174 | InType val = generate_value(static_cast<int64_t>(i), static_cast<int64_t>(j)); | |
| 67 | 358174 | min_val = std::min(min_val, val); | |
| 68 | } | ||
| 69 | local_mins.push_back(min_val); | ||
| 70 | } | ||
| 71 | |||
| 72 |
1/4✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
24 | std::vector<int> recvcounts(size); |
| 73 |
1/4✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
24 | std::vector<int> displs(size); |
| 74 | |||
| 75 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | for (int i = 0; i < size; i++) { |
| 76 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 14 times.
|
48 | int proc_rows = rows_per_proc + (i < remainder ? 1 : 0); |
| 77 | 48 | recvcounts[i] = proc_rows; | |
| 78 | 48 | displs[i] = (i * rows_per_proc) + std::min(i, remainder); | |
| 79 | } | ||
| 80 | |||
| 81 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetOutput().resize(n); |
| 82 | |||
| 83 | // Собираем результаты на процессе 0 | ||
| 84 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
|
24 | if (rank == 0) { |
| 85 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Gatherv(local_mins.data(), num_local_rows, MPI_INT, GetOutput().data(), recvcounts.data(), displs.data(), |
| 86 | MPI_INT, 0, MPI_COMM_WORLD); | ||
| 87 | } else { | ||
| 88 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Gatherv(local_mins.data(), num_local_rows, MPI_INT, nullptr, recvcounts.data(), displs.data(), MPI_INT, 0, |
| 89 | MPI_COMM_WORLD); | ||
| 90 | } | ||
| 91 | |||
| 92 | // Рассылаем результат всем процессам | ||
| 93 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | MPI_Bcast(GetOutput().data(), n, MPI_INT, 0, MPI_COMM_WORLD); |
| 94 | |||
| 95 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | return !GetOutput().empty() && (GetOutput().size() == static_cast<size_t>(n)); |
| 96 | } | ||
| 97 | |||
| 98 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | bool SabirovSMinValMatrixMPI::PostProcessingImpl() { |
| 99 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | return !GetOutput().empty() && (GetOutput().size() == static_cast<size_t>(GetInput())); |
| 100 | } | ||
| 101 | |||
| 102 | } // namespace sabirov_s_min_val_matrix | ||
| 103 |