Skip to content

Commit

Permalink
Thread API
Browse files Browse the repository at this point in the history
  • Loading branch information
GabTux committed Mar 23, 2024
1 parent 4145953 commit 1e0bfac
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
10 changes: 10 additions & 0 deletions include/ppqsort.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ namespace ppqsort {
impl::call_sort(std::forward<ExecutionPolicy>(policy), begin, end);
}

template <typename ExecutionPolicy, typename RandomIt>
void sort(ExecutionPolicy&& policy, RandomIt begin, RandomIt end, const int threads) {
impl::call_sort(std::forward<ExecutionPolicy>(policy), begin, end, threads);
}

template <typename RandomIt, typename Compare>
void sort(RandomIt begin, RandomIt end, Compare comp) {
impl::seq_ppqsort(begin, end, comp);
Expand All @@ -73,4 +78,9 @@ namespace ppqsort {
void sort(ExecutionPolicy&& policy, RandomIt begin, RandomIt end, Compare comp) {
impl::call_sort(std::forward<ExecutionPolicy>(policy), begin, end, comp);
}

template <typename ExecutionPolicy, typename RandomIt, typename Compare>
void sort(ExecutionPolicy&& policy, RandomIt begin, RandomIt end, Compare comp, const int threads) {
impl::call_sort(std::forward<ExecutionPolicy>(policy), begin, end, comp, threads);
}
}
17 changes: 13 additions & 4 deletions include/ppqsort/parallel/cpp/mainloop_par.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace ppqsort::impl {
namespace cpp {

struct ThreadPools {
ThreadPools() = default;
explicit ThreadPools(const int threads) : partition(threads), tasks(threads) {}

ThreadPool<> partition;
ThreadPool<> tasks;
};
Expand Down Expand Up @@ -127,24 +130,23 @@ namespace ppqsort::impl {
}
}


template <bool Force_branchless = false,
typename RandomIt,
typename Compare = std::less<typename std::iterator_traits<RandomIt>::value_type>,
bool Branchless = use_branchless<typename std::iterator_traits<RandomIt>::value_type, Compare>::value>
void par_ppqsort(RandomIt begin, RandomIt end, Compare comp = Compare()) {
void par_ppqsort(RandomIt begin, RandomIt end, Compare comp = Compare(),
int threads = static_cast<int>(std::jthread::hardware_concurrency())) {
if (begin == end)
return;
constexpr bool branchless = Force_branchless || Branchless;
int threads = static_cast<int>(std::jthread::hardware_concurrency());
auto size = end - begin;
if ((threads < 2) || (size < parameters::seq_threshold))
return seq_loop<RandomIt, Compare, branchless>(begin, end, comp, log2(size));

int seq_thr = (end - begin + 1) / threads / parameters::par_thr_div;
seq_thr = std::max(seq_thr, branchless ? parameters::insertion_threshold_primitive
: parameters::insertion_threshold);
cpp::ThreadPools threadpools;
cpp::ThreadPools threadpools(threads);
threadpools.tasks.push_task([begin, end, comp, seq_thr, threads, &threadpools] {
cpp::par_loop<RandomIt, Compare, branchless>(begin, end, comp,
log2(end - begin),
Expand All @@ -154,4 +156,11 @@ namespace ppqsort::impl {
threadpools.tasks.wait_and_stop();
threadpools.partition.wait_and_stop();
}

template <bool Force_branchless = false,
typename RandomIt,
typename Compare = std::less<typename std::iterator_traits<RandomIt>::value_type>>
void par_ppqsort(RandomIt begin, RandomIt end, int threads) {
return par_ppqsort<Force_branchless>(begin, end, Compare(), threads);
}
}
12 changes: 10 additions & 2 deletions include/ppqsort/parallel/openmp/mainloop_par.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ namespace ppqsort::impl {
typename RandomIt,
typename Compare = std::less<typename std::iterator_traits<RandomIt>::value_type>,
bool Branchless = use_branchless<typename std::iterator_traits<RandomIt>::value_type, Compare>::value>
void par_ppqsort(RandomIt begin, RandomIt end, Compare comp = Compare()) {
void par_ppqsort(RandomIt begin, RandomIt end,
Compare comp = Compare(), const int threads = omp_get_max_threads()) {
if (begin == end)
return;
constexpr bool branchless = Force_branchless || Branchless;
const int threads = omp_get_max_threads();
auto size = end - begin;
if ((threads < 2) || (size < parameters::seq_threshold))
return seq_loop<RandomIt, Compare, branchless>(begin, end, comp, log2(size));
Expand All @@ -138,4 +138,12 @@ namespace ppqsort::impl {
}
}
}

template <bool Force_branchless = false,
typename RandomIt,
typename Compare = std::less<typename std::iterator_traits<RandomIt>::value_type>>
void par_ppqsort(RandomIt begin, RandomIt end, const int threads) {
Compare comp = Compare();
par_ppqsort<Force_branchless>(begin, end, comp, threads);
}
}

0 comments on commit 1e0bfac

Please sign in to comment.