Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
memory.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
19#include "shambase/string.hpp"
21#include "shambackends/sycl.hpp"
24#include "shambackends/vec.hpp"
25#include <fmt/base.h>
26
27namespace shamalgs::memory {
28
38 template<class T>
39 T extract_element(sycl::queue &q, sycl::buffer<T> &buf, u32 idx);
40
48 template<class T>
49 sycl::buffer<T> vec_to_buf(const std::vector<T> &buf);
50
59 template<class T>
60 std::vector<T> buf_to_vec(sycl::buffer<T> &buf, u32 len);
61
69 template<class T>
70 inline void move_buffer_on_queue(sycl::queue &q, sycl::buffer<T> &buf) {
71 sycl::buffer<T> tmp(1);
72 q.submit([&](sycl::handler &cgh) {
73 sycl::accessor a{buf, cgh, sycl::read_write};
74 sycl::accessor b{tmp, cgh, sycl::write_only, sycl::no_init};
75
76 cgh.single_task([=]() {
77 b[0] = a[0];
78 });
79 });
80 }
81
90 template<class T>
91 inline void buf_fill_discard(sycl::queue &q, sycl::buffer<T> &buf, T value) {
92 StackEntry stack_loc{};
93 q.submit([&, value](sycl::handler &cgh) {
94 sycl::accessor acc{buf, cgh, sycl::write_only, sycl::no_init};
95
96 shambase::parallel_for(cgh, buf.size(), "buff_fill_discard", [=](u64 id_a) {
97 acc[id_a] = value;
98 });
99 });
100 }
101
112 template<class T>
113 inline void print_buf(sycl::buffer<T> &buf, u32 len, u32 column_count, std::string_view fmt) {
114
115 sycl::host_accessor acc{buf, sycl::read_only};
116
117 std::string accum;
118
119 for (u32 i = 0; i < len; i++) {
120
121 if (i % column_count == 0) {
122 if (i == 0) {
123 accum += sham::format("{:8} : ", i);
124 } else {
125 accum += sham::format("\n{:8} : ", i);
126 }
127 }
128
129 accum += sham::vformat(fmt, fmt::make_format_args(acc[i]));
130 }
131
132 logger::raw_ln(accum);
133 }
134
135 template<class T>
136 void copybuf_discard(sycl::queue &q, sycl::buffer<T> &source, sycl::buffer<T> &dest, u32 cnt) {
137 q.submit([&](sycl::handler &cgh) {
138 sycl::accessor src{source, cgh, sycl::read_only};
139 sycl::accessor dst{dest, cgh, sycl::write_only, sycl::no_init};
140
141 shambase::parallel_for(cgh, cnt, "copybuf_discard", [=](u64 i) {
142 dst[i] = src[i];
143 });
144 });
145 }
146
147 template<class T>
148 void write_with_offset_into(
149 sycl::queue &q,
150 sycl::buffer<T> &buf_ctn,
151 sycl::buffer<T> &buf_in,
152 u32 offset,
153 u32 element_count) {
154 q.submit([&](sycl::handler &cgh) {
155 sycl::accessor source{buf_in, cgh, sycl::read_only};
156 sycl::accessor dest{buf_ctn, cgh, sycl::write_only, sycl::no_init};
157 u32 off = offset;
158 cgh.parallel_for(sycl::range{element_count}, [=](sycl::item<1> item) {
159 dest[item.get_id(0) + off] = source[item];
160 });
161 });
162 }
163
175 template<class T>
176 void write_with_offset_into(
178 sham::DeviceBuffer<T> &buf_ctn,
179 sham::DeviceBuffer<T> &buf_in,
180 u32 offset,
181 u32 element_count) {
182
183 sham::EventList depends_list;
184 auto source = buf_in.get_read_access(depends_list);
185 auto dest = buf_ctn.get_write_access(depends_list);
186
187 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
188 u32 off = offset;
189 cgh.parallel_for(sycl::range{element_count}, [=](sycl::item<1> item) {
190 dest[item.get_id(0) + off] = source[item];
191 });
192 });
193
194 buf_in.complete_event_state(e);
195 buf_ctn.complete_event_state(e);
196 }
197
208 template<class T>
209 void write_with_offset_into(
211 sycl::buffer<T> &buf_ctn,
212 sham::DeviceBuffer<T> &buf_in,
213 u32 offset,
214 u32 element_count) {
215
216 sham::EventList depends_list;
217 auto source = buf_in.get_read_access(depends_list);
218
219 auto e = q.submit(depends_list, [&](sycl::handler &cgh) {
220 sycl::accessor dest{buf_ctn, cgh, sycl::write_only, sycl::no_init};
221 u32 off = offset;
222 cgh.parallel_for(sycl::range{element_count}, [=](sycl::item<1> item) {
223 dest[item.get_id(0) + off] = source[item];
224 });
225 });
226
227 buf_in.complete_event_state(e);
228 }
229
230 template<class T>
231 void write_with_offset_into(
232 sycl::queue &q, sycl::buffer<T> &buf_ctn, T val, u32 offset, u32 element_count) {
233 q.submit([&, val](sycl::handler &cgh) {
234 sycl::accessor dest{buf_ctn, cgh, sycl::write_only, sycl::no_init};
235 u32 off = offset;
236 cgh.parallel_for(sycl::range{element_count}, [=](sycl::item<1> item) {
237 dest[item.get_id(0) + off] = val;
238 });
239 });
240 }
241
242 template<class T>
243 std::unique_ptr<sycl::buffer<T>> duplicate(
244 sycl::queue &q, const std::unique_ptr<sycl::buffer<T>> &buf_in) {
245 if (buf_in) {
246 auto buf = std::make_unique<sycl::buffer<T>>(buf_in->size());
247 copybuf_discard(q, *buf_in, *buf, buf_in->size());
248 return std::move(buf);
249 }
250 return {};
251 }
252
253 template<class T>
254 sycl::buffer<T> vector_to_buf(sycl::queue &q, std::vector<T> &&vec) {
255
256 u32 cnt = vec.size();
257 sycl::buffer<T> ret(cnt);
258
259 sycl::buffer<T> alias(vec.data(), cnt);
260
261 shamalgs::memory::copybuf_discard(q, alias, ret, cnt);
262
263// HIPSYCL segfault otherwise because looks like the destructor of the sycl buffer
264// doesn't wait for the end of the queue resulting in out of bound access
265#ifdef SYCL_COMP_ACPP
266 q.wait();
267#endif
268
269 return std::move(ret);
270 }
271
280 template<class T>
281 sycl::buffer<T> vector_to_buf(sycl::queue &q, std::vector<T> &vec) {
282
283 u32 cnt = vec.size();
284 sycl::buffer<T> ret(cnt);
285
286 sycl::buffer<T> alias(vec.data(), cnt);
287
288 shamalgs::memory::copybuf_discard(q, alias, ret, cnt);
289
290// HIPSYCL segfault otherwise because looks like the destructor of the sycl buffer
291// doesn't wait for the end of the queue resulting in out of bound access
292#ifdef SYCL_COMP_ACPP
293 q.wait();
294#endif
295
296 return std::move(ret);
297 }
298
299} // namespace shamalgs::memory
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 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
memory manipulation algorithms
void buf_fill_discard(sycl::queue &q, sycl::buffer< T > &buf, T value)
Fill a buffer with a given value (sycl::no_init mode).
Definition memory.hpp:91
T extract_element(sycl::queue &q, sycl::buffer< T > &buf, u32 idx)
extract a value of a buffer
Definition memory.cpp:24
void print_buf(sycl::buffer< T > &buf, u32 len, u32 column_count, std::string_view fmt)
Print the content of a sycl::buffer.
Definition memory.hpp:113
sycl::buffer< T > vec_to_buf(const std::vector< T > &buf)
Convert a std::vector to a sycl::buffer.
Definition memory.cpp:29
void move_buffer_on_queue(sycl::queue &q, sycl::buffer< T > &buf)
enqueue a do nothing kernel to force the buffer to move
Definition memory.hpp:70
std::vector< T > buf_to_vec(sycl::buffer< T > &buf, u32 len)
Convert a sycl::buffer to a std::vector.
Definition memory.cpp:34
void raw_ln(Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:89
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.