8 #pragma message("warning: Task-based OpenMP parallelism is not well supported on windows.")
9 #pragma message("warning: Thus, OpenMP-based downsampling is only partially parallelized on windows.")
18 template <
typename RandomAccessIterator,
typename Compare>
19 void merge_sort_omp_impl(RandomAccessIterator first, RandomAccessIterator last,
const Compare& comp) {
20 const size_t n = std::distance(first, last);
22 std::sort(first, last, comp);
26 auto center = first + n / 2;
35 std::inplace_merge(first, center, last, comp);
44 template <
typename RandomAccessIterator,
typename Compare>
45 void merge_sort_omp(RandomAccessIterator first, RandomAccessIterator last,
const Compare& comp,
int num_threads) {
47 #pragma omp parallel num_threads(num_threads)
49 #pragma omp single nowait
53 std::stable_sort(first, last, comp);
61 template <
typename RandomAccessIterator,
typename Compare>
62 void quick_sort_omp_impl(RandomAccessIterator first, RandomAccessIterator last,
const Compare& comp) {
63 const std::ptrdiff_t n = std::distance(first, last);
65 std::sort(first, last, comp);
69 const auto median3 = [&](
const auto& a,
const auto& b,
const auto& c,
const Compare& comp) {
70 return comp(a, b) ? (comp(b, c) ? b : (comp(a, c) ? c : a)) : (comp(a, c) ? a : (comp(b, c) ? c : b));
73 const int offset = n / 8;
74 const auto m1 = median3(*first, *(first + offset), *(first + offset * 2), comp);
75 const auto m2 = median3(*(first + offset * 3), *(first + offset * 4), *(first + offset * 5), comp);
76 const auto m3 = median3(*(first + offset * 6), *(first + offset * 7), *(last - 1), comp);
78 auto pivot = median3(m1, m2, m3, comp);
79 auto middle1 = std::partition(first, last, [&](
const auto& val) {
return comp(val, pivot); });
80 auto middle2 = std::partition(middle1, last, [&](
const auto& val) {
return !comp(pivot, val); });
94 template <
typename RandomAccessIterator,
typename Compare>
95 void quick_sort_omp(RandomAccessIterator first, RandomAccessIterator last,
const Compare& comp,
int num_threads) {
97 #pragma omp parallel num_threads(num_threads)
99 #pragma omp single nowait
103 std::sort(first, last, comp);
Definition: flat_container.hpp:12
void merge_sort_omp_impl(RandomAccessIterator first, RandomAccessIterator last, const Compare &comp)
Implementation of merge sort with OpenMP parallelism. Do not call this directly. Use merge_sort_omp i...
Definition: sort_omp.hpp:19
void quick_sort_omp_impl(RandomAccessIterator first, RandomAccessIterator last, const Compare &comp)
Implementation of quick sort with OpenMP parallelism. Do not call this directly. Use quick_sort_omp i...
Definition: sort_omp.hpp:62
void quick_sort_omp(RandomAccessIterator first, RandomAccessIterator last, const Compare &comp, int num_threads)
Quick sort with OpenMP parallelism.
Definition: sort_omp.hpp:95
void merge_sort_omp(RandomAccessIterator first, RandomAccessIterator last, const Compare &comp, int num_threads)
Merge sort with OpenMP parallelism.
Definition: sort_omp.hpp:45