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
22#include "shambackends/sycl.hpp"
24
30
40 template<class Tkey, class Tval>
42 sycl::queue &q, sycl::buffer<Tkey> &buf_key, sycl::buffer<Tval> &buf_values, u32 len) {
43 shamalgs::primitives::sort_by_key(q, buf_key, buf_values, len);
44 }
45
46 template<class Tkey, class Tval>
47 void sort_by_key(
48 const sham::DeviceScheduler_ptr &sched,
50 sham::DeviceBuffer<Tval> &buf_values,
51 u32 len) {
52 shamalgs::primitives::sort_by_key(sched, buf_key, buf_values, len);
53 }
54
64 template<class Fct>
65 inline sycl::buffer<typename std::invoke_result_t<Fct, u32>> gen_buffer_device(
66 sycl::queue &q, u32 len, Fct &&func) {
67
68 using ret_t = typename std::invoke_result_t<Fct, u32>;
69
70 sycl::buffer<ret_t> ret(len);
71
72 q.submit([&](sycl::handler &cgh) {
73 sycl::accessor out{ret, cgh, sycl::write_only, sycl::no_init};
74
75 cgh.parallel_for(sycl::range<1>(len), [=](sycl::item<1> item) {
76 out[item] = func(item.get_linear_id());
77 });
78 });
79
80 return std::move(ret);
81 }
82
95 template<class T>
96 sycl::buffer<T> index_remap(
97 sycl::queue &q, sycl::buffer<T> &source_buf, sycl::buffer<u32> &index_map, u32 len);
98
112 template<class T>
113 sycl::buffer<T> index_remap_nvar(
114 sycl::queue &q,
115 sycl::buffer<T> &source_buf,
116 sycl::buffer<u32> &index_map,
117 u32 len,
118 u32 nvar);
119
120 template<class T>
121 void index_remap(
122 const sham::DeviceScheduler_ptr &sched,
123 sham::DeviceBuffer<T> &source,
125 sham::DeviceBuffer<u32> &index_map,
126 u32 len);
127
128 template<class T>
129 void index_remap_nvar(
130 const sham::DeviceScheduler_ptr &sched,
131 sham::DeviceBuffer<T> &source,
133 sham::DeviceBuffer<u32> &index_map,
134 u32 len,
135 u32 nvar);
136
137 template<class T>
139 const sham::DeviceScheduler_ptr &sched_ptr,
140 sham::DeviceBuffer<T> &source,
141 sham::DeviceBuffer<u32> &index_map,
142 u32 len) {
143
144 sham::DeviceBuffer<T> dest(len, sched_ptr);
145 index_remap<T>(sched_ptr, source, dest, index_map, len);
146 return dest;
147 }
148
149 template<class T>
151 const sham::DeviceScheduler_ptr &sched_ptr,
152 sham::DeviceBuffer<T> &source,
153 sham::DeviceBuffer<u32> &index_map,
154 u32 len,
155 u32 nvar) {
156
157 sham::DeviceBuffer<T> dest(len * nvar, sched_ptr);
158 index_remap_nvar<T>(sched_ptr, source, dest, index_map, len, nvar);
159 return dest;
160 }
161
169 sycl::buffer<u32> gen_buffer_index(sycl::queue &q, u32 len);
170
171} // 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:65
void sort_by_key(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:41
void sort_by_key(sycl::queue &q, sycl::buffer< Tkey > &buf_key, sycl::buffer< Tval > &buf_values, u32 len)
Sort key-value pairs using sycl::buffers.
Sort by keys algorithms.