EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_GPURuntime.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
23#ifndef EBGEOMETRY_GPURUNTIME_HPP
24#define EBGEOMETRY_GPURUNTIME_HPP
25
26// Our includes
27#include "EBGeometry_GPU.hpp"
28
29#if defined(EBGEOMETRY_CUDA) || defined(EBGEOMETRY_HIP) || defined(EBGEOMETRY_DOXYGEN)
30
31// Std includes (EBGEOMETRY_GPU_CHECK needs fprintf/abort; the wrappers need size_t).
32#include <cstddef>
33#include <cstdio>
34#include <cstdlib>
35
36// Backend runtime header. HIP is checked FIRST: hipcc on the NVIDIA platform defines both
37// __HIPCC__ and __CUDACC__, and there the hip* entry points exist and forward to cuda*, so
38// mapping to hip* whenever __HIPCC__ is defined is correct on both HIP platforms.
39#if defined(EBGEOMETRY_HIP)
40#include <hip/hip_runtime.h> // NOT pre-included by the compiler driver; must be explicit.
41#elif defined(EBGEOMETRY_CUDA)
42#include <cuda_runtime.h> // nvcc pre-includes this; spelled out for hygiene.
43#endif
44
45namespace EBGeometry {
46
54namespace GPU {
55
56#if defined(EBGEOMETRY_DOXYGEN)
57
61using Error = int;
62
66using MemcpyKind = int;
67
71inline constexpr Error Success = 0;
72
76inline constexpr MemcpyKind MemcpyHostToDevice = 1;
77
81inline constexpr MemcpyKind MemcpyDeviceToHost = 2;
82
86inline constexpr MemcpyKind MemcpyDeviceToDevice = 3;
87
94[[nodiscard]] inline Error
95memAlloc(void** a_devicePtr, size_t a_numBytes) noexcept;
96
102[[nodiscard]] inline Error
103memFree(void* a_devicePtr) noexcept;
104
112[[nodiscard]] inline Error
113memAllocManaged(void** a_ptr, size_t a_numBytes) noexcept;
114
121[[nodiscard]] inline Error
122memAllocHost(void** a_ptr, size_t a_numBytes) noexcept;
123
133[[nodiscard]] inline Error
134memAllocHostMapped(void** a_ptr, size_t a_numBytes) noexcept;
135
142[[nodiscard]] inline Error
143memFreeHost(void* a_ptr) noexcept;
144
153[[nodiscard]] inline Error
154memcpy(void* a_dst, const void* a_src, size_t a_numBytes, MemcpyKind a_kind) noexcept;
155
161[[nodiscard]] inline Error
162getDeviceCount(int* a_count) noexcept;
163
167using Stream = void*;
168
176[[nodiscard]] inline Error
177streamCreateNonBlocking(Stream* a_stream) noexcept;
178
184[[nodiscard]] inline Error
185streamDestroy(Stream a_stream) noexcept;
186
192[[nodiscard]] inline Error
193streamSynchronize(Stream a_stream) noexcept;
194
205[[nodiscard]] inline Error
206memcpyAsync(void* a_dst, const void* a_src, size_t a_numBytes, MemcpyKind a_kind, Stream a_stream) noexcept;
207
215[[nodiscard]] inline Error
216getClockRate(int* a_kiloHertz) noexcept;
217
222[[nodiscard]] inline Error
223getLastError() noexcept;
224
230[[nodiscard]] inline Error
232
239[[nodiscard]] inline const char*
240getErrorString(Error a_error) noexcept;
241
249[[nodiscard]] inline bool
250isShutdownError(Error a_error) noexcept;
251
260[[nodiscard]] inline const char*
261getErrorHint(Error a_error) noexcept;
262
263#elif defined(EBGEOMETRY_HIP)
264
265using Error = hipError_t;
266using MemcpyKind = hipMemcpyKind;
267using Stream = hipStream_t;
268
269inline constexpr Error Success = hipSuccess;
270inline constexpr MemcpyKind MemcpyHostToDevice = hipMemcpyHostToDevice;
271inline constexpr MemcpyKind MemcpyDeviceToHost = hipMemcpyDeviceToHost;
272inline constexpr MemcpyKind MemcpyDeviceToDevice = hipMemcpyDeviceToDevice;
273
274[[nodiscard]] inline Error
275memAlloc(void** a_devicePtr, size_t a_numBytes) noexcept
276{
277 return hipMalloc(a_devicePtr, a_numBytes);
278}
279
280[[nodiscard]] inline Error
281memFree(void* a_devicePtr) noexcept
282{
283 return hipFree(a_devicePtr);
284}
285
286[[nodiscard]] inline Error
287memAllocManaged(void** a_ptr, size_t a_numBytes) noexcept
288{
289 return hipMallocManaged(a_ptr, a_numBytes);
290}
291
292[[nodiscard]] inline Error
293memAllocHost(void** a_ptr, size_t a_numBytes) noexcept
294{
295 return hipHostMalloc(a_ptr, a_numBytes, 0u);
296}
297
298[[nodiscard]] inline Error
299memAllocHostMapped(void** a_ptr, size_t a_numBytes) noexcept
300{
301 return hipHostMalloc(a_ptr, a_numBytes, hipHostMallocMapped);
302}
303
304[[nodiscard]] inline Error
305memFreeHost(void* a_ptr) noexcept
306{
307 return hipHostFree(a_ptr);
308}
309
310[[nodiscard]] inline Error
311memcpy(void* a_dst, const void* a_src, size_t a_numBytes, MemcpyKind a_kind) noexcept
312{
313 return hipMemcpy(a_dst, a_src, a_numBytes, a_kind);
314}
315
316[[nodiscard]] inline Error
317getDeviceCount(int* a_count) noexcept
318{
319 return hipGetDeviceCount(a_count);
320}
321
322[[nodiscard]] inline Error
323streamCreateNonBlocking(Stream* a_stream) noexcept
324{
325 return hipStreamCreateWithFlags(a_stream, hipStreamNonBlocking);
326}
327
328[[nodiscard]] inline Error
329streamDestroy(Stream a_stream) noexcept
330{
331 return hipStreamDestroy(a_stream);
332}
333
334[[nodiscard]] inline Error
335streamSynchronize(Stream a_stream) noexcept
336{
337 return hipStreamSynchronize(a_stream);
338}
339
340[[nodiscard]] inline Error
341memcpyAsync(void* a_dst, const void* a_src, size_t a_numBytes, MemcpyKind a_kind, Stream a_stream) noexcept
342{
343 return hipMemcpyAsync(a_dst, a_src, a_numBytes, a_kind, a_stream);
344}
345
346[[nodiscard]] inline Error
347getClockRate(int* a_kiloHertz) noexcept
348{
349 int device = 0;
350 const auto error = hipGetDevice(&device);
351
352 return (error != hipSuccess) ? error : hipDeviceGetAttribute(a_kiloHertz, hipDeviceAttributeClockRate, device);
353}
354
355[[nodiscard]] inline Error
356getLastError() noexcept
357{
358 return hipGetLastError();
359}
360
361[[nodiscard]] inline Error
362deviceSynchronize() noexcept
363{
364 return hipDeviceSynchronize();
365}
366
367[[nodiscard]] inline const char*
368getErrorString(Error a_error) noexcept
369{
370 return hipGetErrorString(a_error);
371}
372
373[[nodiscard]] inline bool
374isShutdownError(Error a_error) noexcept
375{
376 return a_error == hipErrorDeinitialized || a_error == hipErrorContextIsDestroyed;
377}
378
379[[nodiscard]] inline const char*
380getErrorHint(Error a_error) noexcept
381{
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)"
385 : "";
386}
387
388#elif defined(EBGEOMETRY_CUDA)
389
390using Error = cudaError_t;
391using MemcpyKind = cudaMemcpyKind;
392using Stream = cudaStream_t;
393
394inline constexpr Error Success = cudaSuccess;
395inline constexpr MemcpyKind MemcpyHostToDevice = cudaMemcpyHostToDevice;
396inline constexpr MemcpyKind MemcpyDeviceToHost = cudaMemcpyDeviceToHost;
397inline constexpr MemcpyKind MemcpyDeviceToDevice = cudaMemcpyDeviceToDevice;
398
399[[nodiscard]] inline Error
400memAlloc(void** a_devicePtr, size_t a_numBytes) noexcept
401{
402 return cudaMalloc(a_devicePtr, a_numBytes);
403}
404
405[[nodiscard]] inline Error
406memFree(void* a_devicePtr) noexcept
407{
408 return cudaFree(a_devicePtr);
409}
410
411[[nodiscard]] inline Error
412memAllocManaged(void** a_ptr, size_t a_numBytes) noexcept
413{
414 return cudaMallocManaged(a_ptr, a_numBytes, cudaMemAttachGlobal);
415}
416
417[[nodiscard]] inline Error
418memAllocHost(void** a_ptr, size_t a_numBytes) noexcept
419{
420 return cudaHostAlloc(a_ptr, a_numBytes, cudaHostAllocDefault);
421}
422
423[[nodiscard]] inline Error
424memAllocHostMapped(void** a_ptr, size_t a_numBytes) noexcept
425{
426 return cudaHostAlloc(a_ptr, a_numBytes, cudaHostAllocMapped);
427}
428
429[[nodiscard]] inline Error
430memFreeHost(void* a_ptr) noexcept
431{
432 return cudaFreeHost(a_ptr);
433}
434
435[[nodiscard]] inline Error
436memcpy(void* a_dst, const void* a_src, size_t a_numBytes, MemcpyKind a_kind) noexcept
437{
438 return cudaMemcpy(a_dst, a_src, a_numBytes, a_kind);
439}
440
441[[nodiscard]] inline Error
442getDeviceCount(int* a_count) noexcept
443{
444 return cudaGetDeviceCount(a_count);
445}
446
447[[nodiscard]] inline Error
448streamCreateNonBlocking(Stream* a_stream) noexcept
449{
450 return cudaStreamCreateWithFlags(a_stream, cudaStreamNonBlocking);
451}
452
453[[nodiscard]] inline Error
454streamDestroy(Stream a_stream) noexcept
455{
456 return cudaStreamDestroy(a_stream);
457}
458
459[[nodiscard]] inline Error
460streamSynchronize(Stream a_stream) noexcept
461{
462 return cudaStreamSynchronize(a_stream);
463}
464
465[[nodiscard]] inline Error
466memcpyAsync(void* a_dst, const void* a_src, size_t a_numBytes, MemcpyKind a_kind, Stream a_stream) noexcept
467{
468 return cudaMemcpyAsync(a_dst, a_src, a_numBytes, a_kind, a_stream);
469}
470
471[[nodiscard]] inline Error
472getClockRate(int* a_kiloHertz) noexcept
473{
474 int device = 0;
475 const auto error = cudaGetDevice(&device);
476
477 return (error != cudaSuccess) ? error : cudaDeviceGetAttribute(a_kiloHertz, cudaDevAttrClockRate, device);
478}
479
480[[nodiscard]] inline Error
481getLastError() noexcept
482{
483 return cudaGetLastError();
484}
485
486[[nodiscard]] inline Error
487deviceSynchronize() noexcept
488{
489 return cudaDeviceSynchronize();
490}
491
492[[nodiscard]] inline const char*
493getErrorString(Error a_error) noexcept
494{
495 return cudaGetErrorString(a_error);
496}
497
498[[nodiscard]] inline bool
499isShutdownError(Error a_error) noexcept
500{
501 return a_error == cudaErrorCudartUnloading || a_error == cudaErrorContextIsDestroyed;
502}
503
504[[nodiscard]] inline const char*
505getErrorHint(Error a_error) noexcept
506{
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 "
510 "the driver"
511 : "";
512}
513
514#endif
515
516} // namespace GPU
517} // namespace EBGeometry
518
527#define EBGEOMETRY_GPU_CHECK(x) \
528 do { \
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), \
534 #x, \
535 __FILE__, \
536 __LINE__, \
537 EBGeometry::GPU::getErrorHint(ebgeometry_gpu_err)); \
538 std::abort(); \
539 } \
540 } while (0)
541
542#endif // EBGEOMETRY_CUDA || EBGEOMETRY_HIP || EBGEOMETRY_DOXYGEN
543
544#endif // EBGEOMETRY_GPURUNTIME_HPP
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