37#if (defined(NEKTAR_ENABLE_CUDA) && defined(DEVICE_COMPILE_ONLY)) || \
38 (defined(NEKTAR_ENABLE_HIP) && defined(DEVICE_COMPILE_ONLY))
46template <
typename Functor,
48__global__ __launch_bounds__(blockSize)
void parallel_for(
const size_t begin,
50 const Functor functor)
52 size_t i = begin + blockDim.x * blockIdx.x + threadIdx.x;
57 i += blockDim.x * gridDim.x;
63 typename ExecSpace,
typename Functor,
64 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
66inline void parallel_for(
const size_t begin,
const size_t end,
67 const Functor &functor)
82 const unsigned int gridSize = ((end - begin) + blockSize - 1u) / blockSize;
84#if defined(NEKTAR_ENABLE_CUDA)
86#elif defined(NEKTAR_ENABLE_HIP)
90 parallel_for<<<gridSize, blockSize, 0, stream>>>(begin, end, functor);
98template <
bool init,
typename Reduction>
100 typename Reduction::value_type *
const dest,
101 const typename Reduction::value_type val)
103 using TData =
typename Reduction::value_type;
108 else if constexpr (std::is_same_v<Reduction, Nektar::ReduceSum<TData>>)
110 atomic_add<NektarSpaces::GlobalScope>(dest, val);
112 else if constexpr (std::is_same_v<Reduction, Nektar::ReduceMax<TData>>)
114 atomic_max<NektarSpaces::GlobalScope>(dest, val);
116 else if constexpr (std::is_same_v<Reduction, Nektar::ReduceMin<TData>>)
118 atomic_min<NektarSpaces::GlobalScope>(dest, val);
126template <
bool init,
typename Reduction,
unsigned int nred,
typename Functor,
128__global__ __launch_bounds__(blockSize)
void reduceNKernel(
129 const size_t begin,
const size_t end,
130 typename Reduction::value_type *buffer,
const Functor functor)
132 using TData =
typename Reduction::value_type;
134 constexpr TData identity = reduce_identity<Reduction>();
137 auto grid = cg::this_grid();
138 auto block = cg::this_thread_block();
139 auto warp = cg::tiled_partition<warpsize>(block);
142 for (
unsigned int s = 0; s < nred; ++s)
147 for (
size_t tid = begin + grid.thread_rank(); tid < end; tid += grid.size())
151 for (
unsigned int s = 0; s < nred; ++s)
153 v[s] = reduce_join<Reduction>(v[s], vals[s]);
157 for (
unsigned int s = 0; s < nred; ++s)
159 v[s] = blockReduceWarpPartials(v[s], identity, [&](TData x) {
161 for (
unsigned int offset = warpsize / 2; offset > 0; offset /= 2)
163 x = reduce_join<Reduction>(x, warp.shfl_down(x, offset));
169 if (block.thread_rank() == 0)
171 for (
unsigned int s = 0; s < nred; ++s)
173 reduce_store<init, Reduction>(
174 &buffer[s * gridDim.x + block.group_index().x], v[s]);
181 typename ExecSpace,
bool init,
typename Reduction,
typename Functor,
182 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
185 const Functor &functor,
186 typename Reduction::value_type *out)
188 using TData =
typename Reduction::value_type;
192 const unsigned int gridSize =
195#if defined(NEKTAR_ENABLE_CUDA)
197#elif defined(NEKTAR_ENABLE_HIP)
201 if (internalMemoryBufferMap.find(streamID) == internalMemoryBufferMap.end())
204 internalReductionBufferSize, streamID);
207 TData *
buffer = (TData *)internalMemoryBufferMap[streamID];
208 reduceNKernel<true, Reduction, 1><<<gridSize, blockSize, 0, stream>>>(
210 [=] __device__(
const size_t i, TData *vals) { vals[0] = functor(i); });
212 reduceNKernel<init, Reduction, 1><<<1, blockSize, 0, stream>>>(
214 [=] __device__(
const size_t i, TData *vals) { vals[0] =
buffer[i]; });
220 typename ExecSpace,
typename Reduction,
typename Functor,
221 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
224 const Functor &functor,
225 typename Reduction::value_type &out)
227 using TData =
typename Reduction::value_type;
230 if (internalHostBuffer ==
nullptr)
233 deviceMalloc(&internalDeviceBuffer, internalMaxDataSizeByte, streamID);
236 parallel_reduce<ExecSpace, true, Reduction>(begin, end, functor,
237 (TData *)internalDeviceBuffer);
239 deviceMemcpy<eDeviceToHost>(internalHostBuffer, internalDeviceBuffer,
240 sizeof(TData), streamID);
242 out = *(TData *)internalHostBuffer;
251 typename ExecSpace,
bool init,
typename Reduction,
unsigned int nred,
253 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
256 const Functor &functor,
257 typename Reduction::value_type *out)
259 using TData =
typename Reduction::value_type;
262 "parallel_reduce: too many values.");
266 const unsigned int gridSize =
269 ASSERTL0(nred * gridSize *
sizeof(TData) <= internalReductionBufferSize,
270 "Too many values for the reduction buffer.");
272#if defined(NEKTAR_ENABLE_CUDA)
274#elif defined(NEKTAR_ENABLE_HIP)
278 if (internalMemoryBufferMap.find(streamID) == internalMemoryBufferMap.end())
281 internalReductionBufferSize, streamID);
284 TData *
buffer = (TData *)internalMemoryBufferMap[streamID];
285 reduceNKernel<true, Reduction, nred>
286 <<<gridSize, blockSize, 0, stream>>>(begin, end,
buffer, functor);
288 reduceNKernel<init, Reduction, nred><<<1, blockSize, 0, stream>>>(
289 0, gridSize,
out, [=] __device__(
const size_t i, TData *vals) {
290 for (
unsigned int s = 0; s < nred; ++s)
292 vals[s] =
buffer[s * gridSize + i];
#define NEK_DEVICE_INLINE
#define CHECK_LAST_HIPCUDA_ERROR()
#define ASSERTL0(condition, msg)
static cudaStream_t & GetInstance(unsigned int id)
static hipStream_t & GetInstance(unsigned int id)
constexpr unsigned int warpsize
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 defaultBlockSize
static constexpr unsigned int reductionBlockSize()
static constexpr unsigned int warpSize
static constexpr unsigned int reductionGridSize(const size_t n)