Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
algorithm.hpp
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
10#pragma once
11
18
22#include "shambackends/sycl.hpp"
24
30
42 template<class Tkey, class Tval>
44 sycl::queue &q, sycl::buffer<Tkey> &buf_key, sycl::buffer<Tval> &buf_values, u32 len) {
45 shamalgs::primitives::sort_by_key_pow2_len(q, buf_key, buf_values, len);
46 }
47
48 template<class Tkey, class Tval>
50 const sham::DeviceScheduler_ptr &sched,
52 sham::DeviceBuffer<Tval> &buf_values,
53 u32 len) {
54 shamalgs::primitives::sort_by_key_pow2_len(sched, buf_key, buf_values, len);
55 }
56
66 template<class Fct>
67 inline sycl::buffer<typename std::invoke_result_t<Fct, u32>> gen_buffer_device(
68 sycl::queue &q, u32 len, Fct &&func) {
69
70 using ret_t = typename std::invoke_result_t<Fct, u32>;
71
72 sycl::buffer<ret_t> ret(len);
73
74 q.submit([&](sycl::handler &cgh) {
75 sycl::accessor out{ret, cgh, sycl::write_only, sycl::no_init};
76
77 cgh.parallel_for(sycl::range<1>(len), [=](sycl::item<1> item) {
78 out[item] = func(item.get_linear_id());
79 });
80 });
81
82 return std::move(ret);
83 }
84
97 template<class T>
98 sycl::buffer<T> index_remap(
99 sycl::queue &q, sycl::buffer<T> &source_buf, sycl::buffer<u32> &index_map, u32 len);
100
114 template<class T>
115 sycl::buffer<T> index_remap_nvar(
116 sycl::queue &q,
117 sycl::buffer<T> &source_buf,
118 sycl::buffer<u32> &index_map,
119 u32 len,
120 u32 nvar);
121
122 template<class T>
123 void index_remap(
124 const sham::DeviceScheduler_ptr &sched,
125 sham::DeviceBuffer<T> &source,
127 sham::DeviceBuffer<u32> &index_map,
128 u32 len);
129
130 template<class T>
131 void index_remap_nvar(
132 const sham::DeviceScheduler_ptr &sched,
133 sham::DeviceBuffer<T> &source,
135 sham::DeviceBuffer<u32> &index_map,
136 u32 len,
137 u32 nvar);
138
139 template<class T>
141 const sham::DeviceScheduler_ptr &sched_ptr,
142 sham::DeviceBuffer<T> &source,
143 sham::DeviceBuffer<u32> &index_map,
144 u32 len) {
145
146 sham::DeviceBuffer<T> dest(len, sched_ptr);
147 index_remap<T>(sched_ptr, source, dest, index_map, len);
148 return dest;
149 }
150
151 template<class T>
153 const sham::DeviceScheduler_ptr &sched_ptr,
154 sham::DeviceBuffer<T> &source,
155 sham::DeviceBuffer<u32> &index_map,
156 u32 len,
157 u32 nvar) {
158
159 sham::DeviceBuffer<T> dest(len * nvar, sched_ptr);
160 index_remap_nvar<T>(sched_ptr, source, dest, index_map, len, nvar);
161 return dest;
162 }
163
171 sycl::buffer<u32> gen_buffer_index(sycl::queue &q, u32 len);
172
173} // namespace shamalgs::algorithm
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
namespace to store algorithms implemented by shamalgs
Definition algorithm.hpp:29
sycl::buffer< T > index_remap(sycl::queue &q, sycl::buffer< T > &source_buf, sycl::buffer< u32 > &index_map, u32 len)
remap a buffer according to a given index map result[i] = result[index_map[i]]
Definition algorithm.cpp:32
sycl::buffer< u32 > gen_buffer_index(sycl::queue &q, u32 len)
generate a buffer such that for i in [0,len[, buf[i] = i
Definition algorithm.cpp:25
sycl::buffer< T > index_remap_nvar(sycl::queue &q, sycl::buffer< T > &source_buf, sycl::buffer< u32 > &index_map, u32 len, u32 nvar)
remap a buffer (with multiple variable per index) according to a given index map result[i] = result[i...
Definition algorithm.cpp:51
sycl::buffer< typename std::invoke_result_t< Fct, u32 > > gen_buffer_device(sycl::queue &q, u32 len, Fct &&func)
generate a buffer from a lambda expression based on the indexes
Definition algorithm.hpp:67
void sort_by_key_pow2_len(sycl::queue &q, sycl::buffer< Tkey > &buf_key, sycl::buffer< Tval > &buf_values, u32 len)
Sort the buffer according to the key order.
Definition algorithm.hpp:43
void sort_by_key_pow2_len(sycl::queue &q, sycl::buffer< Tkey > &buf_key, sycl::buffer< Tval > &buf_values, u32 len)
Sort key-value pairs using sycl::buffers (power-of-2 optimized).
Sort by keys algorithms.