Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
algorithm.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
18#include "shambase/memory.hpp"
22
23namespace shamalgs::algorithm {
24
25 sycl::buffer<u32> gen_buffer_index(sycl::queue &q, u32 len) {
26 return gen_buffer_device(q, len, [](u32 i) -> u32 {
27 return i;
28 });
29 }
30
31 template<class T>
32 sycl::buffer<T> index_remap(
33 sycl::queue &q, sycl::buffer<T> &buf, sycl::buffer<u32> &index_map, u32 len) {
34
35 sycl::buffer<T> ret(len);
36
37 q.submit([&](sycl::handler &cgh) {
38 sycl::accessor in{buf, cgh, sycl::read_only};
39 sycl::accessor out{ret, cgh, sycl::write_only, sycl::no_init};
40 sycl::accessor permut{index_map, cgh, sycl::read_only};
41
42 cgh.parallel_for(sycl::range<1>(len), [=](sycl::item<1> item) {
43 out[item] = in[permut[item]];
44 });
45 });
46
47 return std::move(ret);
48 }
49
50 template<class T>
51 void index_remap(
52 const sham::DeviceScheduler_ptr &sched_ptr,
55 sham::DeviceBuffer<u32> &index_map,
56 u32 len) {
57
58 sham::DeviceQueue &q = shambase::get_check_ref(sched_ptr).get_queue();
59
61
62 const T *in = source.get_read_access(el);
63 T *out = dest.get_write_access(el);
64 const u32 *permut = index_map.get_read_access(el);
65
66 auto e = q.submit(el, [&](sycl::handler &cgh) {
67 cgh.parallel_for(sycl::range<1>(len), [=](sycl::item<1> item) {
68 out[item] = in[permut[item]];
69 });
70 });
71
72 source.complete_event_state(e);
74 index_map.complete_event_state(e);
75 }
76
77 template<class T>
78 void index_remap_nvar(
79 const sham::DeviceScheduler_ptr &sched_ptr,
80 sham::DeviceBuffer<T> &source,
81 sham::DeviceBuffer<T> &dest,
82 sham::DeviceBuffer<u32> &index_map,
83 u32 len,
84 u32 nvar) {
85
86 sham::DeviceQueue &q = shambase::get_check_ref(sched_ptr).get_queue();
87
88 sham::EventList el;
89
90 const T *in = source.get_read_access(el);
91 T *out = dest.get_write_access(el);
92 const u32 *permut = index_map.get_read_access(el);
93
94 auto e = q.submit(el, [&](sycl::handler &cgh) {
95 u32 nvar_loc = nvar;
96
97 cgh.parallel_for(sycl::range<1>(len), [=](sycl::item<1> item) {
98 u32 in_id = permut[item] * nvar_loc;
99 u32 out_id = item.get_linear_id() * nvar_loc;
100
101 for (u32 a = 0; a < nvar_loc; a++) {
102 out[out_id + a] = in[in_id + a];
103 }
104 });
105 });
106
107 source.complete_event_state(e);
108 dest.complete_event_state(e);
109 index_map.complete_event_state(e);
110 }
111
112#define XMAC_TYPES \
113 X(f32) \
114 X(f32_2) \
115 X(f32_3) \
116 X(f32_4) \
117 X(f32_8) \
118 X(f32_16) \
119 X(f64) \
120 X(f64_2) \
121 X(f64_3) \
122 X(f64_4) \
123 X(f64_8) \
124 X(f64_16) \
125 X(u32) \
126 X(u64) \
127 X(u32_3) \
128 X(u64_3) \
129 X(i64_3) \
130 X(i64)
131
132#define X(_arg_) \
133 template sycl::buffer<_arg_> index_remap( \
134 sycl::queue &q, sycl::buffer<_arg_> &buf, sycl::buffer<u32> &index_map, u32 len); \
135 \
136 template void index_remap( \
137 const sham::DeviceScheduler_ptr &sched, \
138 sham::DeviceBuffer<_arg_> &source, \
139 sham::DeviceBuffer<_arg_> &dest, \
140 sham::DeviceBuffer<u32> &index_map, \
141 u32 len); \
142 \
143 template void index_remap_nvar( \
144 const sham::DeviceScheduler_ptr &sched, \
145 sham::DeviceBuffer<_arg_> &source, \
146 sham::DeviceBuffer<_arg_> &dest, \
147 sham::DeviceBuffer<u32> &index_map, \
148 u32 len, \
149 u32 nvar);
150
151 XMAC_TYPES
152
153#undef X
154
155} // namespace shamalgs::algorithm
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
void complete_event_state(sycl::event e) const
Complete the event state of the buffer.
T * get_write_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{})
Get a read-write pointer to the buffer's data.
const T * get_read_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{}) const
Get a read-only pointer to the buffer's data.
A SYCL queue associated with a device and a context.
sycl::event submit(Fct &&fct)
Submits a kernel to the SYCL queue.
Class to manage a list of SYCL events.
Definition EventList.hpp:32
main include file for the shamalgs algorithms
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
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:112