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
101 template<class T>
102 void index_remap(
103 const sham::DeviceScheduler_ptr &sched,
104 sham::DeviceBuffer<T> &source,
106 sham::DeviceBuffer<u32> &index_map,
107 u32 len);
108
109 template<class T>
110 void index_remap_nvar(
111 const sham::DeviceScheduler_ptr &sched,
112 sham::DeviceBuffer<T> &source,
114 sham::DeviceBuffer<u32> &index_map,
115 u32 len,
116 u32 nvar);
117
118 template<class T>
120 const sham::DeviceScheduler_ptr &sched_ptr,
121 sham::DeviceBuffer<T> &source,
122 sham::DeviceBuffer<u32> &index_map,
123 u32 len) {
124
125 sham::DeviceBuffer<T> dest(len, sched_ptr);
126 index_remap<T>(sched_ptr, source, dest, index_map, len);
127 return dest;
128 }
129
130 template<class T>
131 sham::DeviceBuffer<T> index_remap_nvar(
132 const sham::DeviceScheduler_ptr &sched_ptr,
133 sham::DeviceBuffer<T> &source,
134 sham::DeviceBuffer<u32> &index_map,
135 u32 len,
136 u32 nvar) {
137
138 sham::DeviceBuffer<T> dest(len * nvar, sched_ptr);
139 index_remap_nvar<T>(sched_ptr, source, dest, index_map, len, nvar);
140 return dest;
141 }
142
150 sycl::buffer<u32> gen_buffer_index(sycl::queue &q, u32 len);
151
152} // 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< 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.