23#ifndef EBGEOMETRY_GPURUNTIME_HPP
24#define EBGEOMETRY_GPURUNTIME_HPP
29#if defined(EBGEOMETRY_CUDA) || defined(EBGEOMETRY_HIP) || defined(EBGEOMETRY_DOXYGEN)
39#if defined(EBGEOMETRY_HIP)
40#include <hip/hip_runtime.h>
41#elif defined(EBGEOMETRY_CUDA)
42#include <cuda_runtime.h>
56#if defined(EBGEOMETRY_DOXYGEN)
89[[nodiscard]]
inline Error
90memAlloc(
void** a_devicePtr,
size_t a_numBytes)
noexcept;
97[[nodiscard]]
inline Error
107[[nodiscard]]
inline Error
116[[nodiscard]]
inline Error
128[[nodiscard]]
inline Error
137[[nodiscard]]
inline Error
148[[nodiscard]]
inline Error
156[[nodiscard]]
inline Error
163[[nodiscard]]
inline Error
171[[nodiscard]] inline
Error
180[[nodiscard]] inline const
char*
190[[nodiscard]] inline
bool
201[[nodiscard]] inline const
char*
204#elif defined(EBGEOMETRY_HIP)
206using Error = hipError_t;
213[[nodiscard]]
inline Error
214memAlloc(
void** a_devicePtr,
size_t a_numBytes)
noexcept
216 return hipMalloc(a_devicePtr, a_numBytes);
219[[nodiscard]]
inline Error
220memFree(
void* a_devicePtr)
noexcept
222 return hipFree(a_devicePtr);
225[[nodiscard]]
inline Error
228 return hipMallocManaged(a_ptr, a_numBytes);
231[[nodiscard]]
inline Error
234 return hipHostMalloc(a_ptr, a_numBytes, 0u);
237[[nodiscard]]
inline Error
240 return hipHostMalloc(a_ptr, a_numBytes, hipHostMallocMapped);
243[[nodiscard]]
inline Error
246 return hipHostFree(a_ptr);
249[[nodiscard]]
inline Error
250memcpy(
void* a_dst,
const void* a_src,
size_t a_numBytes,
MemcpyKind a_kind)
noexcept
252 return hipMemcpy(a_dst, a_src, a_numBytes, a_kind);
255[[nodiscard]]
inline Error
258 return hipGetDeviceCount(a_count);
261[[nodiscard]]
inline Error
264 return hipGetLastError();
267[[nodiscard]]
inline Error
270 return hipDeviceSynchronize();
273[[nodiscard]]
inline const char*
276 return hipGetErrorString(a_error);
279[[nodiscard]]
inline bool
282 return a_error == hipErrorDeinitialized || a_error == hipErrorContextIsDestroyed;
285[[nodiscard]]
inline const char*
288 return (a_error == hipErrorNoBinaryForGpu)
289 ?
"\n hint: the binary has no code for this GPU; build for its architecture (--offload-arch, "
290 "HIP_ARCH or CMAKE_HIP_ARCHITECTURES)"
294#elif defined(EBGEOMETRY_CUDA)
296using Error = cudaError_t;
303[[nodiscard]]
inline Error
304memAlloc(
void** a_devicePtr,
size_t a_numBytes)
noexcept
306 return cudaMalloc(a_devicePtr, a_numBytes);
309[[nodiscard]]
inline Error
310memFree(
void* a_devicePtr)
noexcept
312 return cudaFree(a_devicePtr);
315[[nodiscard]]
inline Error
318 return cudaMallocManaged(a_ptr, a_numBytes, cudaMemAttachGlobal);
321[[nodiscard]]
inline Error
324 return cudaHostAlloc(a_ptr, a_numBytes, cudaHostAllocDefault);
327[[nodiscard]]
inline Error
330 return cudaHostAlloc(a_ptr, a_numBytes, cudaHostAllocMapped);
333[[nodiscard]]
inline Error
336 return cudaFreeHost(a_ptr);
339[[nodiscard]]
inline Error
340memcpy(
void* a_dst,
const void* a_src,
size_t a_numBytes,
MemcpyKind a_kind)
noexcept
342 return cudaMemcpy(a_dst, a_src, a_numBytes, a_kind);
345[[nodiscard]]
inline Error
348 return cudaGetDeviceCount(a_count);
351[[nodiscard]]
inline Error
354 return cudaGetLastError();
357[[nodiscard]]
inline Error
360 return cudaDeviceSynchronize();
363[[nodiscard]]
inline const char*
366 return cudaGetErrorString(a_error);
369[[nodiscard]]
inline bool
372 return a_error == cudaErrorCudartUnloading || a_error == cudaErrorContextIsDestroyed;
375[[nodiscard]]
inline const char*
378 return (a_error == cudaErrorUnsupportedPtxVersion || a_error == cudaErrorNoKernelImageForDevice)
379 ?
"\n hint: the binary has no machine code for this GPU, and the driver cannot compile its PTX; "
380 "build for the GPU's architecture (-arch, CUDA_ARCH or CMAKE_CUDA_ARCHITECTURES), or update "
398#define EBGEOMETRY_GPU_CHECK(x) \
400 const EBGeometry::GPU::Error ebgeometry_gpu_err = (x); \
401 if (ebgeometry_gpu_err != EBGeometry::GPU::Success) { \
402 std::fprintf(stderr, \
403 "EBGeometry GPU error: %s\n call: %s\n file: %s\n line: %d%s\n", \
404 EBGeometry::GPU::getErrorString(ebgeometry_gpu_err), \
408 EBGeometry::GPU::getErrorHint(ebgeometry_gpu_err)); \
GPU portability layer: host/device decoration macros and backend detection.
Error memAllocHostMapped(void **a_ptr, size_t a_numBytes) noexcept
Allocate page-locked (pinned) host memory that is additionally mapped into the device address space f...
Error memFree(void *a_devicePtr) noexcept
Free device memory allocated with memAlloc (cudaFree / hipFree).
Error memcpy(void *a_dst, const void *a_src, size_t a_numBytes, MemcpyKind a_kind) noexcept
Copy memory between host and device (cudaMemcpy / hipMemcpy). Synchronous.
Error getDeviceCount(int *a_count) noexcept
Get the number of available GPU devices (cudaGetDeviceCount / hipGetDeviceCount).
Error deviceSynchronize() noexcept
Block until the device has finished all preceding work (cudaDeviceSynchronize / hipDeviceSynchronize)...
int Error
Backend runtime error code (cudaError_t under CUDA, hipError_t under HIP).
Definition EBGeometry_GPURuntime.hpp:61
const char * getErrorHint(Error a_error) noexcept
What to do about an error that means the binary has no code this GPU can run: the binary was built fo...
Error memAlloc(void **a_devicePtr, size_t a_numBytes) noexcept
Allocate device memory (cudaMalloc / hipMalloc).
const char * getErrorString(Error a_error) noexcept
Get the human-readable description of an error code (cudaGetErrorString / hipGetErrorString).
Error memAllocManaged(void **a_ptr, size_t a_numBytes) noexcept
Allocate managed (unified) memory accessible from host and device (cudaMallocManaged / hipMallocManag...
Error memFreeHost(void *a_ptr) noexcept
Free page-locked host memory allocated with memAllocHost (cudaFreeHost / hipHostFree).
Error getLastError() noexcept
Get and clear the last runtime error (cudaGetLastError / hipGetLastError).
constexpr MemcpyKind MemcpyDeviceToHost
Device-to-host copy direction (cudaMemcpyDeviceToHost / hipMemcpyDeviceToHost).
Definition EBGeometry_GPURuntime.hpp:81
bool isShutdownError(Error a_error) noexcept
True for the errors a runtime call returns once the runtime has shut down at program exit (cudaErrorC...
constexpr Error Success
Success value of Error (cudaSuccess / hipSuccess).
Definition EBGeometry_GPURuntime.hpp:71
Error memAllocHost(void **a_ptr, size_t a_numBytes) noexcept
Allocate page-locked (pinned) host memory (cudaHostAlloc / hipHostMalloc).
int MemcpyKind
Memory-copy direction (cudaMemcpyKind under CUDA, hipMemcpyKind under HIP).
Definition EBGeometry_GPURuntime.hpp:66
constexpr MemcpyKind MemcpyHostToDevice
Host-to-device copy direction (cudaMemcpyHostToDevice / hipMemcpyHostToDevice).
Definition EBGeometry_GPURuntime.hpp:76
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26