| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "shkrebko_m_shell_sort_batcher_merge/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <cstddef> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "shkrebko_m_shell_sort_batcher_merge/common/include/mpi_utils.hpp" | ||
| 9 | #include "shkrebko_m_shell_sort_batcher_merge/common/include/utils.hpp" | ||
| 10 | |||
| 11 | namespace shkrebko_m_shell_sort_batcher_merge { | ||
| 12 | |||
| 13 | 6 | bool ShkrebkoMShellSortBatcherMergeMPI::ValidationImpl() { | |
| 14 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank_); | |
| 15 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &world_size_); | |
| 16 | 6 | return true; | |
| 17 | } | ||
| 18 | |||
| 19 | 6 | bool ShkrebkoMShellSortBatcherMergeMPI::PreProcessingImpl() { | |
| 20 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &world_rank_); | |
| 21 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &world_size_); | |
| 22 | |||
| 23 | 6 | int global_size = 0; | |
| 24 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (world_rank_ == 0) { |
| 25 | 3 | global_size = static_cast<int>(GetInput().size()); | |
| 26 | } | ||
| 27 | 6 | MPI_Bcast(&global_size, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 28 | |||
| 29 | 6 | counts_.assign(world_size_, 0); | |
| 30 | 6 | displs_.assign(world_size_, 0); | |
| 31 | |||
| 32 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | const int base = (world_size_ > 0) ? (global_size / world_size_) : 0; |
| 33 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | const int rem = (world_size_ > 0) ? (global_size % world_size_) : 0; |
| 34 | |||
| 35 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
|
18 | for (int rank_index = 0; rank_index < world_size_; ++rank_index) { |
| 36 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
24 | counts_[rank_index] = base + ((rank_index < rem) ? 1 : 0); |
| 37 | } | ||
| 38 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | for (int rank_index = 1; rank_index < world_size_; ++rank_index) { |
| 39 | 6 | displs_[rank_index] = displs_[rank_index - 1] + counts_[rank_index - 1]; | |
| 40 | } | ||
| 41 | |||
| 42 | 6 | local_.assign(static_cast<std::size_t>(counts_[world_rank_]), 0); | |
| 43 | |||
| 44 | int *send_buf = nullptr; | ||
| 45 |
3/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
6 | if (world_rank_ == 0 && global_size > 0) { |
| 46 | send_buf = GetInput().data(); | ||
| 47 | } | ||
| 48 | |||
| 49 | 6 | MPI_Scatterv(send_buf, counts_.data(), displs_.data(), MPI_INT, local_.data(), counts_[world_rank_], MPI_INT, 0, | |
| 50 | MPI_COMM_WORLD); | ||
| 51 | |||
| 52 | GetOutput().clear(); | ||
| 53 | 6 | return true; | |
| 54 | } | ||
| 55 | |||
| 56 | 6 | bool ShkrebkoMShellSortBatcherMergeMPI::RunImpl() { | |
| 57 | 6 | ShellSort(&local_); | |
| 58 | |||
| 59 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
|
9 | for (int step = 1; step < world_size_; step <<= 1) { |
| 60 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if ((world_rank_ % (2 * step)) == 0) { |
| 61 | 3 | const int partner = world_rank_ + step; | |
| 62 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (partner < world_size_) { |
| 63 | 3 | std::vector<int> other = RecvVector(partner, 1000 + step, MPI_COMM_WORLD); | |
| 64 |
2/6✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
6 | local_ = BatcherOddEvenMerge(local_, other); |
| 65 | } | ||
| 66 | } else { | ||
| 67 | 3 | const int partner = world_rank_ - step; | |
| 68 | 3 | SendVector(partner, 1000 + step, local_, MPI_COMM_WORLD); | |
| 69 | 3 | break; | |
| 70 | } | ||
| 71 | } | ||
| 72 | 6 | return true; | |
| 73 | } | ||
| 74 | |||
| 75 | 6 | bool ShkrebkoMShellSortBatcherMergeMPI::PostProcessingImpl() { | |
| 76 | 6 | int out_size = 0; | |
| 77 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (world_rank_ == 0) { |
| 78 | 3 | GetOutput() = local_; | |
| 79 | 3 | out_size = static_cast<int>(GetOutput().size()); | |
| 80 | } | ||
| 81 | |||
| 82 | 6 | MPI_Bcast(&out_size, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 83 | |||
| 84 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (world_rank_ != 0) { |
| 85 | 3 | GetOutput().assign(static_cast<std::size_t>(out_size), 0); | |
| 86 | } | ||
| 87 | |||
| 88 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (out_size > 0) { |
| 89 | 6 | MPI_Bcast(GetOutput().data(), out_size, MPI_INT, 0, MPI_COMM_WORLD); | |
| 90 | } | ||
| 91 | 6 | return true; | |
| 92 | } | ||
| 93 | |||
| 94 | } // namespace shkrebko_m_shell_sort_batcher_merge | ||
| 95 |