| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "chernov_t_radix_sort/omp/include/ops_omp.hpp" | ||
| 2 | |||
| 3 | #include <omp.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "chernov_t_radix_sort/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace chernov_t_radix_sort { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | ChernovTRadixSortOMP::ChernovTRadixSortOMP(const InType &in) { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | GetInput() = in; |
| 17 | GetOutput() = {}; | ||
| 18 | 32 | } | |
| 19 | |||
| 20 | 32 | bool ChernovTRadixSortOMP::ValidationImpl() { | |
| 21 | 32 | return true; | |
| 22 | } | ||
| 23 | |||
| 24 | 32 | bool ChernovTRadixSortOMP::PreProcessingImpl() { | |
| 25 | 32 | GetOutput() = GetInput(); | |
| 26 | 32 | return true; | |
| 27 | } | ||
| 28 | |||
| 29 | constexpr int kBitsPerDigit = 8; | ||
| 30 | constexpr int kRadix = 1 << kBitsPerDigit; | ||
| 31 | constexpr uint32_t kSignMask = 0x80000000U; | ||
| 32 | |||
| 33 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | void ChernovTRadixSortOMP::RadixSortLSD(std::vector<int> &data) { |
| 34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (data.empty()) { |
| 35 | ✗ | return; | |
| 36 | } | ||
| 37 | |||
| 38 | const size_t n = data.size(); | ||
| 39 | 48 | std::vector<uint32_t> temp(n); | |
| 40 | |||
| 41 |
2/2✓ Branch 0 taken 164 times.
✓ Branch 1 taken 48 times.
|
212 | for (size_t i = 0; i < n; ++i) { |
| 42 | 164 | temp[i] = static_cast<uint32_t>(data[i]) ^ kSignMask; | |
| 43 | } | ||
| 44 | |||
| 45 |
1/4✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
48 | std::vector<uint32_t> buffer(n); |
| 46 | |||
| 47 |
2/2✓ Branch 0 taken 192 times.
✓ Branch 1 taken 48 times.
|
240 | for (int byte_index = 0; byte_index < 4; ++byte_index) { |
| 48 | 192 | const int shift = byte_index * kBitsPerDigit; | |
| 49 | |||
| 50 |
1/4✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
192 | std::vector<int> count(kRadix, 0); |
| 51 |
2/2✓ Branch 0 taken 656 times.
✓ Branch 1 taken 192 times.
|
848 | for (size_t i = 0; i < n; ++i) { |
| 52 | 656 | int digit = static_cast<int>((temp[i] >> shift) & 0xFFU); | |
| 53 | 656 | ++count[static_cast<size_t>(digit)]; | |
| 54 | } | ||
| 55 | |||
| 56 |
2/2✓ Branch 0 taken 48960 times.
✓ Branch 1 taken 192 times.
|
49152 | for (int i = 1; i < kRadix; ++i) { |
| 57 | 48960 | count[static_cast<size_t>(i)] += count[static_cast<size_t>(i - 1)]; | |
| 58 | } | ||
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 656 times.
✓ Branch 1 taken 192 times.
|
848 | for (size_t i = n; i-- > 0;) { |
| 61 | 656 | uint32_t val = temp[i]; | |
| 62 | 656 | int digit = static_cast<int>((val >> shift) & 0xFFU); | |
| 63 | 656 | buffer[static_cast<size_t>(--count[static_cast<size_t>(digit)])] = val; | |
| 64 | } | ||
| 65 | |||
| 66 | temp.swap(buffer); | ||
| 67 | } | ||
| 68 | |||
| 69 |
2/2✓ Branch 0 taken 164 times.
✓ Branch 1 taken 48 times.
|
212 | for (size_t i = 0; i < n; ++i) { |
| 70 | 164 | data[i] = static_cast<int>(temp[i] ^ kSignMask); | |
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | 24 | void ChernovTRadixSortOMP::SimpleMerge(const std::vector<int> &left, const std::vector<int> &right, | |
| 75 | std::vector<int> &result) { | ||
| 76 | 24 | result.resize(left.size() + right.size()); | |
| 77 | |||
| 78 | size_t i = 0; | ||
| 79 | size_t j = 0; | ||
| 80 | size_t k = 0; | ||
| 81 | |||
| 82 |
4/4✓ Branch 0 taken 104 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 96 times.
|
120 | while (i < left.size() && j < right.size()) { |
| 83 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 52 times.
|
96 | if (left[i] <= right[j]) { |
| 84 | 44 | result[k++] = left[i++]; | |
| 85 | } else { | ||
| 86 | 52 | result[k++] = right[j++]; | |
| 87 | } | ||
| 88 | } | ||
| 89 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 24 times.
|
52 | while (i < left.size()) { |
| 90 | 28 | result[k++] = left[i++]; | |
| 91 | } | ||
| 92 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 24 times.
|
64 | while (j < right.size()) { |
| 93 | 40 | result[k++] = right[j++]; | |
| 94 | } | ||
| 95 | 24 | } | |
| 96 | |||
| 97 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
|
32 | bool ChernovTRadixSortOMP::RunImpl() { |
| 98 | auto &data = GetOutput(); | ||
| 99 | |||
| 100 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
|
32 | if (data.size() <= 1) { |
| 101 | return true; | ||
| 102 | } | ||
| 103 | |||
| 104 | 24 | const size_t mid = data.size() / 2; | |
| 105 |
1/2✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
24 | std::vector<int> left(data.begin(), data.begin() + static_cast<std::ptrdiff_t>(mid)); |
| 106 |
1/4✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
24 | std::vector<int> right(data.begin() + static_cast<std::ptrdiff_t>(mid), data.end()); |
| 107 | |||
| 108 | 24 | #pragma omp parallel sections default(none) shared(left, right, data) | |
| 109 | { | ||
| 110 | #pragma omp section | ||
| 111 | { | ||
| 112 | RadixSortLSD(left); | ||
| 113 | } | ||
| 114 | |||
| 115 | #pragma omp section | ||
| 116 | { | ||
| 117 | RadixSortLSD(right); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | SimpleMerge(left, right, data); |
| 122 | |||
| 123 | return true; | ||
| 124 | } | ||
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
|
32 | bool ChernovTRadixSortOMP::PostProcessingImpl() { |
| 127 | 32 | return std::is_sorted(GetOutput().begin(), GetOutput().end()); | |
| 128 | } | ||
| 129 | |||
| 130 | } // namespace chernov_t_radix_sort | ||
| 131 |