1#ifndef AMREX_GPU_UTILITY_H_
2#define AMREX_GPU_UTILITY_H_
3#include <AMReX_Config.H>
20#include <curand_kernel.h>
29#if defined(AMREX_USE_CUDA)
40#if defined(AMREX_USE_CUDA)
49#if defined(AMREX_USE_HIP)
50 hipPointerAttribute_t attrib;
51 hipError_t r = hipPointerGetAttributes(&attrib, p);
52#if defined(HIP_VERSION_MAJOR) && (HIP_VERSION_MAJOR < 6)
53 return r == hipSuccess && attrib.memoryType == hipMemoryTypeManaged;
55 return r == hipSuccess && attrib.type == hipMemoryTypeManaged;
57#elif defined(AMREX_USE_CUDA)
58 CUpointer_attribute attrib = CU_POINTER_ATTRIBUTE_IS_MANAGED;
59 unsigned int is_managed = 0;
60 void* data[] = { (
void*)(&is_managed) };
61 CUresult r = cuPointerGetAttributes(1, &attrib, data, (CUdeviceptr)p);
62 return r == CUDA_SUCCESS && is_managed;
63#elif defined(AMREX_USE_SYCL)
64 auto type = sycl::get_pointer_type(p,Device::syclContext());
65 return type == sycl::usm::alloc::shared;
73#if defined(AMREX_USE_HIP)
74 hipPointerAttribute_t attrib;
75 hipError_t r = hipPointerGetAttributes(&attrib, p);
76#if defined(HIP_VERSION_MAJOR) && (HIP_VERSION_MAJOR < 6)
77 return r == hipSuccess && attrib.memoryType == hipMemoryTypeDevice;
79 return r == hipSuccess && attrib.type == hipMemoryTypeDevice;
81#elif defined(AMREX_USE_CUDA)
82 CUpointer_attribute attrib = CU_POINTER_ATTRIBUTE_MEMORY_TYPE;
83 CUmemorytype mem_type =
static_cast<CUmemorytype
>(0);
84 void* data[] = { (
void*)(&mem_type) };
85 CUresult r = cuPointerGetAttributes(1, &attrib, data, (CUdeviceptr)p);
86 return r == CUDA_SUCCESS && mem_type == CU_MEMORYTYPE_DEVICE;
87#elif defined(AMREX_USE_SYCL)
88 auto type = sycl::get_pointer_type(p,Device::syclContext());
89 return type == sycl::usm::alloc::device;
97#if defined(AMREX_USE_HIP)
98 hipPointerAttribute_t attrib;
99 hipError_t r = hipPointerGetAttributes(&attrib, p);
100#if defined(HIP_VERSION_MAJOR) && (HIP_VERSION_MAJOR < 6)
101 return r == hipSuccess && attrib.memoryType == hipMemoryTypeHost;
103 return r == hipSuccess && attrib.type == hipMemoryTypeHost;
105#elif defined(AMREX_USE_CUDA)
106 CUpointer_attribute attrib = CU_POINTER_ATTRIBUTE_MEMORY_TYPE;
107 CUmemorytype mem_type =
static_cast<CUmemorytype
>(0);
108 void* data[] = { (
void*)(&mem_type) };
109 CUresult r = cuPointerGetAttributes(1, &attrib, data, (CUdeviceptr)p);
110 return r == CUDA_SUCCESS && mem_type == CU_MEMORYTYPE_HOST;
111#elif defined(AMREX_USE_SYCL)
112 auto type = sycl::get_pointer_type(p,Device::syclContext());
113 return type == sycl::usm::alloc::host;
121#if defined(AMREX_USE_HIP)
122 hipPointerAttribute_t attrib;
123 hipError_t r = hipPointerGetAttributes(&attrib, p);
124 if (r != hipSuccess) {
return false; }
125#if defined(HIP_VERSION_MAJOR) && (HIP_VERSION_MAJOR < 6)
126 auto t = attrib.memoryType;
128 auto t = attrib.type;
130 return (t == hipMemoryTypeHost ||
131 t == hipMemoryTypeDevice ||
132 t == hipMemoryTypeArray ||
133 t == hipMemoryTypeUnified ||
134 t == hipMemoryTypeManaged);
135#elif defined(AMREX_USE_CUDA)
136 CUpointer_attribute attrib = CU_POINTER_ATTRIBUTE_MEMORY_TYPE;
137 CUmemorytype mem_type =
static_cast<CUmemorytype
>(0);
138 void* data[] = { (
void*)(&mem_type) };
139 CUresult r = cuPointerGetAttributes(1, &attrib, data, (CUdeviceptr)p);
140 return r == CUDA_SUCCESS &&
141 (mem_type == CU_MEMORYTYPE_HOST ||
142 mem_type == CU_MEMORYTYPE_DEVICE ||
143 mem_type == CU_MEMORYTYPE_ARRAY ||
144 mem_type == CU_MEMORYTYPE_UNIFIED);
145#elif defined(AMREX_USE_SYCL)
146 auto type = sycl::get_pointer_type(p,Device::syclContext());
147 return type != sycl::usm::alloc::unknown;
161#elif defined(AMREX_USE_SYCL)
162 return sycl::isnan(m);
164 return std::isnan(m);
175#elif defined(AMREX_USE_SYCL)
176 return sycl::isinf(m);
178 return std::isinf(m);
196 StreamIter (
int n,
bool is_thread_safe=
true)
noexcept;
208 [[nodiscard]]
bool isValid () const noexcept {
return m_i < m_n; }
210#if !defined(AMREX_USE_GPU)
217 void init () noexcept;
226void*
memcpy (
void* dest, const
void* src, std::
size_t count)
228#if defined(__HIP_DEVICE_COMPILE__) && defined(AMREX_USE_HIP)
229 return ::memcpy(dest, src, count);
231 return std::memcpy(dest, src, count);
238std::ostream&
operator<< (std::ostream& os,
const dim3& d);
246#if defined(AMREX_USE_CUDA) || defined(AMREX_USE_HIP)
248#define AMREX_GET_DEVICE_FUNC_PTR(FP_t, sym) \
250 auto* amrex_i_hp = (FP_t*) amrex::The_Pinned_Arena()->alloc(sizeof(FP_t)); \
251 amrex::single_task([=] AMREX_GPU_DEVICE () { *amrex_i_hp = sym; }); \
252 amrex::Gpu::streamSynchronize(); \
253 auto amrex_i_r = *amrex_i_hp; \
254 amrex::The_Pinned_Arena()->free(amrex_i_hp); \
257#elif !defined(AMREX_USE_SYCL)
258#define AMREX_GET_DEVICE_FUNC_PTR(FP_t, sym) sym
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:119
#define AMREX_IF_ON_DEVICE(CODE)
Definition AMReX_GpuQualifiers.H:56
#define AMREX_IF_ON_HOST(CODE)
Definition AMReX_GpuQualifiers.H:58
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
Definition AMReX_GpuUtility.H:194
int operator()() const noexcept
Definition AMReX_GpuUtility.H:206
~StreamIter()
Definition AMReX_GpuUtility.cpp:65
bool isValid() const noexcept
Definition AMReX_GpuUtility.H:208
void operator=(StreamIter const &)=delete
StreamIter(StreamIter &&)=delete
StreamIter(StreamIter const &)=delete
void operator++()
Definition AMReX_GpuUtility.cpp:81
bool isManaged(void const *p) noexcept
Definition AMReX_GpuUtility.H:48
__host__ __device__ T LDG(Array4< T > const &a, int i, int j, int k) noexcept
Definition AMReX_GpuUtility.H:28
bool isGpuPtr(void const *p) noexcept
Definition AMReX_GpuUtility.H:120
__host__ __device__ bool isnan(T m) noexcept
Definition AMReX_GpuUtility.H:156
bool isPinnedPtr(void const *p) noexcept
Definition AMReX_GpuUtility.H:96
bool inNoSyncRegion() noexcept
Definition AMReX_GpuControl.H:148
__host__ __device__ void * memcpy(void *dest, const void *src, std::size_t count)
Definition AMReX_GpuUtility.H:226
__host__ __device__ bool isinf(T m) noexcept
Definition AMReX_GpuUtility.H:170
bool isDevicePtr(void const *p) noexcept
Definition AMReX_GpuUtility.H:72
Definition AMReX_Amr.cpp:50
std::ostream & operator<<(std::ostream &os, AmrMesh const &amr_mesh)
Stream helper; forwards to the friend declared inside AmrMesh.
Definition AMReX_AmrMesh.cpp:1306
__host__ __device__ void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:139
A multidimensional array accessor.
Definition AMReX_Array4.H:283
Definition AMReX_GpuUtility.H:183
StreamItInfo() noexcept
Definition AMReX_GpuUtility.H:185
StreamItInfo & DisableDeviceSync() noexcept
Definition AMReX_GpuUtility.H:187
bool device_sync
Definition AMReX_GpuUtility.H:184