Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
sort_by_keys.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
16
23#include "shamcomm/logs.hpp"
24#include <algorithm>
25#include <vector>
26
27namespace shamalgs::primitives::details {
28
31 template<class Tkey, class Tval>
33 sham::DeviceBuffer<Tkey> &buf_key, sham::DeviceBuffer<Tval> &buf_values, u32 len) {
34
35 std::vector<Tkey> key_stdvec = buf_key.copy_to_stdvec();
36 std::vector<Tval> val_stdvec = buf_values.copy_to_stdvec();
37
38 std::vector<Tkey> key_sub(key_stdvec.begin(), key_stdvec.begin() + len);
39 std::vector<Tval> val_sub(val_stdvec.begin(), val_stdvec.begin() + len);
40
42
43 std::copy(key_sub.begin(), key_sub.end(), key_stdvec.begin());
44 std::copy(val_sub.begin(), val_sub.end(), val_stdvec.begin());
45
46 buf_key.copy_from_stdvec(key_stdvec);
47 buf_values.copy_from_stdvec(val_stdvec);
48 }
49
50} // namespace shamalgs::primitives::details
51
52namespace shamalgs::primitives {
53
55 namespace impl {
56
58 struct StdSort {
59 static constexpr std::string_view variant_type_name = "std_sort";
60 };
61
64 static constexpr std::string_view variant_type_name = "batcher_odd_even_host_serial";
65 };
66
69 static constexpr std::string_view variant_type_name = "batcher_odd_even";
70 };
71
73 sort_by_keys_impl{[](const sham::DeviceScheduler_ptr &, auto &self) {
74 self.set(StdSort{});
75 }};
76
78 std::vector<std::string> get_default_impl_list_sort_by_keys() {
79 return sort_by_keys_impl.get_default_config_list();
80 }
81
84 return sort_by_keys_impl.get_current_config();
85 }
86
88 bool is_impl_set_sort_by_keys() { return sort_by_keys_impl.is_set(); }
89
91 void set_impl_sort_by_keys(const std::string &impl) {
92 shamlog_info_ln("algs", "setting sort by keys implementation to impl :", impl);
93 sort_by_keys_impl.set(impl);
94 }
95
97 void autoselect_impl_sort_by_keys(const sham::DeviceScheduler_ptr &dev_sched) {
98 sort_by_keys_impl.autoselect(dev_sched);
99 shamlog_info_ln(
100 "algs",
101 "defaulting sort by keys implementation to impl :",
103 }
104
105 } // namespace impl
106
107 template<class Tkey, class Tval>
109 sham::DeviceBuffer<Tkey> &buf_key, sham::DeviceBuffer<Tval> &buf_values, u32 len) {
110
111 if (!impl::sort_by_keys_impl.is_set()) {
113 }
114
115 std::visit(
117 [&](impl::StdSort) {
118 device::details::sort_by_keys_std_sort(buf_key, buf_values, len);
119 },
122 },
125 buf_key.get_dev_scheduler_ptr(), buf_key, buf_values, len);
126 },
127 },
128 impl::sort_by_keys_impl.get());
129 }
130
131 template void sort_by_keys(
132 sham::DeviceBuffer<u32> &buf_key, sham::DeviceBuffer<u32> &buf_values, u32 len);
133
134 template void sort_by_keys(
135 sham::DeviceBuffer<u64> &buf_key, sham::DeviceBuffer<u32> &buf_values, u32 len);
136
137 template void sort_by_keys(
138 sham::DeviceBuffer<f64> &buf_key, sham::DeviceBuffer<f64> &buf_values, u32 len);
139
140 template void sort_by_keys(
141 sham::DeviceBuffer<f32> &buf_key, sham::DeviceBuffer<f32> &buf_values, u32 len);
142
143} // namespace shamalgs::primitives
Generic std::variant-based implementation selector.
std::uint32_t u32
32 bit unsigned integer
Batcher odd-even mergesort, native for any length.
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.
Drop-in replacement for the hand-rolled "global variable + enum + name mapping.
This header file contains utility functions related to exception handling in the code.
void sort_by_key_batcher_odd_even_host_reference(std::vector< Tkey > &keys, std::vector< Tval > &values)
Host reference of sort_by_key_batcher_odd_even.
void sort_by_key_batcher_odd_even(const sham::DeviceScheduler_ptr &sched, sham::DeviceBuffer< Tkey > &buf_key, sham::DeviceBuffer< Tval > &buf_values, u32 len)
Sort key-value pairs of any length using a Batcher odd-even merge network.
namespace to control implementation behavior
std::vector< std::string > get_default_impl_list_sort_by_keys()
Get list of available sort by keys implementations, as config json strings.
std::string get_current_impl_sort_by_keys()
Get the current implementation for sort by keys, as a config json string.
void autoselect_impl_sort_by_keys(const sham::DeviceScheduler_ptr &dev_sched)
Select the default implementation for sort by keys.
void set_impl_sort_by_keys(const std::string &impl)
Set the implementation for sort by keys, from a config json string.
bool is_impl_set_sort_by_keys()
Check if an implementation has been selected for sort by keys.
namespace for primitive algorithm (e.g. sort, scan, reductions, ...)
void sort_by_keys(sham::DeviceBuffer< Tkey > &buf_key, sham::DeviceBuffer< Tval > &buf_values, u32 len)
Sort key-value pairs using USM buffers (general length).
void sort_by_keys_batcher_odd_even_host_serial(sham::DeviceBuffer< Tkey > &buf_key, sham::DeviceBuffer< Tval > &buf_values, u32 len)
Sort by keys algorithms.
std::sort based sort by keys implementation, shared by the sort by keys primitives
void sort_by_keys_std_sort(sham::DeviceBuffer< Tkey > &buf_key, sham::DeviceBuffer< Tval > &buf_values, u32 len)
Copy both buffers to host, std::sort the zipped key/value pairs, and copy back.
Copy the buffers to host, sort with Batcher's odd-even merge sort, and copy back.
Copy the buffers to host, sort with Batcher's odd-even merge sort, and copy back.
Copy the buffers to host, std::sort the zipped key/value pairs, and copy back.
Build an overload set out of several callables, for use with std::visit.