37#if defined(NEKTAR_ENABLE_SYCL)
44 typename ExecSpace,
typename Functor,
45 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
47inline void parallel_for(
const size_t begin,
const size_t end,
48 const Functor &functor)
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); });
64template <
bool init,
typename Reduction>
66 typename Reduction::value_type *
const dest,
67 const typename Reduction::value_type val)
69 using TData =
typename Reduction::value_type;
74 else if constexpr (std::is_same_v<Reduction, Nektar::ReduceSum<TData>>)
76 atomic_add<NektarSpaces::GlobalScope>(dest, val);
78 else if constexpr (std::is_same_v<Reduction, Nektar::ReduceMax<TData>>)
80 atomic_max<NektarSpaces::GlobalScope>(dest, val);
82 else if constexpr (std::is_same_v<Reduction, Nektar::ReduceMin<TData>>)
84 atomic_min<NektarSpaces::GlobalScope>(dest, val);
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)
98 using TData =
typename Reduction::value_type;
100 constexpr TData identity = reduce_identity<Reduction>();
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);
109 sycl::nd_range<1>(gridSize * blockSize, blockSize),
114 .template get_multi_ptr<sycl::access::decorated::yes>()
116 const size_t lid = indx.get_local_id(0);
117 size_t gid = begin + indx.get_global_id(0);
120 for (
unsigned int s = 0; s < nred; ++s)
128 for (
unsigned int s = 0; s < nred; ++s)
130 tmp[s] = reduce_join<Reduction>(tmp[s], vals[s]);
132 gid += indx.get_global_range(0);
134 for (
unsigned int s = 0; s < nred; ++s)
136 scratch[s * blockSize + lid] = tmp[s];
139#if defined(__ADAPTIVECPP__)
140 indx.barrier(sycl::access::fence_space::local_space);
142 sycl::group_barrier(indx.get_group(),
143 sycl::memory_scope::work_group);
149 if (blockSize > n && lid < n && lid + n < blockSize)
151 for (
unsigned int s = 0; s < nred; ++s)
153 scratch[s * blockSize + lid] =
154 reduce_join<Reduction>(
155 scratch[s * blockSize + lid],
156 scratch[s * blockSize + lid + n]);
159#if defined(__ADAPTIVECPP__)
160 indx.barrier(sycl::access::fence_space::local_space);
162 sycl::group_barrier(indx.get_group(),
163 sycl::memory_scope::work_group);
170 for (
unsigned int s = 0; s < nred; ++s)
172 reduce_store<init, Reduction>(buffer + s * gridSize +
174 scratch[s * blockSize]);
184 typename ExecSpace,
bool init,
typename Reduction,
typename Functor,
185 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
188 const Functor &functor,
189 typename Reduction::value_type *out)
191 using TData =
typename Reduction::value_type;
195 const unsigned int gridSize =
198 if (internalMemoryBufferMap.find(streamID) == internalMemoryBufferMap.end())
201 internalReductionBufferSize, streamID);
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]; });
215 typename ExecSpace,
typename Reduction,
typename Functor,
216 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
219 const Functor &functor,
220 typename Reduction::value_type &out)
222 using TData =
typename Reduction::value_type;
225 if (internalHostBuffer ==
nullptr)
228 deviceMalloc(&internalDeviceBuffer, internalMaxDataSizeByte, streamID);
231 parallel_reduce<ExecSpace, true, Reduction>(begin, end, functor,
232 (TData *)internalDeviceBuffer);
234 deviceMemcpy<eDeviceToHost>(internalHostBuffer, internalDeviceBuffer,
235 sizeof(TData), streamID);
237 out = *(TData *)internalHostBuffer;
246 typename ExecSpace,
bool init,
typename Reduction,
unsigned int nred,
248 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
251 const Functor &functor,
252 typename Reduction::value_type *out)
254 using TData =
typename Reduction::value_type;
257 "parallel_reduce: too many values.");
261 const unsigned int gridSize =
264 ASSERTL0(nred * gridSize *
sizeof(TData) <= internalReductionBufferSize,
265 "Too many values for the reduction buffer.");
267 if (internalMemoryBufferMap.find(streamID) == internalMemoryBufferMap.end())
270 internalReductionBufferSize, streamID);
273 TData *
buffer = (TData *)internalMemoryBufferMap[streamID];
274 reduceNKernel<true, Reduction, nred>(gridSize, blockSize, begin, end,
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)
280 vals[s] =
buffer[s * gridSize + i];
#define SYCL_SUBGROUP_SIZE(x)
#define NEK_DEVICE_INLINE
#define ASSERTL0(condition, msg)
static cgsize_t gid(int i, int j, int k)
static sycl::queue & GetInstance(unsigned int id)
static void SetEvent(unsigned int id, sycl::event &e)
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
static constexpr unsigned int reductionBlockSize()
static constexpr unsigned int maximumBlockSize
static constexpr unsigned int warpSize
static constexpr unsigned int reductionGridSize(const size_t n)