| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "timofeev_n_radix_batcher_sort/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <tbb/tbb.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <climits> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "timofeev_n_radix_batcher_sort/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace timofeev_n_radix_batcher_sort_threads { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | TimofeevNRadixBatcherTBB::TimofeevNRadixBatcherTBB(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | GetInput() = in; |
| 18 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | GetOutput() = in; |
| 19 | 12 | } | |
| 20 | |||
| 21 | ✗ | void TimofeevNRadixBatcherTBB::CompExch(int &a, int &b, int digit) { | |
| 22 | 552 | int b_r = b % (digit * 10) / digit; | |
| 23 | 552 | int a_r = a % (digit * 10) / digit; | |
| 24 |
2/4✓ Branch 0 taken 84 times.
✓ Branch 1 taken 468 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
552 | if (b_r < a_r) { |
| 25 | std::swap(a, b); | ||
| 26 | } | ||
| 27 | ✗ | } | |
| 28 | |||
| 29 | 56 | void TimofeevNRadixBatcherTBB::BubbleSort(std::vector<int> &arr, int digit, int left, int right) { | |
| 30 |
2/2✓ Branch 0 taken 232 times.
✓ Branch 1 taken 56 times.
|
288 | for (int i = left; i <= right; i++) { |
| 31 |
2/2✓ Branch 0 taken 552 times.
✓ Branch 1 taken 232 times.
|
784 | for (int j = 0; j + 1 < right - left; j++) { |
| 32 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 468 times.
|
552 | CompExch(arr[left + j], arr[left + j + 1], digit); |
| 33 | } | ||
| 34 | } | ||
| 35 | 56 | } | |
| 36 | |||
| 37 | ✗ | void TimofeevNRadixBatcherTBB::ComparR(int &a, int &b) { | |
| 38 |
2/4✓ Branch 0 taken 36 times.
✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
140 | if (a > b) { |
| 39 | std::swap(a, b); | ||
| 40 | } | ||
| 41 | ✗ | } | |
| 42 | |||
| 43 | 28 | void TimofeevNRadixBatcherTBB::OddEvenMerge(std::vector<int> &arr, int lft, int n) { | |
| 44 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (n <= 1) { |
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | 28 | int otstup = n / 2; | |
| 49 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 28 times.
|
132 | for (int i = 0; i < otstup; i += 1) { |
| 50 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 68 times.
|
104 | if (arr[lft + i] > arr[lft + otstup + i]) { |
| 51 | std::swap(arr[lft + i], arr[lft + otstup + i]); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 28 times.
|
76 | for (otstup = n / 4; otstup > 0; otstup /= 2) { |
| 56 | 48 | int h = otstup * 2; | |
| 57 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 48 times.
|
152 | for (int start = otstup; start + otstup < n; start += h) { |
| 58 |
2/2✓ Branch 0 taken 140 times.
✓ Branch 1 taken 104 times.
|
244 | for (int i = 0; i < otstup; i += 1) { |
| 59 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 104 times.
|
140 | ComparR(arr[lft + start + i], arr[lft + start + i + otstup]); |
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | ✗ | int TimofeevNRadixBatcherTBB::Loggo(int inputa) { | |
| 66 | int count = 0; | ||
| 67 | ✗ | while (inputa > 1) { | |
| 68 | ✗ | inputa /= 2; | |
| 69 | ✗ | count++; | |
| 70 | } | ||
| 71 | ✗ | return count; | |
| 72 | } | ||
| 73 | |||
| 74 | 12 | bool TimofeevNRadixBatcherTBB::ValidationImpl() { | |
| 75 | 12 | return GetInput().size() >= 2; | |
| 76 | } | ||
| 77 | |||
| 78 | 12 | bool TimofeevNRadixBatcherTBB::PreProcessingImpl() { | |
| 79 | 12 | return true; | |
| 80 | } | ||
| 81 | |||
| 82 | 12 | void TimofeevNRadixBatcherTBB::PrepAux(int &n, int &m, std::vector<int> &in, int &max_x, size_t &num_threads, | |
| 83 | size_t &n_thr) { | ||
| 84 | 12 | in = GetInput(); | |
| 85 | 12 | n = static_cast<int>(in.size()); | |
| 86 | 12 | m = n; | |
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
|
28 | while (n % 2 == 0) { |
| 89 | 16 | n /= 2; | |
| 90 | } | ||
| 91 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
|
12 | if (n > 1) { |
| 92 | n = static_cast<int>(in.size()); | ||
| 93 | int p = 1; | ||
| 94 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8 times.
|
28 | while (p < n) { |
| 95 | 20 | p *= 2; | |
| 96 | } | ||
| 97 | 8 | n = p; | |
| 98 | } else { | ||
| 99 | 4 | n = m; | |
| 100 | } | ||
| 101 | |||
| 102 | 12 | max_x = *(std::ranges::max_element(in.begin(), in.end())); | |
| 103 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
|
12 | if (n != m) { |
| 104 | 8 | in.resize(n, max_x); | |
| 105 | } | ||
| 106 | // tbb | ||
| 107 | 12 | n_thr = tbb::this_task_arena::max_concurrency(); | |
| 108 | 12 | num_threads = 1; | |
| 109 |
4/4✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 4 times.
|
32 | while (num_threads * 2 <= n_thr && n / num_threads >= 4) { |
| 110 | 20 | num_threads *= 2; | |
| 111 | } | ||
| 112 | 12 | } | |
| 113 | |||
| 114 | 12 | void TimofeevNRadixBatcherTBB::HandleTBB(size_t &num_threads, size_t &n_n, size_t &m_m, int &max_x, | |
| 115 | std::vector<int> &reff, std::vector<int> &r_in) { | ||
| 116 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | tbb::task_arena arena(static_cast<int>(num_threads)); |
| 117 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | arena.execute([&] { |
| 118 | 12 | size_t piece = 0; | |
| 119 | 52 | tbb::parallel_for(tbb::blocked_range<size_t>(0, num_threads), [&](const tbb::blocked_range<size_t> &range) { | |
| 120 | size_t t_n = range.begin(); | ||
| 121 | 40 | piece = n_n / num_threads; | |
| 122 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 40 times.
|
96 | for (int k = 1; k <= max_x; k *= 10) { |
| 123 | 56 | BubbleSort(r_in, k, static_cast<int>(piece * t_n), static_cast<int>((piece * t_n) + piece)); | |
| 124 | } | ||
| 125 | 40 | }); | |
| 126 | |||
| 127 | 12 | size_t c_p = piece * 2; | |
| 128 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 12 times.
|
32 | for (; c_p <= n_n; c_p *= 2) { |
| 129 | 20 | tbb::parallel_for(tbb::blocked_range<size_t>(0, n_n, c_p), [&](const tbb::blocked_range<size_t> &range) { | |
| 130 |
2/4✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
56 | for (size_t i = range.begin(); i < range.end(); i += c_p) { |
| 131 | 28 | OddEvenMerge(r_in, static_cast<int>(i), static_cast<int>(c_p)); | |
| 132 | } | ||
| 133 | }); | ||
| 134 | } | ||
| 135 | |||
| 136 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
20 | if (m_m != n_n && tbb::this_task_arena::current_thread_index() == 0) { |
| 137 | 8 | r_in.resize(m_m); | |
| 138 | } | ||
| 139 | |||
| 140 | 12 | tbb::parallel_for(tbb::blocked_range<size_t>(0, r_in.size()), [&](const tbb::blocked_range<size_t> &range) { | |
| 141 |
2/4✓ Branch 0 taken 96 times.
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
192 | for (size_t i = range.begin(); i < range.end(); ++i) { |
| 142 | 96 | reff[i] = r_in[i]; | |
| 143 | } | ||
| 144 | }); | ||
| 145 | 12 | }); | |
| 146 | 12 | } | |
| 147 | |||
| 148 | 12 | bool TimofeevNRadixBatcherTBB::RunImpl() { | |
| 149 | 12 | std::vector<int> in; | |
| 150 | 12 | int n = 0; | |
| 151 | 12 | int m = 0; | |
| 152 | 12 | int max_x = 0; | |
| 153 | 12 | size_t n_thr = 0; // = tbb::this_task_arena::max_concurrency(); | |
| 154 | 12 | size_t num_threads = 0; | |
| 155 | |||
| 156 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | PrepAux(n, m, in, max_x, num_threads, n_thr); |
| 157 | |||
| 158 | std::vector<int> &r_in = in; | ||
| 159 | 12 | size_t n_n = n; | |
| 160 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | size_t m_m = m; |
| 161 | std::vector<int> &reff = GetInput(); | ||
| 162 | |||
| 163 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | HandleTBB(num_threads, n_n, m_m, max_x, reff, r_in); |
| 164 | // tbb | ||
| 165 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | GetOutput() = reff; |
| 166 | 12 | return true; | |
| 167 | } | ||
| 168 | |||
| 169 | 12 | bool TimofeevNRadixBatcherTBB::PostProcessingImpl() { | |
| 170 | 12 | return true; | |
| 171 | } | ||
| 172 | |||
| 173 | } // namespace timofeev_n_radix_batcher_sort_threads | ||
| 174 |