47#include <unordered_map>
55#if defined(NEKTAR_ENABLE_CUDA)
57#elif defined(NEKTAR_ENABLE_HIP)
59#elif defined(NEKTAR_ENABLE_SYCL)
69#if defined(NEKTAR_ENABLE_DEVICE)
70extern std::unordered_map<unsigned int, void *> internalMemoryBufferMap;
71extern void *internalDeviceBuffer;
72extern void *internalHostBuffer;
73extern unsigned int internalMaxDataSizeByte;
77extern unsigned int internalReductionBufferSize;
88#if defined(NEKTAR_ENABLE_DEVICE)
101 static inline const std::string
name =
"Serial";
107 static inline const std::string
name =
"AVX";
113 static inline const std::string
name =
"Device";
115#if defined(NEKTAR_ENABLE_CUDA) || defined(SYCL_ENABLE_CUDA)
118 static constexpr unsigned int warpSize = 32u;
119#elif defined(NEKTAR_ENABLE_HIP) || defined(SYCL_ENABLE_HIP)
122 static constexpr unsigned int warpSize = 64u;
123#elif defined(SYCL_ENABLE_INTEL)
126 static constexpr unsigned int warpSize = 32u;
127#elif defined(SYCL_ENABLE_CPU)
130#if defined(__ADAPTIVECPP__) || defined(__DPCPP_COMPILER_NATIVE_CPU)
131 static constexpr unsigned int warpSize = 1u;
132#elif defined(__DPCPP_COMPILER)
133 static constexpr unsigned int warpSize = 8u;
134#elif defined(NEKTAR_USE_SIMSYCL)
136 static constexpr unsigned int warpSize = 32u;
138 static constexpr unsigned int warpSize = 1u;
159 return static_cast<unsigned int>(std::min<size_t>(
160 std::max<size_t>((n + blockSize - 1u) / blockSize, 1u),
165#if defined(__ADAPTIVECPP__)
166#define SYCL_SUBGROUP_SIZE(x)
167#elif defined(__DPCPP_COMPILER)
168#define SYCL_SUBGROUP_SIZE(x) [[sycl::reqd_sub_group_size(x)]]
170#define SYCL_SUBGROUP_SIZE(x)
174#if defined(NEKTAR_ENABLE_SIMD)
179 __STDCPP_DEFAULT_NEW_ALIGNMENT__;
189 static constexpr unsigned int value = 1u;
199#if defined(NEKTAR_ENABLE_CUDA) || defined(SYCL_ENABLE_CUDA) || \
200 defined(NEKTAR_ENABLE_HIP) || defined(SYCL_ENABLE_HIP) || \
201 defined(SYCL_ENABLE_INTEL)
204 "AVX/Device back-ends interoperability requires device vector width "
205 "(warpsize) to be integer multiple of SIMD vector width");
213 static constexpr unsigned int value = std::max(
217template <
typename TData>
220 if (execName ==
"Serial")
224 else if (execName ==
"AVX")
228 else if (execName ==
"Device")
240#if defined(__CUDACC__) || defined(__NEK_HIPCC__) || defined(NEKTAR_ENABLE_SYCL)
241#define DEVICE_COMPILE_ONLY
245#if defined(NEKTAR_ENABLE_CUDA) && defined(DEVICE_COMPILE_ONLY)
246#define NEK_RESTRICT __restrict__
247#elif defined(NEKTAR_ENABLE_HIP) && defined(DEVICE_COMPILE_ONLY)
248#define NEK_RESTRICT __restrict__
249#elif defined(NEKTAR_ENABLE_SYCL)
250#define NEK_RESTRICT __restrict__
251#elif defined(_MSC_VER)
252#define NEK_RESTRICT __restrict
254#define NEK_RESTRICT __restrict__
261#if defined(NEKTAR_ENABLE_CUDA) && defined(DEVICE_COMPILE_ONLY)
262#define NEK_HOSTDEVICE_INLINE __host__ __device__ __forceinline__
263#elif defined(NEKTAR_ENABLE_HIP) && defined(DEVICE_COMPILE_ONLY)
264#define NEK_HOSTDEVICE_INLINE __host__ __device__ __forceinline__
265#elif defined(NEKTAR_ENABLE_SYCL)
266#define NEK_HOSTDEVICE_INLINE NEK_FORCE_INLINE
268#define NEK_HOSTDEVICE_INLINE NEK_FORCE_INLINE
276#if defined(NEKTAR_ENABLE_CUDA) && defined(DEVICE_COMPILE_ONLY)
277#define NEK_DEVICE_INLINE __device__ __forceinline__
278#elif defined(NEKTAR_ENABLE_HIP) && defined(DEVICE_COMPILE_ONLY)
279#define NEK_DEVICE_INLINE __device__ __forceinline__
280#elif defined(NEKTAR_ENABLE_SYCL)
281#define NEK_DEVICE_INLINE NEK_FORCE_INLINE
283#define NEK_DEVICE_INLINE NEK_FORCE_INLINE
290#if defined(NEKTAR_ENABLE_CUDA) && defined(DEVICE_COMPILE_ONLY)
291#define NEK_DEVICE_KERNEL __global__
292#elif defined(NEKTAR_ENABLE_HIP) && defined(DEVICE_COMPILE_ONLY)
293#define NEK_DEVICE_KERNEL __global__
294#elif defined(NEKTAR_ENABLE_SYCL)
295#define NEK_DEVICE_KERNEL NEK_FORCE_INLINE
297#define NEK_DEVICE_KERNEL NEK_FORCE_INLINE
302 if constexpr (std::is_same_v<TData, float>)
306 else if constexpr (std::is_same_v<TData, double>)
314template <
bool B,
typename TData =
void>
struct const_if
352template <
typename TMask,
typename TData>
356 if constexpr (std::is_arithmetic_v<TData>)
367 [[maybe_unused]]
unsigned int device_rank)
369#if defined(NEKTAR_ENABLE_CUDA)
371#elif defined(NEKTAR_ENABLE_HIP)
373#elif defined(NEKTAR_ENABLE_SYCL)
382#if defined(NEKTAR_ENABLE_CUDA)
386#elif defined(NEKTAR_ENABLE_HIP)
390#elif defined(NEKTAR_ENABLE_SYCL)
400#if defined(NEKTAR_ENABLE_CUDA)
402#elif defined(NEKTAR_ENABLE_HIP)
404#elif defined(SYCL_ENABLE_CUDA) || defined(SYCL_ENABLE_HIP) || \
405 defined(SYCL_ENABLE_INTEL)
406 num_device = sycl::device::get_devices(sycl::info::device_type::gpu).size();
407#elif defined(NEKTAR_USE_SIMSYCL)
415#if defined(NEKTAR_ENABLE_CUDA)
417#elif defined(NEKTAR_ENABLE_HIP)
419#elif defined(NEKTAR_ENABLE_SYCL)
421 for (
auto &item : Queues)
429 [[maybe_unused]]
const unsigned int streamID)
431#if defined(NEKTAR_ENABLE_CUDA)
434#elif defined(NEKTAR_ENABLE_HIP)
437#elif defined(NEKTAR_ENABLE_SYCL)
444 std::enable_if_t<!std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
447 [[maybe_unused]]
unsigned int eventID)
453 std::enable_if_t<!std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
456 [[maybe_unused]]
unsigned int streamID,
457 [[maybe_unused]] std::vector<unsigned int> &eventIDs)
463 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
466 [[maybe_unused]]
unsigned int eventID)
468#if defined(NEKTAR_ENABLE_CUDA)
469 if (streamID != eventID)
478#elif defined(NEKTAR_ENABLE_HIP)
479 if (streamID != eventID)
488#elif defined(NEKTAR_ENABLE_SYCL)
489 if (streamID != eventID)
492#if defined(__ADAPTIVECPP__)
495 cgh.depends_on(event);
496 cgh.AdaptiveCpp_enqueue_custom_operation(
497 [=]([[maybe_unused]] sycl::interop_handle ih) {});
500#elif defined(__DPCPP_COMPILER)
502 [&](sycl::handler &cgh) { cgh.ext_oneapi_barrier({
event}); });
511 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Device>,
bool>
514 [[maybe_unused]]
unsigned int streamID,
515 [[maybe_unused]] std::vector<unsigned int> &eventIDs)
517#if defined(NEKTAR_ENABLE_CUDA)
519 for (
auto eventID : eventIDs)
521 if (streamID != eventID)
530#elif defined(NEKTAR_ENABLE_HIP)
532 for (
auto eventID : eventIDs)
534 if (streamID != eventID)
543#elif defined(NEKTAR_ENABLE_SYCL)
544 std::vector<sycl::event> events;
545 for (
auto eventID : eventIDs)
547 if (streamID != eventID)
552#if defined(__ADAPTIVECPP__)
555 cgh.depends_on(events);
556 cgh.AdaptiveCpp_enqueue_custom_operation(
557 [=]([[maybe_unused]] sycl::interop_handle ih) {});
560#elif defined(__DPCPP_COMPILER)
562 [&](sycl::handler &cgh) { cgh.ext_oneapi_barrier(events); });
570template <
typename ExecSpace>
573 if constexpr (std::is_same_v<ExecSpace, NektarSpaces::Device>)
575#if defined(NEKTAR_ENABLE_CUDA)
577#elif defined(NEKTAR_ENABLE_HIP)
584template <
typename ExecSpace>
587 if constexpr (std::is_same_v<ExecSpace, NektarSpaces::Device>)
589#if defined(NEKTAR_ENABLE_CUDA)
591#elif defined(NEKTAR_ENABLE_HIP)
593#elif defined(NEKTAR_ENABLE_SYCL)
610template <
typename ExecSpace>
612 [[maybe_unused]]
const unsigned int streamID,
613 [[maybe_unused]]
const std::vector<unsigned int> &streamIDs)
615 if constexpr (std::is_same_v<ExecSpace, NektarSpaces::Device>)
617#if defined(NEKTAR_ENABLE_CUDA) || defined(NEKTAR_ENABLE_HIP)
623 for (
const auto id : streamIDs)
625 if (
id != streamID && StreamExists<ExecSpace>(
id))
627 RecordStreamEvent<ExecSpace>(
id);
628 SetStreamDependencies<ExecSpace>(streamID,
id);
631#elif defined(NEKTAR_ENABLE_SYCL) && !defined(SYCL_ENABLE_CPU)
633 std::vector<unsigned int> waitOn;
634 for (
const auto id : streamIDs)
636 if (
id != streamID && StreamExists<ExecSpace>(
id))
638 waitOn.push_back(
id);
644 SetStreamDependencies<ExecSpace>(streamID, waitOn);
652template <
typename ExecSpace>
654 const unsigned int producerID)
656 JoinStreams<ExecSpace>(streamID, std::vector<unsigned int>{producerID});
667template <
typename ExecSpace>
669 [[maybe_unused]]
const unsigned int streamID,
670 [[maybe_unused]]
const std::vector<unsigned int> &streamIDs)
672 if constexpr (std::is_same_v<ExecSpace, NektarSpaces::Device>)
674#if defined(NEKTAR_ENABLE_CUDA) || defined(NEKTAR_ENABLE_HIP)
675 if (streamID == 0 || !StreamExists<ExecSpace>(streamID))
680 bool recorded =
false;
681 for (
const auto id : streamIDs)
690 RecordStreamEvent<ExecSpace>(streamID);
694 SetStreamDependencies<ExecSpace>(
id, streamID);
696#elif defined(NEKTAR_ENABLE_SYCL) && !defined(SYCL_ENABLE_CPU)
697 if (!StreamExists<ExecSpace>(streamID))
702 for (
const auto id : streamIDs)
706 SetStreamDependencies<ExecSpace>(
id, streamID);
#define NEK_HOSTDEVICE_INLINE
#define CHECK_HIPCUDA_ERROR(err)
static std::unordered_map< unsigned int, cudaStream_t > & GetAllInstances(void)
static void RecordEvent(unsigned int id)
static cudaEvent_t GetEvent(unsigned int id)
static cudaStream_t & GetInstance(unsigned int id)
static hipStream_t & GetInstance(unsigned int id)
static void RecordEvent(unsigned int id)
static hipEvent_t GetEvent(unsigned int id)
static std::unordered_map< unsigned int, hipStream_t > & GetAllInstances(void)
static std::unordered_map< unsigned int, sycl::queue * > & GetAllInstances(void)
static sycl::queue & GetInstance(unsigned int id)
static void SetEvent(unsigned int id, sycl::event &e)
static sycl::event & GetEvent(unsigned int id)
static unsigned int GetVectorWidth(const std::string &execName)
static constexpr size_t host_memory_alignment
static unsigned int nekGetNumDevice()
static unsigned int nekGetDevice()
static void SetStreamDependencies(unsigned int streamID, unsigned int eventID)
static void nekDeviceSynchronize(void)
static void nekStreamSynchronize(const unsigned int streamID)
unsigned int internalSYCLDeviceId
NEK_HOSTDEVICE_INLINE TData Select(const TMask &m, const TData &a, const TData &b)
a where m holds, and b elsewhere.
static void nekSetDevice(unsigned int device_rank)
static bool StreamExists(const unsigned int streamID)
Whether stream streamID has been created, and so may hold work.
static void ReleaseStreams(const unsigned int streamID, const std::vector< unsigned int > &streamIDs)
Order the work queued next on each of streamIDs after the work already queued on stream streamID.
static void RecordStreamEvent(const unsigned int streamID)
Record the current point of stream streamID for SetStreamDependencies. SYCL queues record their last ...
std::string DataTypeToString(void)
static void JoinStreams(const unsigned int streamID, const std::vector< unsigned int > &streamIDs)
Order the work queued next on stream streamID after the work already queued on each of streamIDs.
constexpr unsigned int internalReductionMaxValues
typename abi< ScalarType, width >::type simd
scalarT< double > select(scalarMask m, scalarT< double > a, scalarT< double > b)
static const std::string name
static constexpr unsigned int defaultBlockSize
static constexpr unsigned int reductionBlockSize()
static constexpr unsigned int maximumBlockSize
static constexpr unsigned int warpSize
static constexpr unsigned int reductionGridSize(const size_t n)
static const std::string name
static const std::string name
static constexpr unsigned int value
tinysimd::simd< TData > type
tinysimd::simd< TData > type
tinysimd::scalarT< TData > type