Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
sycl_utils.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
20#include "shambase/integer.hpp"
22#include "shambackends/sycl.hpp"
23#include "shambackends/vec.hpp"
24#include "shamcomm/logs.hpp"
25#include <stdexcept>
26
27#ifdef SHAMROCK_USE_NVTX
28 #include <nvtx3/nvtx3.hpp>
29#endif
30
31namespace shambase {
32
39 inline std::string getDevice_type(const sycl::device &Device) {
40 auto DeviceType = Device.get_info<sycl::info::device::device_type>();
41 switch (DeviceType) {
42 case sycl::info::device_type::cpu : return "CPU";
43 case sycl::info::device_type::gpu : return "GPU";
44 case sycl::info::device_type::host : return "HOST";
45 case sycl::info::device_type::accelerator: return "ACCELERATOR";
46 default : return "UNKNOWN";
47 }
48 }
49
57 inline sycl::nd_range<1> make_range(u32 length, const u32 group_size = 32) {
58 u32 group_cnt = shambase::group_count(length, group_size);
59 u32 len = group_cnt * group_size;
60 return sycl::nd_range<1>{len, group_size};
61 }
62
63 enum ParallelForWrapMode { PARALLEL_FOR, PARALLEL_FOR_ROUND, ND_RANGE };
64
65#ifdef SHAMROCK_LOOP_DEFAULT_PARALLEL_FOR
66 constexpr ParallelForWrapMode default_loop_mode = PARALLEL_FOR;
67#endif
68
69#ifdef SHAMROCK_LOOP_DEFAULT_PARALLEL_FOR_ROUND
70 constexpr ParallelForWrapMode default_loop_mode = PARALLEL_FOR_ROUND;
71#endif
72
73#ifdef SHAMROCK_LOOP_DEFAULT_ND_RANGE
74 constexpr ParallelForWrapMode default_loop_mode = ND_RANGE;
75#endif
76
77 constexpr u32 default_gsize = SHAMROCK_LOOP_GSIZE;
78 constexpr u32 default_gsize_2d = 16;
79 constexpr u32 default_gsize_3d = 4;
80
81 template<
82 u32 group_size = default_gsize,
83 ParallelForWrapMode mode = default_loop_mode,
84 class LambdaKernel>
85 inline void parallel_for(sycl::handler &cgh, u32 length, const char *name, LambdaKernel &&ker) {
86
87#ifdef SHAMROCK_USE_NVTX
88 nvtxRangePush(name);
89#endif
90
91 shamlog_debug_sycl_ln("SYCL", sham::format("parallel_for {} N={}", name, length));
92
93 if constexpr (mode == PARALLEL_FOR) {
94
95 cgh.parallel_for(sycl::range<1>{length}, [=](sycl::item<1> id) {
96 ker(id.get_linear_id());
97 });
98
99 } else if constexpr (mode == PARALLEL_FOR_ROUND) {
100
101 u32 len = shambase::group_count(length, group_size) * group_size;
102
103 cgh.parallel_for(sycl::range<1>{len}, [=](sycl::item<1> id) {
104 u64 gid = id.get_linear_id();
105 if (gid >= length)
106 return;
107
108 ker(gid);
109 });
110
111 } else if constexpr (mode == ND_RANGE) {
112
113 cgh.parallel_for(make_range(length, group_size), [=](sycl::nd_item<1> id) {
114 u64 gid = id.get_global_linear_id();
115 if (gid >= length)
116 return;
117
118 ker(gid);
119 });
120
121 } else {
123 }
124
125#ifdef SHAMROCK_USE_NVTX
126 nvtxRangePop();
127#endif
128 }
129
130 template<
131 u32 group_size = default_gsize_2d,
132 ParallelForWrapMode mode = default_loop_mode,
133 class LambdaKernel>
134 inline void parallel_for_2d(
135 sycl::handler &cgh, u32 length_x, u32 length_y, const char *name, LambdaKernel &&ker) {
136
137#ifdef SHAMROCK_USE_NVTX
138 nvtxRangePush(name);
139#endif
140
141 shamlog_debug_sycl_ln(
142 "SYCL", sham::format("parallel_for {} N={} {}", name, length_x, length_y));
143
144 if constexpr (mode == PARALLEL_FOR) {
145
146 cgh.parallel_for(sycl::range<2>{length_x, length_y}, [=](sycl::item<2> id) {
147 ker(id.get_id(0), id.get_id(1));
148 });
149
150 } else if constexpr (mode == PARALLEL_FOR_ROUND) {
151
152 u32 len_x = shambase::group_count(length_x, group_size) * group_size;
153 u32 len_y = shambase::group_count(length_y, group_size) * group_size;
154
155 cgh.parallel_for(sycl::range<2>{len_x, len_y}, [=](sycl::item<2> id) {
156 if (id.get_id(0) >= length_x || id.get_id(1) >= length_y)
157 return;
158
159 ker(id.get_id(0), id.get_id(1));
160 });
161
162 } else if constexpr (mode == ND_RANGE) {
163
164 sycl::nd_range<1> rx = make_range(length_x, group_size);
165 sycl::nd_range<1> ry = make_range(length_y, group_size);
166
167 sycl::range<2> tmp_s{rx.get_global_range().size(), ry.get_global_range().size()};
168 sycl::range<2> tmp_g{rx.get_group_range().size(), ry.get_group_range().size()};
169
170 cgh.parallel_for(sycl::nd_range<2>{tmp_s, tmp_g}, [=](sycl::nd_item<2> id) {
171 if (id.get_global_id(0) >= length_x || id.get_global_id(1) >= length_y)
172 return;
173
174 ker(id.get_global_id(0), id.get_global_id(1));
175 });
176
177 } else {
179 }
180
181#ifdef SHAMROCK_USE_NVTX
182 nvtxRangePop();
183#endif
184 }
185
186 template<
187 u32 group_size = default_gsize_3d,
188 ParallelForWrapMode mode = default_loop_mode,
189 class LambdaKernel>
190 inline void parallel_for_3d(
191 sycl::handler &cgh,
192 u32 length_x,
193 u32 length_y,
194 u32 length_z,
195 const char *name,
196 LambdaKernel &&ker) {
197
198#ifdef SHAMROCK_USE_NVTX
199 nvtxRangePush(name);
200#endif
201
202 shamlog_debug_sycl_ln(
203 "SYCL", sham::format("parallel_for {} N={} {} {}", name, length_x, length_y, length_z));
204
205 if constexpr (mode == PARALLEL_FOR) {
206
207 cgh.parallel_for(sycl::range<3>{length_x, length_y, length_z}, [=](sycl::item<3> id) {
208 ker(id.get_id(0), id.get_id(1), id.get_id(2));
209 });
210
211 } else if constexpr (mode == PARALLEL_FOR_ROUND) {
212
213 u32 len_x = shambase::group_count(length_x, group_size) * group_size;
214 u32 len_y = shambase::group_count(length_y, group_size) * group_size;
215 u32 len_z = shambase::group_count(length_z, group_size) * group_size;
216
217 cgh.parallel_for(sycl::range<3>{len_x, len_y, len_z}, [=](sycl::item<3> id) {
218 if (id.get_id(0) >= length_x || id.get_id(1) >= length_y
219 || id.get_id(2) >= length_z)
220 return;
221
222 ker(id.get_id(0), id.get_id(1), id.get_id(2));
223 });
224
225 } else if constexpr (mode == ND_RANGE) {
226
227 sycl::nd_range<1> rx = make_range(length_x, group_size);
228 sycl::nd_range<1> ry = make_range(length_y, group_size);
229 sycl::nd_range<1> rz = make_range(length_z, group_size);
230
231 sycl::range<3> tmp_s{
232 rx.get_global_range().size(),
233 ry.get_global_range().size(),
234 rz.get_global_range().size()};
235 sycl::range<3> tmp_g{
236 rx.get_group_range().size(),
237 ry.get_group_range().size(),
238 rz.get_group_range().size()};
239
240 cgh.parallel_for(sycl::nd_range<3>{tmp_s, tmp_g}, [=](sycl::nd_item<3> id) {
241 if (id.get_global_id(0) >= length_x || id.get_global_id(1) >= length_y
242 || id.get_global_id(2) >= length_z)
243 return;
244
245 ker(id.get_global_id(0), id.get_global_id(1), id.get_global_id(2));
246 });
247
248 } else {
250 }
251
252#ifdef SHAMROCK_USE_NVTX
253 nvtxRangePop();
254#endif
255 }
256
257 template<ParallelForWrapMode mode = default_loop_mode, class LambdaKernel>
258 inline void parallel_for_gsize(
259 sycl::handler &cgh, u32 length, u32 group_size, const char *name, LambdaKernel &&ker) {
260
261#ifdef SHAMROCK_USE_NVTX
262 nvtxRangePush(name);
263#endif
264
265 if constexpr (mode == PARALLEL_FOR) {
266
267 cgh.parallel_for(sycl::range<1>{length}, [=](sycl::item<1> id) {
268 ker(id.get_linear_id());
269 });
270
271 } else if constexpr (mode == PARALLEL_FOR_ROUND) {
272
273 u32 len = shambase::group_count(length, group_size) * group_size;
274
275 cgh.parallel_for(sycl::range<1>{len}, [=](sycl::item<1> id) {
276 u64 gid = id.get_linear_id();
277 if (gid >= length)
278 return;
279
280 ker(gid);
281 });
282
283 } else if constexpr (mode == ND_RANGE) {
284
285 cgh.parallel_for(make_range(length, group_size), [=](sycl::nd_item<1> id) {
286 u64 gid = id.get_global_linear_id();
287 if (gid >= length)
288 return;
289
290 ker(gid);
291 });
292
293 } else {
295 }
296
297#ifdef SHAMROCK_USE_NVTX
298 nvtxRangePop();
299#endif
300 }
301
302 inline void check_queue_state(sycl::queue &q, SourceLocation loc = SourceLocation()) {
303 shamlog_debug_sycl_ln("SYCL", "checking queue state", loc.format_one_line());
304 q.wait_and_throw();
305 shamlog_debug_sycl_ln("SYCL", "checking queue state : OK !");
306 }
307
308} // namespace shambase
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
This header file contains utility functions related to exception handling in the code.
namespace for basic c++ utilities
constexpr u32 group_count(u32 len, u32 group_size)
Calculates the number of groups based on the length and group size.
Definition integer.hpp:125
sycl::nd_range< 1 > make_range(u32 length, const u32 group_size=32)
Generate a sycl nd range out of a group size and length.
std::string getDevice_type(const sycl::device &Device)
Get the Device Type Name.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
This file contains the definition for the stacktrace related functionality.