24namespace shamalgs::primitives::details {
26 template<
class T,
class Comp>
27 inline void segmented_sort_in_place_local_insertion_sort(
28 sham::DeviceBuffer<T> &buf,
const sham::DeviceBuffer<u32> &offsets, Comp &&comp) {
32 size_t interact_count = buf.
get_size();
33 size_t offsets_count = offsets.
get_size();
34 size_t N = offsets_count - 1;
38 sham::MultiRef{offsets},
42 comp](
u32 gid,
const u32 *__restrict__ offsets, T *__restrict__ in_out_sorted) {
43 u32 start_index = offsets[gid];
44 u32 end_index = offsets[gid + 1];
50 if (start_index == end_index) {
65 template<
class T,
class Comp>
66 inline void segmented_sort_in_place_multi_std_sort(
67 sham::DeviceBuffer<T> &buf,
const sham::DeviceBuffer<u32> &offsets, Comp &&comp) {
71 size_t interact_count = buf.
get_size();
72 size_t offsets_count = offsets.
get_size();
73 size_t N = offsets_count - 1;
78#pragma omp parallel for
79 for (
u32 i = 0; i < N; ++i) {
80 u32 start_index = offsets_stdvec[i];
81 u32 end_index = offsets_stdvec[i + 1];
87 if (start_index == end_index) {
98 std::sort(buf_stdvec.begin() + start_index, buf_stdvec.begin() + end_index, comp);
113 static constexpr std::string_view variant_type_name =
"local_insertion_sort";
118 static constexpr std::string_view variant_type_name =
"multi_std_sort";
122 [](
const sham::DeviceScheduler_ptr &,
auto &self) {
128 return segmented_sort_in_place_impl.get_default_config_list();
133 return segmented_sort_in_place_impl.get_current_config();
142 "algs",
"setting segmented sort in place implementation to impl :",
impl);
143 segmented_sort_in_place_impl.set(
impl);
148 segmented_sort_in_place_impl.autoselect(dev_sched);
151 "defaulting segmented sort in place implementation to impl :",
157 template<
class T,
class Comp>
158 void internal_segmented_sort_in_place(
169 if (!impl::segmented_sort_in_place_impl.is_set()) {
176 details::segmented_sort_in_place_local_insertion_sort(buf, offsets, comp);
179 details::segmented_sort_in_place_multi_std_sort(buf, offsets, comp);
182 impl::segmented_sort_in_place_impl.get());
186 void segmented_sort_in_place<u32_2>(
187 sham::DeviceBuffer<u32_2> &buf,
const sham::DeviceBuffer<u32> &offsets) {
189 internal_segmented_sort_in_place(buf, offsets, [](u32_2 a, u32_2 b) {
190 return (a.x() == b.x()) ? (a.y() < b.y()) : (a.x() < b.x());
195 void segmented_sort_in_place<u32>(
196 sham::DeviceBuffer<u32> &buf,
const sham::DeviceBuffer<u32> &offsets) {
197 internal_segmented_sort_in_place(buf, offsets, [](
u32 a,
u32 b) {
Generic std::variant-based implementation selector.
std::uint32_t u32
32 bit unsigned integer
Shamrock assertion utility.
#define SHAM_ASSERT(x)
Shorthand for SHAM_ASSERT_NAMED without a message.
A buffer allocated in USM (Unified Shared Memory).
void copy_from_stdvec(const std::vector< T > &vec)
Copy the content of a std::vector into the buffer.
std::shared_ptr< DeviceScheduler > & get_dev_scheduler_ptr()
Gets the Device scheduler pointer corresponding to the held allocation.
std::vector< T > copy_to_stdvec() const
Copy the content of the buffer to a std::vector.
size_t get_size() const
Gets the number of elements in the buffer.
DeviceScheduler & get_dev_scheduler() const
Gets the Device scheduler corresponding to the held allocation.
DeviceQueue & get_queue(u32 id=0)
Get a reference to a DeviceQueue.
Drop-in replacement for the hand-rolled "global variable + enum + name mapping.
void kernel_call(sham::DeviceQueue &q, RefIn in, RefOut in_out, u32 n, Functor &&func, SourceLocation &&callsite=SourceLocation{})
Submit a kernel to a SYCL queue.
namespace to control implementation behavior
void set_impl_segmented_sort_in_place(const std::string &impl)
Set the implementation for segmented sort in place, from a config json string.
void autoselect_impl_segmented_sort_in_place(const sham::DeviceScheduler_ptr &dev_sched)
Select the default implementation for segmented sort in place.
std::string get_current_impl_segmented_sort_in_place()
Get the current implementation for segmented sort in place, as a config json string.
bool is_impl_set_segmented_sort_in_place()
Check if an implementation has been selected for segmented sort in place.
std::vector< std::string > get_default_impl_list_segmented_sort_in_place()
Get list of available segmented sort in place implementations, as config json strings.
namespace for primitive algorithm (e.g. sort, scan, reductions, ...)
void ptr_insert_sort(T *data, u32 start, u32 end, Comp &&comp)
Simple insertion sort on pointer range.
overloaded(Ts...) -> overloaded< Ts... >
Deduction guide so overloaded{lambda1, lambda2, ...} deduces Ts... from the lambdas.
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
Sort each segment locally with an insertion sort, one kernel work-item per segment.
Copy back to host and sort each segment with std::sort, parallelized over OpenMP.