Nektar++
Loading...
Searching...
No Matches
SYCL_Host_API.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: SYCL_Host_API.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#pragma once
36
37#if defined(NEKTAR_ENABLE_SYCL)
39
40#include <vector>
41
42namespace Nektar
43{
44
45#if defined(SYCL_ENABLE_CUDA) && defined(__ADAPTIVECPP__)
46#define sycl_backend sycl::backend::cuda
47#elif defined(SYCL_ENABLE_CUDA) && defined(__DPCPP_COMPILER)
48#define sycl_backend sycl::backend::ext_oneapi_cuda
49#elif defined(SYCL_ENABLE_HIP) && defined(__ADAPTIVECPP__)
50#define sycl_backend sycl::backend::hip
51#elif defined(SYCL_ENABLE_HIP) && defined(__DPCPP_COMPILER)
52#define sycl_backend sycl::backend::ext_oneapi_hip
53#endif
54
55static void inline setSYCLDefaultExecutionDependency(
56 const unsigned int streamID, sycl::handler &cgh)
57{
58 // Set SYCL depedencies to reproduce CUDA/HIP default stream behavior.
59 if (streamID == 0)
60 {
61 // Tasks in the "default" queue depend on all "non-default" queue.
62 for (auto &item : SYCLQueue::GetAllEvents())
63 {
64 if (item.first != 0)
65 {
66 cgh.depends_on(item.second);
67 }
68 }
69 }
70 else
71 {
72 // Tasks in "non-default" queue depend on the "default" queue.
73 cgh.depends_on(SYCLQueue::GetEvent(0));
74 }
75}
76
77// The dependencies of setSYCLDefaultExecutionDependency, as the list of events
78// a oneMath or oneMKL call takes.
79static inline std::vector<sycl::event> getSYCLDefaultExecutionDependencies(
80 const unsigned int streamID)
81{
82 std::vector<sycl::event> dependencies;
83 if (streamID == 0)
84 {
85 for (auto &item : SYCLQueue::GetAllEvents())
86 {
87 if (item.first != 0)
88 {
89 dependencies.push_back(item.second);
90 }
91 }
92 }
93 else
94 {
95 dependencies.push_back(SYCLQueue::GetEvent(0));
96 }
97
98 return dependencies;
99}
100
101// Kernel launcher on a one-dimensional GPU grid with shared memory provision.
102// KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL. The last two
103// arguments of the KERNEL function MUST be of type unsigned char * and
104// sycl::nd_item<1>. The shared memory size must be specified in bytes. The
105// shared memory is declared as unsigned char* type. The shmemptr must then cast
106// to the appropriate type before use (e.g. auto ptr = (TData *)shmemptr).
107// clang-format off
108#define DEVICE_1DGRID_KERNEL_LAUNCHER(KERNEL, GRIDSIZE, BLOCKSIZE, SHMEMSIZE, \
109 STREAMID, ...) \
110 { \
111 sycl::queue &Q = SYCLQueue::GetInstance(STREAMID); \
112 const auto syclSHMEMSIZE = SHMEMSIZE; \
113 const auto syclSTREAMID = STREAMID; \
114 const auto args = std::make_tuple(__VA_ARGS__); \
115 sycl::event e = Q.submit([=](sycl::handler &cgh) { \
116 setSYCLDefaultExecutionDependency(syclSTREAMID, cgh); \
117 sycl::local_accessor<unsigned char, 1> shmem( \
118 sycl::range<1>(syclSHMEMSIZE), cgh); \
119 cgh.parallel_for( \
120 sycl::nd_range<1>(GRIDSIZE * BLOCKSIZE, BLOCKSIZE), \
121 [=](sycl::nd_item<1> item_ct1) SYCL_SUBGROUP_SIZE( \
122 NektarSpaces::Device::warpSize) { \
123 auto shmemptr = shmem \
124 .template get_multi_ptr<sycl::access::decorated::yes>()\
125 .get(); \
126 std::apply( \
127 [&](auto &&...args) { \
128 KERNEL(std::forward<decltype(args)>(args)..., \
129 shmemptr, item_ct1); \
130 }, \
131 args); \
132 }); \
133 }); \
134 SYCLQueue::SetEvent(STREAMID, e); \
135 }
136// clang-format on
137
138// Kernel launcher on a two-dimensional GPU grid with shared memory provision.
139// KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL. The last two
140// arguments of the KERNEL function MUST be of type unsigned char * and
141// sycl::nd_item<2>. The shared memory size must be specified in bytes. The
142// shared memory is declared as unsigned char* type. The shmemptr must then cast
143// to the appropriate type before use (e.g. auto ptr = (TData *)shmemptr).
144// clang-format off
145#define DEVICE_2DGRID_KERNEL_LAUNCHER(KERNEL, GRIDSIZEX, GRIDSIZEY, \
146 BLOCKSIZEX, BLOCKSIZEY, SHMEMSIZE, \
147 STREAMID, ...) \
148 { \
149 sycl::queue &Q = SYCLQueue::GetInstance(STREAMID); \
150 const auto syclSHMEMSIZE = SHMEMSIZE; \
151 const auto syclSTREAMID = STREAMID; \
152 const auto args = std::make_tuple(__VA_ARGS__); \
153 sycl::range<2> GRIDSIZE(GRIDSIZEY, GRIDSIZEX); \
154 sycl::range<2> BLOCKSIZE(BLOCKSIZEY, BLOCKSIZEX); \
155 sycl::event e = Q.submit([=](sycl::handler &cgh) { \
156 setSYCLDefaultExecutionDependency(syclSTREAMID, cgh); \
157 sycl::local_accessor<unsigned char, 1> shmem( \
158 sycl::range<1>(syclSHMEMSIZE), cgh); \
159 cgh.parallel_for( \
160 sycl::nd_range<2>(GRIDSIZE * BLOCKSIZE, BLOCKSIZE), \
161 [=](sycl::nd_item<2> item_ct1) SYCL_SUBGROUP_SIZE( \
162 NektarSpaces::Device::warpSize) { \
163 auto shmemptr = shmem \
164 .template get_multi_ptr<sycl::access::decorated::yes>()\
165 .get(); \
166 std::apply( \
167 [&](auto &&...args) { \
168 KERNEL(std::forward<decltype(args)>(args)..., \
169 shmemptr, item_ct1); \
170 }, \
171 args); \
172 }); \
173 }); \
174 SYCLQueue::SetEvent(STREAMID, e); \
175 }
176
177// Kernel launcher on a three-dimensional GPU grid with shared memory provision.
178// KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL. The last two
179// arguments of the KERNEL function MUST be of type unsigned char * and
180// sycl::nd_item<3>. The shared memory size must be specified in bytes. The
181// shared memory is declared as unsigned char* type. The shmemptr must then cast
182// to the appropriate type before use (e.g. auto ptr = (TData *)shmemptr).
183#define DEVICE_3DGRID_KERNEL_LAUNCHER(KERNEL, GRIDSIZEX, GRIDSIZEY, GRIDSIZEZ, \
184 BLOCKSIZEX, BLOCKSIZEY, BLOCKSIZEZ, \
185 SHMEMSIZE, STREAMID, ...) \
186 { \
187 sycl::queue &Q = SYCLQueue::GetInstance(STREAMID); \
188 const auto syclSHMEMSIZE = SHMEMSIZE; \
189 const auto syclSTREAMID = STREAMID; \
190 const auto args = std::make_tuple(__VA_ARGS__); \
191 sycl::range<3> GRIDSIZE(GRIDSIZEZ, GRIDSIZEY, GRIDSIZEX); \
192 sycl::range<3> BLOCKSIZE(BLOCKSIZEZ, BLOCKSIZEY, BLOCKSIZEX); \
193 sycl::event e = Q.submit([=](sycl::handler &cgh) { \
194 setSYCLDefaultExecutionDependency(syclSTREAMID, cgh); \
195 sycl::local_accessor<unsigned char, 1> shmem( \
196 sycl::range<1>(syclSHMEMSIZE), cgh); \
197 cgh.parallel_for( \
198 sycl::nd_range<3>(GRIDSIZE * BLOCKSIZE, BLOCKSIZE), \
199 [=](sycl::nd_item<3> item_ct1) SYCL_SUBGROUP_SIZE( \
200 NektarSpaces::Device::warpSize) { \
201 auto shmemptr = shmem \
202 .template get_multi_ptr<sycl::access::decorated::yes>()\
203 .get(); \
204 std::apply( \
205 [&](auto &&...args) { \
206 KERNEL(std::forward<decltype(args)>(args)..., \
207 shmemptr, item_ct1); \
208 }, \
209 args); \
210 }); \
211 }); \
212 SYCLQueue::SetEvent(STREAMID, e); \
213 }
214// clang-format on
215
216// Kernel launcher on a one-dimensional GPU grid without shared memory
217// provision. KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL.
218// The last argument of the KERNEL function MUST be of type sycl::nd_item<1>.
219// clang-format off
220#define DEVICE_1DGRID_KERNEL_LAUNCHER_NOSHMEM(KERNEL, GRIDSIZE, BLOCKSIZE, \
221 STREAMID, ...) \
222 { \
223 sycl::queue &Q = SYCLQueue::GetInstance(STREAMID); \
224 const auto syclSTREAMID = STREAMID; \
225 const auto args = std::make_tuple(__VA_ARGS__); \
226 sycl::event e = Q.submit([=](sycl::handler &cgh) { \
227 setSYCLDefaultExecutionDependency(syclSTREAMID, cgh); \
228 cgh.parallel_for( \
229 sycl::nd_range<1>(GRIDSIZE * BLOCKSIZE, BLOCKSIZE), \
230 [=](sycl::nd_item<1> item_ct1) SYCL_SUBGROUP_SIZE( \
231 NektarSpaces::Device::warpSize) { \
232 std::apply( \
233 [&](auto &&...args) { \
234 KERNEL(std::forward<decltype(args)>(args)..., \
235 item_ct1); \
236 }, \
237 args); \
238 }); \
239 }); \
240 SYCLQueue::SetEvent(STREAMID, e); \
241 }
242// clang-format on
243
244// Kernel launcher on a two-dimensional GPU grid without shared memory
245// provision. KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL.
246// The last argument of the KERNEL function MUST be of type sycl::nd_item<2>.
247// clang-format off
248#define DEVICE_2DGRID_KERNEL_LAUNCHER_NOSHMEM( \
249 KERNEL, GRIDSIZEX, GRIDSIZEY, BLOCKSIZEX, BLOCKSIZEY, STREAMID, ...) \
250 { \
251 sycl::queue &Q = SYCLQueue::GetInstance(STREAMID); \
252 const auto syclSTREAMID = STREAMID; \
253 const auto args = std::make_tuple(__VA_ARGS__); \
254 sycl::range<2> GRIDSIZE(GRIDSIZEY, GRIDSIZEX); \
255 sycl::range<2> BLOCKSIZE(BLOCKSIZEY, BLOCKSIZEX); \
256 sycl::event e = Q.submit([=](sycl::handler &cgh) { \
257 setSYCLDefaultExecutionDependency(syclSTREAMID, cgh); \
258 cgh.parallel_for( \
259 sycl::nd_range<2>(GRIDSIZE * BLOCKSIZE, BLOCKSIZE), \
260 [=](sycl::nd_item<2> item_ct1) SYCL_SUBGROUP_SIZE( \
261 NektarSpaces::Device::warpSize) { \
262 std::apply( \
263 [&](auto &&...args) { \
264 KERNEL(std::forward<decltype(args)>(args)..., \
265 item_ct1); \
266 }, \
267 args); \
268 }); \
269 }); \
270 SYCLQueue::SetEvent(STREAMID, e); \
271 }
272// clang-format on
273
274// Kernel launcher on a three-dimensional GPU grid without shared memory
275// provision. KERNEL must be a kernel function decorated by NEK_DEVICE_KERNEL.
276// The last argument of the KERNEL function MUST be of type sycl::nd_item<3>.
277// clang-format off
278#define DEVICE_3DGRID_KERNEL_LAUNCHER_NOSHMEM( \
279 KERNEL, GRIDSIZEX, GRIDSIZEY, GRIDSIZEZ, BLOCKSIZEX, BLOCKSIZEY, \
280 BLOCKSIZEZ, STREAMID, ...) \
281 { \
282 sycl::queue &Q = SYCLQueue::GetInstance(STREAMID); \
283 const auto syclSTREAMID = STREAMID; \
284 const auto args = std::make_tuple(__VA_ARGS__); \
285 sycl::range<3> GRIDSIZE(GRIDSIZEZ, GRIDSIZEY, GRIDSIZEX); \
286 sycl::range<3> BLOCKSIZE(BLOCKSIZEZ, BLOCKSIZEY, BLOCKSIZEX); \
287 sycl::event e = Q.submit([=](sycl::handler &cgh) { \
288 setSYCLDefaultExecutionDependency(syclSTREAMID, cgh); \
289 cgh.parallel_for( \
290 sycl::nd_range<3>(GRIDSIZE * BLOCKSIZE, BLOCKSIZE), \
291 [=](sycl::nd_item<3> item_ct1) SYCL_SUBGROUP_SIZE( \
292 NektarSpaces::Device::warpSize) { \
293 std::apply( \
294 [&](auto &&...args) { \
295 KERNEL(std::forward<decltype(args)>(args)..., \
296 item_ct1); \
297 }, \
298 args); \
299 }); \
300 }); \
301 SYCLQueue::SetEvent(STREAMID, e); \
302 }
303// clang-format on
304
305} // namespace Nektar
306#endif
static sycl::event & GetEvent(unsigned int id)