Nektar++
Loading...
Searching...
No Matches
LoopExecutionSYCL.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: LoopExecutionSYCL.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)
38
39namespace Nektar
40{
41
42// Parallel for launchers.
43template <
44 typename ExecSpace, typename Functor,
45 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>, bool>
46 Enable = true>
47inline void parallel_for(const size_t begin, const size_t end,
48 const Functor &functor)
49{
50 const unsigned int streamID = internalLoopExecutionStreamID;
51 sycl::queue &Q = SYCLQueue::GetInstance(streamID);
52 sycl::event e = Q.submit([=](sycl::handler &cgh) {
53 setSYCLDefaultExecutionDependency(streamID, cgh);
54 cgh.parallel_for(sycl::range<1>(end - begin),
55 [=](sycl::id<1> indx) { functor(begin + indx); });
56 });
57 SYCLQueue::SetEvent(streamID, e);
58}
59
60// Sets *dest to val if init, and otherwise combines val into it by Reduction.
61// Set, it has a single writer. Combined into, it may be shared with the
62// reductions of other blocks of a Field on other queues, so takes one
63// device-scope atomic.
64template <bool init, typename Reduction>
65NEK_DEVICE_INLINE static void reduce_store(
66 typename Reduction::value_type *const dest,
67 const typename Reduction::value_type val)
68{
69 using TData = typename Reduction::value_type;
70 if constexpr (init)
71 {
72 *dest = val;
73 }
74 else if constexpr (std::is_same_v<Reduction, Nektar::ReduceSum<TData>>)
75 {
76 atomic_add<NektarSpaces::GlobalScope>(dest, val);
77 }
78 else if constexpr (std::is_same_v<Reduction, Nektar::ReduceMax<TData>>)
79 {
80 atomic_max<NektarSpaces::GlobalScope>(dest, val);
81 }
82 else if constexpr (std::is_same_v<Reduction, Nektar::ReduceMin<TData>>)
83 {
84 atomic_min<NektarSpaces::GlobalScope>(dest, val);
85 }
86}
87
88// Partial results of nred values per work-group: functor(i, vals) writes those
89// of index i to vals[0, nred), which Reduction combines. Result s of
90// work-group g is in buffer[s * gridSize + g], which it is set to if init and
91// is otherwise combined into.
92template <bool init, typename Reduction, unsigned int nred, typename Functor>
93void reduceNKernel(const unsigned int gridSize, const unsigned int blockSize,
94 const size_t begin, const size_t end,
95 typename Reduction::value_type *buffer,
96 const Functor &functor)
97{
98 using TData = typename Reduction::value_type;
99
100 constexpr TData identity = reduce_identity<Reduction>();
101
102 const unsigned int streamID = internalLoopExecutionStreamID;
103 sycl::queue &Q = SYCLQueue::GetInstance(streamID);
104 sycl::event e = Q.submit([=](sycl::handler &cgh) {
105 setSYCLDefaultExecutionDependency(streamID, cgh);
106 sycl::local_accessor<TData, 1> scratchpad(
107 sycl::range<1>(nred * blockSize), cgh);
108 cgh.parallel_for(
109 sycl::nd_range<1>(gridSize * blockSize, blockSize),
110 [=](sycl::nd_item<1> indx) SYCL_SUBGROUP_SIZE(
112 auto scratch =
113 scratchpad
114 .template get_multi_ptr<sycl::access::decorated::yes>()
115 .get();
116 const size_t lid = indx.get_local_id(0);
117 size_t gid = begin + indx.get_global_id(0);
118
119 TData tmp[nred];
120 for (unsigned int s = 0; s < nred; ++s)
121 {
122 tmp[s] = identity;
123 }
124 while (gid < end)
125 {
126 TData vals[nred];
127 functor(gid, vals);
128 for (unsigned int s = 0; s < nred; ++s)
129 {
130 tmp[s] = reduce_join<Reduction>(tmp[s], vals[s]);
131 }
132 gid += indx.get_global_range(0);
133 }
134 for (unsigned int s = 0; s < nred; ++s)
135 {
136 scratch[s * blockSize + lid] = tmp[s];
137 }
138
139#if defined(__ADAPTIVECPP__)
140 indx.barrier(sycl::access::fence_space::local_space);
141#else
142 sycl::group_barrier(indx.get_group(),
143 sycl::memory_scope::work_group);
144#endif
145
146 unsigned int n = NektarSpaces::Device::maximumBlockSize / 2;
147 while (n > 0)
148 {
149 if (blockSize > n && lid < n && lid + n < blockSize)
150 {
151 for (unsigned int s = 0; s < nred; ++s)
152 {
153 scratch[s * blockSize + lid] =
154 reduce_join<Reduction>(
155 scratch[s * blockSize + lid],
156 scratch[s * blockSize + lid + n]);
157 }
158 }
159#if defined(__ADAPTIVECPP__)
160 indx.barrier(sycl::access::fence_space::local_space);
161#else
162 sycl::group_barrier(indx.get_group(),
163 sycl::memory_scope::work_group);
164#endif
165 n /= 2;
166 }
167
168 if (lid == 0)
169 {
170 for (unsigned int s = 0; s < nred; ++s)
171 {
172 reduce_store<init, Reduction>(buffer + s * gridSize +
173 indx.get_group(0),
174 scratch[s * blockSize]);
175 }
176 }
177 });
178 });
179 SYCLQueue::SetEvent(streamID, e);
180}
181
182// Parallel reduction launchers without device-to-host copy.
183template <
184 typename ExecSpace, bool init, typename Reduction, typename Functor,
185 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>, bool>
186 Enable = true>
187inline void parallel_reduce(const size_t begin, const size_t end,
188 const Functor &functor,
189 typename Reduction::value_type *out)
190{
191 using TData = typename Reduction::value_type;
192
193 const unsigned int streamID = internalLoopExecutionStreamID;
194 const unsigned int blockSize = NektarSpaces::Device::reductionBlockSize();
195 const unsigned int gridSize =
197
198 if (internalMemoryBufferMap.find(streamID) == internalMemoryBufferMap.end())
199 {
200 deviceMalloc(&internalMemoryBufferMap[streamID],
201 internalReductionBufferSize, streamID);
202 }
203
204 TData *buffer = (TData *)internalMemoryBufferMap[streamID];
205 reduceNKernel<true, Reduction, 1>(
206 gridSize, blockSize, begin, end, buffer,
207 [=](const size_t i, TData *vals) { vals[0] = functor(i); });
208 reduceNKernel<init, Reduction, 1>(
209 1, gridSize, 0, gridSize, out,
210 [=](const size_t i, TData *vals) { vals[0] = buffer[i]; });
211}
212
213// Parallel reduction launchers with device-to-host copy.
214template <
215 typename ExecSpace, typename Reduction, typename Functor,
216 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>, bool>
217 Enable = true>
218inline void parallel_reduce(const size_t begin, const size_t end,
219 const Functor &functor,
220 typename Reduction::value_type &out)
221{
222 using TData = typename Reduction::value_type;
223
224 const unsigned int streamID = internalLoopExecutionStreamID;
225 if (internalHostBuffer == nullptr)
226 {
227 hostMallocPinned(&internalHostBuffer, internalMaxDataSizeByte);
228 deviceMalloc(&internalDeviceBuffer, internalMaxDataSizeByte, streamID);
229 }
230
231 parallel_reduce<ExecSpace, true, Reduction>(begin, end, functor,
232 (TData *)internalDeviceBuffer);
233
234 deviceMemcpy<eDeviceToHost>(internalHostBuffer, internalDeviceBuffer,
235 sizeof(TData), streamID);
236
237 out = *(TData *)internalHostBuffer;
238}
239
240// Parallel reduction of nred values without device-to-host copy:
241// functor(i, vals) writes those of index i to vals[0, nred), which Reduction
242// combines into out[0, nred), together with the values already there unless
243// init. At most internalReductionMaxValues values, the partial results the
244// reduction scratch holds.
245template <
246 typename ExecSpace, bool init, typename Reduction, unsigned int nred,
247 typename Functor,
248 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>, bool>
249 Enable = true>
250inline void parallel_reduce(const size_t begin, const size_t end,
251 const Functor &functor,
252 typename Reduction::value_type *out)
253{
254 using TData = typename Reduction::value_type;
255
256 static_assert(nred <= internalReductionMaxValues,
257 "parallel_reduce: too many values.");
258
259 const unsigned int streamID = internalLoopExecutionStreamID;
260 const unsigned int blockSize = NektarSpaces::Device::reductionBlockSize();
261 const unsigned int gridSize =
263
264 ASSERTL0(nred * gridSize * sizeof(TData) <= internalReductionBufferSize,
265 "Too many values for the reduction buffer.");
266
267 if (internalMemoryBufferMap.find(streamID) == internalMemoryBufferMap.end())
268 {
269 deviceMalloc(&internalMemoryBufferMap[streamID],
270 internalReductionBufferSize, streamID);
271 }
272
273 TData *buffer = (TData *)internalMemoryBufferMap[streamID];
274 reduceNKernel<true, Reduction, nred>(gridSize, blockSize, begin, end,
275 buffer, functor);
276 reduceNKernel<init, Reduction, nred>(
277 1, blockSize, 0, gridSize, out, [=](const size_t i, TData *vals) {
278 for (unsigned int s = 0; s < nred; ++s)
279 {
280 vals[s] = buffer[s * gridSize + i];
281 }
282 });
283}
284
285} // namespace Nektar
286
287#endif
#define SYCL_SUBGROUP_SIZE(x)
Definition Backends.hpp:170
#define NEK_DEVICE_INLINE
Definition Backends.hpp:283
#define ASSERTL0(condition, msg)
static cgsize_t gid(int i, int j, int k)
static sycl::queue & GetInstance(unsigned int id)
Definition SYCLQueue.hpp:55
static void SetEvent(unsigned int id, sycl::event &e)
array buffer
Definition GsLib.hpp:81
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT TData *NEK_RESTRICT out
void parallel_for(const size_t begin, const size_t end, const Functor &functor)
unsigned int internalLoopExecutionStreamID
void deviceMalloc(TData **src, const size_t size, const unsigned int streamID, const MemAllocType memAllocType=eHostPageable)
void parallel_reduce(const size_t begin, const size_t end, const Functor &functor, typename Reduction::value_type *red)
void hostMallocPinned(TData **src, const size_t size)
constexpr unsigned int internalReductionMaxValues
Definition Backends.hpp:67
static constexpr unsigned int reductionBlockSize()
Definition Backends.hpp:147
static constexpr unsigned int maximumBlockSize
Definition Backends.hpp:142
static constexpr unsigned int warpSize
Definition Backends.hpp:143
static constexpr unsigned int reductionGridSize(const size_t n)
Definition Backends.hpp:156