| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "chetverikova_e_shell_sort_simple_merge/omp/include/ops_omp.hpp" | ||
| 2 | |||
| 3 | #include <omp.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <iterator> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "chetverikova_e_shell_sort_simple_merge/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace chetverikova_e_shell_sort_simple_merge { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | ChetverikovaEShellSortSimpleMergeOMP::ChetverikovaEShellSortSimpleMergeOMP(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | GetInput() = in; |
| 18 | GetOutput().clear(); | ||
| 19 | 12 | } | |
| 20 | |||
| 21 | 12 | bool ChetverikovaEShellSortSimpleMergeOMP::ValidationImpl() { | |
| 22 | 12 | return !(GetInput().empty()); | |
| 23 | } | ||
| 24 | |||
| 25 | 12 | bool ChetverikovaEShellSortSimpleMergeOMP::PreProcessingImpl() { | |
| 26 | 12 | return true; | |
| 27 | } | ||
| 28 | |||
| 29 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | void ChetverikovaEShellSortSimpleMergeOMP::ShellSort(std::vector<int> &data) { |
| 30 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (data.empty()) { |
| 31 | return; | ||
| 32 | } | ||
| 33 | |||
| 34 | size_t n = data.size(); | ||
| 35 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 30 times.
|
77 | for (size_t gap = n / 2; gap > 0; gap /= 2) { |
| 36 |
2/2✓ Branch 0 taken 174 times.
✓ Branch 1 taken 47 times.
|
221 | for (size_t i = gap; i < n; i++) { |
| 37 | 174 | int temp = data[i]; | |
| 38 | size_t j = i; | ||
| 39 | |||
| 40 |
4/4✓ Branch 0 taken 221 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 111 times.
✓ Branch 3 taken 110 times.
|
285 | while (j >= gap && data[j - gap] > temp) { |
| 41 | 111 | data[j] = data[j - gap]; | |
| 42 | j -= gap; | ||
| 43 | } | ||
| 44 | |||
| 45 | 174 | data[j] = temp; | |
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | bool ChetverikovaEShellSortSimpleMergeOMP::RunImpl() { |
| 51 | const auto &input = GetInput(); | ||
| 52 | auto &output = GetOutput(); | ||
| 53 | |||
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (input.empty()) { |
| 55 | output.clear(); | ||
| 56 | ✗ | return true; | |
| 57 | } | ||
| 58 | |||
| 59 | 12 | output = input; | |
| 60 |
1/2✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | const std::size_t threads = std::max(1, omp_get_max_threads()); |
| 61 | const std::size_t counts_parts = std::min<std::size_t>(threads, input.size()); | ||
| 62 | 12 | const std::size_t len_part = input.size() / counts_parts; | |
| 63 | 12 | const std::size_t rem = input.size() % counts_parts; | |
| 64 | |||
| 65 | 12 | std::vector<size_t> ind_parts; | |
| 66 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | ind_parts.push_back(0); |
| 67 | |||
| 68 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 12 times.
|
42 | for (size_t i = 0; i < counts_parts; ++i) { |
| 69 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | ind_parts.push_back(ind_parts.back() + len_part); |
| 70 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 18 times.
|
30 | if (i < rem) { |
| 71 | 12 | ind_parts[i + 1]++; | |
| 72 | } | ||
| 73 | } | ||
| 74 |
1/4✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
12 | std::vector<std::vector<int>> local_buffers(counts_parts); |
| 75 | |||
| 76 | 12 | #pragma omp parallel for default(none) shared(input, output, ind_parts, local_buffers, counts_parts) schedule(static) | |
| 77 | for (size_t i = 0; i < counts_parts; ++i) { | ||
| 78 | auto left = static_cast<std::ptrdiff_t>(ind_parts[i]); | ||
| 79 | auto right = static_cast<std::ptrdiff_t>(ind_parts[i + 1]); | ||
| 80 | |||
| 81 | std::vector<int> temp(input.begin() + left, input.begin() + right); | ||
| 82 | ShellSort(temp); | ||
| 83 | local_buffers[i] = std::move(temp); | ||
| 84 | } | ||
| 85 | |||
| 86 | output = std::move(local_buffers[0]); | ||
| 87 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 12 times.
|
30 | for (size_t i = 1; i < counts_parts; ++i) { |
| 88 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | std::vector<int> merged; |
| 89 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | merged.reserve(output.size() + local_buffers[i].size()); |
| 90 | 18 | std::merge(output.begin(), output.end(), local_buffers[i].begin(), local_buffers[i].end(), | |
| 91 | std::back_inserter(merged)); | ||
| 92 | output = std::move(merged); | ||
| 93 | } | ||
| 94 | |||
| 95 | return true; | ||
| 96 | 12 | } | |
| 97 | |||
| 98 | 12 | bool ChetverikovaEShellSortSimpleMergeOMP::PostProcessingImpl() { | |
| 99 | 12 | return true; | |
| 100 | } | ||
| 101 | |||
| 102 | } // namespace chetverikova_e_shell_sort_simple_merge | ||
| 103 |