| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "litvyakov_d_shell_sort/omp/include/ops_omp.hpp" | ||
| 2 | |||
| 3 | #include <omp.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdio> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "litvyakov_d_shell_sort/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace litvyakov_d_shell_sort { | ||
| 13 | |||
| 14 | 30 | void LitvyakovDShellSortOMP::BaseShellSort(std::vector<int>::iterator first, std::vector<int>::iterator last) { | |
| 15 |
2/2✓ Branch 0 taken 139 times.
✓ Branch 1 taken 30 times.
|
169 | for (std::ptrdiff_t dist = (last - first) / 2; dist > 0; dist /= 2) { |
| 16 |
2/2✓ Branch 0 taken 29735 times.
✓ Branch 1 taken 139 times.
|
29874 | for (auto i = first + dist; i != last; ++i) { |
| 17 |
4/4✓ Branch 0 taken 53031 times.
✓ Branch 1 taken 2354 times.
✓ Branch 2 taken 25650 times.
✓ Branch 3 taken 27381 times.
|
55385 | for (auto j = i; j - first >= dist && (*j < *(j - dist)); j -= dist) { |
| 18 | std::swap(*j, *(j - dist)); | ||
| 19 | } | ||
| 20 | } | ||
| 21 | } | ||
| 22 | 30 | } | |
| 23 | |||
| 24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | std::vector<std::size_t> LitvyakovDShellSortOMP::GetBounds(std::size_t n, std::size_t parts) { |
| 25 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
|
12 | parts = std::max<std::size_t>(1, std::min(parts, n)); |
| 26 | |||
| 27 | 12 | std::vector<std::size_t> bounds; | |
| 28 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | bounds.reserve(parts + 1); |
| 29 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | bounds.push_back(0); |
| 30 | |||
| 31 | 12 | const std::size_t base = n / parts; | |
| 32 | 12 | const std::size_t rem = n % parts; | |
| 33 | |||
| 34 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 12 times.
|
42 | for (std::size_t i = 0; i < parts; ++i) { |
| 35 |
1/4✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
30 | bounds.push_back(bounds.back() + base); |
| 36 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 25 times.
|
30 | if (i < rem) { |
| 37 | 5 | bounds[i + 1]++; | |
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | 12 | return bounds; | |
| 42 | } | ||
| 43 | |||
| 44 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | LitvyakovDShellSortOMP::LitvyakovDShellSortOMP(const InType &in) { |
| 45 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 46 |
1/2✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
|
16 | GetInput() = in; |
| 47 | 16 | GetOutput() = std::vector<int>(); | |
| 48 | 16 | } | |
| 49 | |||
| 50 | 16 | bool LitvyakovDShellSortOMP::ValidationImpl() { | |
| 51 | const InType &vec = GetInput(); | ||
| 52 | 16 | return !vec.empty(); | |
| 53 | } | ||
| 54 | |||
| 55 | 16 | bool LitvyakovDShellSortOMP::PreProcessingImpl() { | |
| 56 | 16 | GetOutput() = GetInput(); | |
| 57 | 16 | return true; | |
| 58 | } | ||
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
|
16 | bool LitvyakovDShellSortOMP::RunImpl() { |
| 61 | std::vector<int> &vec = GetOutput(); | ||
| 62 | |||
| 63 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
|
16 | if (vec.size() <= 1) { |
| 64 | return true; | ||
| 65 | } | ||
| 66 | |||
| 67 | 12 | const std::size_t threads = std::max(1, omp_get_max_threads()); | |
| 68 | const std::size_t parts_count = std::min<std::size_t>(threads, vec.size()); | ||
| 69 | 12 | const auto bounds = GetBounds(vec.size(), parts_count); | |
| 70 | 12 | int parts_count_t = static_cast<int>(parts_count); | |
| 71 | |||
| 72 | 12 | #pragma omp parallel for default(none) shared(vec, bounds, parts_count_t) schedule(static) | |
| 73 | for (int i = 0; i < parts_count_t; ++i) { | ||
| 74 | const std::size_t l = bounds[i]; | ||
| 75 | const std::size_t r = bounds[i + 1]; | ||
| 76 | BaseShellSort(vec.begin() + static_cast<std::ptrdiff_t>(l), vec.begin() + static_cast<std::ptrdiff_t>(r)); | ||
| 77 | } | ||
| 78 | |||
| 79 | // cppreference.com: | ||
| 80 | // void inplace_merge( BidirIt first, BidirIt middle, BidirIt last ), | ||
| 81 | // Merges two consecutive sorted ranges [first, middle) and [middle, last) into one sorted range [first, last). | ||
| 82 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 12 times.
|
30 | for (std::size_t i = 1; i < parts_count; ++i) { |
| 83 | 18 | std::inplace_merge(vec.begin(), vec.begin() + static_cast<std::ptrdiff_t>(bounds[i]), | |
| 84 | 18 | vec.begin() + static_cast<std::ptrdiff_t>(bounds[i + 1])); | |
| 85 | } | ||
| 86 | |||
| 87 | return true; | ||
| 88 | } | ||
| 89 | |||
| 90 | 16 | bool LitvyakovDShellSortOMP::PostProcessingImpl() { | |
| 91 | 16 | return true; | |
| 92 | } | ||
| 93 | |||
| 94 | } // namespace litvyakov_d_shell_sort | ||
| 95 |