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)
94[[nodiscard]]
inline Error
95memAlloc(
void** a_devicePtr,
size_t a_numBytes)
noexcept;
102[[nodiscard]]
inline Error
112[[nodiscard]]
inline Error
121[[nodiscard]]
inline Error
133[[nodiscard]]
inline Error
142[[nodiscard]]
inline Error
153[[nodiscard]]
inline Error
161[[nodiscard]]
inline Error
176[[nodiscard]]
inline Error
184[[nodiscard]]
inline Error
192[[nodiscard]]
inline Error
205[[nodiscard]]
inline Error
215[[nodiscard]]
inline Error
222[[nodiscard]]
inline Error
230[[nodiscard]] inline
Error
239[[nodiscard]] inline const
char*
249[[nodiscard]] inline
bool
260[[nodiscard]] inline const
char*
263#elif defined(EBGEOMETRY_HIP)
265using Error = hipError_t;
267using Stream = hipStream_t;
274[[nodiscard]]
inline Error
275memAlloc(
void** a_devicePtr,
size_t a_numBytes)
noexcept
277 return hipMalloc(a_devicePtr, a_numBytes);
280[[nodiscard]]
inline Error
281memFree(
void* a_devicePtr)
noexcept
283 return hipFree(a_devicePtr);
286[[nodiscard]]
inline Error
289 return hipMallocManaged(a_ptr, a_numBytes);
292[[nodiscard]]
inline Error
295 return hipHostMalloc(a_ptr, a_numBytes, 0u);
298[[nodiscard]]
inline Error
301 return hipHostMalloc(a_ptr, a_numBytes, hipHostMallocMapped);
304[[nodiscard]]
inline Error
307 return hipHostFree(a_ptr);
310[[nodiscard]]
inline Error
311memcpy(
void* a_dst,
const void* a_src,
size_t a_numBytes,
MemcpyKind a_kind)
noexcept
313 return hipMemcpy(a_dst, a_src, a_numBytes, a_kind);
316[[nodiscard]]
inline Error
319 return hipGetDeviceCount(a_count);
322[[nodiscard]]
inline Error
325 return hipStreamCreateWithFlags(a_stream, hipStreamNonBlocking);
328[[nodiscard]]
inline Error
331 return hipStreamDestroy(a_stream);
334[[nodiscard]]
inline Error
337 return hipStreamSynchronize(a_stream);
340[[nodiscard]]
inline Error
343 return hipMemcpyAsync(a_dst, a_src, a_numBytes, a_kind, a_stream);
346[[nodiscard]]
inline Error
350 const auto error = hipGetDevice(&device);
352 return (error != hipSuccess) ? error : hipDeviceGetAttribute(a_kiloHertz, hipDeviceAttributeClockRate, device);
355[[nodiscard]]
inline Error
358 return hipGetLastError();
361[[nodiscard]]
inline Error
364 return hipDeviceSynchronize();
367[[nodiscard]]
inline const char*
370 return hipGetErrorString(a_error);
373[[nodiscard]]
inline bool
376 return a_error == hipErrorDeinitialized || a_error == hipErrorContextIsDestroyed;
379[[nodiscard]]
inline const char*
382 return (a_error == hipErrorNoBinaryForGpu)
383 ?
"\n hint: the binary has no code for this GPU; build for its architecture (--offload-arch, "
384 "HIP_ARCH or CMAKE_HIP_ARCHITECTURES)"
388#elif defined(EBGEOMETRY_CUDA)
390using Error = cudaError_t;
392using Stream = cudaStream_t;
399[[nodiscard]]
inline Error
400memAlloc(
void** a_devicePtr,
size_t a_numBytes)
noexcept
402 return cudaMalloc(a_devicePtr, a_numBytes);
405[[nodiscard]]
inline Error
406memFree(
void* a_devicePtr)
noexcept
408 return cudaFree(a_devicePtr);
411[[nodiscard]]
inline Error
414 return cudaMallocManaged(a_ptr, a_numBytes, cudaMemAttachGlobal);
417[[nodiscard]]
inline Error
420 return cudaHostAlloc(a_ptr, a_numBytes, cudaHostAllocDefault);
423[[nodiscard]]
inline Error
426 return cudaHostAlloc(a_ptr, a_numBytes, cudaHostAllocMapped);
429[[nodiscard]]
inline Error
432 return cudaFreeHost(a_ptr);
435[[nodiscard]]
inline Error
436memcpy(
void* a_dst,
const void* a_src,
size_t a_numBytes,
MemcpyKind a_kind)
noexcept
438 return cudaMemcpy(a_dst, a_src, a_numBytes, a_kind);
441[[nodiscard]]
inline Error
444 return cudaGetDeviceCount(a_count);
447[[nodiscard]]
inline Error
450 return cudaStreamCreateWithFlags(a_stream, cudaStreamNonBlocking);
453[[nodiscard]]
inline Error
456 return cudaStreamDestroy(a_stream);
459[[nodiscard]]
inline Error
462 return cudaStreamSynchronize(a_stream);
465[[nodiscard]]
inline Error
468 return cudaMemcpyAsync(a_dst, a_src, a_numBytes, a_kind, a_stream);
471[[nodiscard]]
inline Error
475 const auto error = cudaGetDevice(&device);
477 return (error != cudaSuccess) ? error : cudaDeviceGetAttribute(a_kiloHertz, cudaDevAttrClockRate, device);
480[[nodiscard]]
inline Error
483 return cudaGetLastError();
486[[nodiscard]]
inline Error
489 return cudaDeviceSynchronize();
492[[nodiscard]]
inline const char*
495 return cudaGetErrorString(a_error);
498[[nodiscard]]
inline bool
501 return a_error == cudaErrorCudartUnloading || a_error == cudaErrorContextIsDestroyed;
504[[nodiscard]]
inline const char*
507 return (a_error == cudaErrorUnsupportedPtxVersion || a_error == cudaErrorNoKernelImageForDevice)
508 ?
"\n hint: the binary has no machine code for this GPU, and the driver cannot compile its PTX; "
509 "build for the GPU's architecture (-arch, CUDA_ARCH or CMAKE_CUDA_ARCHITECTURES), or update "
527#define EBGEOMETRY_GPU_CHECK(x) \
529 const EBGeometry::GPU::Error ebgeometry_gpu_err = (x); \
530 if (ebgeometry_gpu_err != EBGeometry::GPU::Success) { \
531 std::fprintf(stderr, \
532 "EBGeometry GPU error: %s\n call: %s\n file: %s\n line: %d%s\n", \
533 EBGeometry::GPU::getErrorString(ebgeometry_gpu_err), \
537 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 getClockRate(int *a_kiloHertz) noexcept
Get the current device's clock rate, the rate at which clock64() counts in a kernel (cudaDeviceGetAtt...
Error streamSynchronize(Stream a_stream) noexcept
Wait until a stream's work is done (cudaStreamSynchronize / hipStreamSynchronize).
Error deviceSynchronize() noexcept
Block until the device has finished all preceding work (cudaDeviceSynchronize / hipDeviceSynchronize)...
Error memcpyAsync(void *a_dst, const void *a_src, size_t a_numBytes, MemcpyKind a_kind, Stream a_stream) noexcept
Copy memory between host and device on a stream, returning before the copy is done (cudaMemcpyAsync /...
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 streamCreateNonBlocking(Stream *a_stream) noexcept
Create a stream that runs alongside the default stream rather than waiting for its work (cudaStreamCr...
Error memFreeHost(void *a_ptr) noexcept
Free page-locked host memory allocated with memAllocHost (cudaFreeHost / hipHostFree).
Error streamDestroy(Stream a_stream) noexcept
Destroy a stream (cudaStreamDestroy / hipStreamDestroy).
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
void * Stream
A stream of device work (cudaStream_t / hipStream_t).
Definition EBGeometry_GPURuntime.hpp:167
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 MemcpyDeviceToDevice
Device-to-device copy direction (cudaMemcpyDeviceToDevice / hipMemcpyDeviceToDevice).
Definition EBGeometry_GPURuntime.hpp:86
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