| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kurpiakov_a_shellsort/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "kurpiakov_a_shellsort/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace kurpiakov_a_shellsort { | ||
| 10 | |||
| 11 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | KurpiakovAShellsortMPI::KurpiakovAShellsortMPI(const InType &in) { |
| 12 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 13 | GetInput() = in; | ||
| 14 | GetOutput() = {}; | ||
| 15 | 14 | } | |
| 16 | |||
| 17 | 14 | bool KurpiakovAShellsortMPI::ValidationImpl() { | |
| 18 | 14 | int rank = 0; | |
| 19 | 14 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 20 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank == 0) { |
| 21 | 7 | int input_size = std::get<0>(GetInput()); | |
| 22 | 7 | int vector_size = static_cast<int>(std::get<1>(GetInput()).size()); | |
| 23 | 7 | return input_size == vector_size && input_size >= 0; | |
| 24 | } | ||
| 25 | return true; | ||
| 26 | } | ||
| 27 | |||
| 28 | 14 | bool KurpiakovAShellsortMPI::PreProcessingImpl() { | |
| 29 | 14 | world_size_ = 0; | |
| 30 | 14 | rank_ = 0; | |
| 31 | |||
| 32 | 14 | MPI_Comm_size(MPI_COMM_WORLD, &world_size_); | |
| 33 | 14 | MPI_Comm_rank(MPI_COMM_WORLD, &rank_); | |
| 34 | |||
| 35 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank_ == 0) { |
| 36 | 7 | data_ = std::get<1>(GetInput()); | |
| 37 | } | ||
| 38 | |||
| 39 | 14 | int n = 0; | |
| 40 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank_ == 0) { |
| 41 | 7 | n = static_cast<int>(data_.size()); | |
| 42 | } | ||
| 43 | 14 | MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 44 | |||
| 45 | 14 | return true; | |
| 46 | } | ||
| 47 | |||
| 48 | 12 | void KurpiakovAShellsortMPI::ShellSortLocal(OutType &arr) { | |
| 49 | 12 | int n = static_cast<int>(arr.size()); | |
| 50 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 12 times.
|
44 | for (int gap = n / 2; gap > 0; gap /= 2) { |
| 51 |
2/2✓ Branch 0 taken 7068 times.
✓ Branch 1 taken 32 times.
|
7100 | for (int i = gap; i < n; ++i) { |
| 52 | 7068 | int temp = arr[i]; | |
| 53 | int j = i; | ||
| 54 |
4/4✓ Branch 0 taken 12834 times.
✓ Branch 1 taken 492 times.
✓ Branch 2 taken 6258 times.
✓ Branch 3 taken 6576 times.
|
13326 | while (j >= gap && arr[j - gap] > temp) { |
| 55 | 6258 | arr[j] = arr[j - gap]; | |
| 56 | j -= gap; | ||
| 57 | } | ||
| 58 | 7068 | arr[j] = temp; | |
| 59 | } | ||
| 60 | } | ||
| 61 | 12 | } | |
| 62 | |||
| 63 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | void KurpiakovAShellsortMPI::MergeSortedArrays(const OutType &send_counts, const OutType &gathered_data, |
| 64 | const OutType &displs, const int &n) { | ||
| 65 | data_.clear(); | ||
| 66 | 6 | data_.reserve(n); | |
| 67 | |||
| 68 | 6 | std::vector<int> indices(world_size_, 0); | |
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 1041 times.
✓ Branch 1 taken 6 times.
|
1047 | for (int k = 0; k < n; k++) { |
| 71 | 1041 | int min_val = 0; | |
| 72 | int min_part = -1; | ||
| 73 | |||
| 74 |
2/2✓ Branch 0 taken 2082 times.
✓ Branch 1 taken 1041 times.
|
3123 | for (int i = 0; i < world_size_; i++) { |
| 75 |
2/2✓ Branch 0 taken 2066 times.
✓ Branch 1 taken 16 times.
|
2082 | if (indices[i] < send_counts[i]) { |
| 76 | 2066 | int val = gathered_data[displs[i] + indices[i]]; | |
| 77 |
4/4✓ Branch 0 taken 1025 times.
✓ Branch 1 taken 1041 times.
✓ Branch 2 taken 514 times.
✓ Branch 3 taken 511 times.
|
2066 | if (min_part == -1 || val < min_val) { |
| 78 | 1555 | min_val = val; | |
| 79 | min_part = i; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | data_.push_back(min_val); | ||
| 85 | 1041 | indices[min_part]++; | |
| 86 | } | ||
| 87 | 6 | } | |
| 88 | |||
| 89 | 14 | bool KurpiakovAShellsortMPI::RunImpl() { | |
| 90 | 14 | int n = 0; | |
| 91 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14 | if (rank_ == 0) { |
| 92 | 7 | n = static_cast<int>(data_.size()); | |
| 93 | } | ||
| 94 | |||
| 95 | 14 | MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 96 | |||
| 97 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
|
14 | if (n == 0) { |
| 98 | data_.clear(); | ||
| 99 | 2 | return true; | |
| 100 | } | ||
| 101 | |||
| 102 | 12 | int base_size = n / world_size_; | |
| 103 | 12 | int remainder = n % world_size_; | |
| 104 | |||
| 105 | 12 | std::vector<int> send_counts(world_size_); | |
| 106 |
1/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
12 | std::vector<int> displs(world_size_); |
| 107 | |||
| 108 | int offset = 0; | ||
| 109 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12 times.
|
36 | for (int i = 0; i < world_size_; i++) { |
| 110 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 2 times.
|
46 | send_counts[i] = base_size + (i < remainder ? 1 : 0); |
| 111 | 24 | displs[i] = offset; | |
| 112 | 24 | offset += send_counts[i]; | |
| 113 | } | ||
| 114 | |||
| 115 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | int local_size = send_counts[rank_]; |
| 116 |
3/6✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
12 | std::vector<int> local_data(local_size); |
| 117 | |||
| 118 |
3/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
18 | MPI_Scatterv(rank_ == 0 ? data_.data() : nullptr, send_counts.data(), displs.data(), MPI_INT, local_data.data(), |
| 119 | local_size, MPI_INT, 0, MPI_COMM_WORLD); | ||
| 120 | |||
| 121 | 12 | ShellSortLocal(local_data); | |
| 122 | |||
| 123 |
2/6✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
12 | std::vector<int> gathered_data(n); |
| 124 | |||
| 125 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Gatherv(local_data.data(), local_size, MPI_INT, gathered_data.data(), send_counts.data(), displs.data(), MPI_INT, |
| 126 | 0, MPI_COMM_WORLD); | ||
| 127 | |||
| 128 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank_ == 0) { |
| 129 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | MergeSortedArrays(send_counts, gathered_data, displs, n); |
| 130 | } | ||
| 131 | |||
| 132 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
|
12 | if (rank_ != 0) { |
| 133 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | data_.resize(n); |
| 134 | } | ||
| 135 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | MPI_Bcast(data_.data(), n, MPI_INT, 0, MPI_COMM_WORLD); |
| 136 | |||
| 137 | return true; | ||
| 138 | } | ||
| 139 | |||
| 140 | 14 | bool KurpiakovAShellsortMPI::PostProcessingImpl() { | |
| 141 | 14 | GetOutput() = data_; | |
| 142 | 14 | return true; | |
| 143 | } | ||
| 144 | |||
| 145 | } // namespace kurpiakov_a_shellsort | ||
| 146 |