Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
GPUPortability.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include "core/CompilerPortability.hpp" // IWYU pragma: export
12#include <cstddef> // IWYU pragma: keep
13#include <utility> // IWYU pragma: keep
14
15#ifdef CORRELATION_USE_HIP
16#include <hip/hip_runtime.h>
17#elif defined(CORRELATION_USE_CUDA)
18#include <cuda_runtime.h>
19
20// Map HIP API names to CUDA equivalents
21using hipError_t = cudaError_t;
22#define hipSuccess cudaSuccess
23#define hipGetDeviceCount cudaGetDeviceCount
24#define hipMalloc cudaMalloc
25#define hipFree cudaFree
26#define hipMemcpy cudaMemcpy
27#define hipMemcpyHostToDevice cudaMemcpyHostToDevice
28#define hipMemcpyDeviceToHost cudaMemcpyDeviceToHost
29#define hipDeviceSynchronize cudaDeviceSynchronize
30#define hipGetErrorString cudaGetErrorString
31
32#ifdef __CUDACC__
44template <typename K, typename... Args>
45inline void hipLaunchKernelGGL(K kernel, dim3 grid, dim3 block, std::size_t shared,
46 cudaStream_t stream, Args &&...args) {
47 kernel<<<grid, block, shared, stream>>>(std::forward<Args>(args)...);
48}
49#endif
50
51#else
52
54using hipError_t = int;
56constexpr int hipSuccess = 0;
58constexpr int hipErrorNoDevice = 100;
60constexpr int hipErrorNotSupported = 801;
61
67inline hipError_t hipGetDeviceCount(int *count) noexcept {
68 if (count != nullptr) {
69 *count = 0;
70 }
71 return hipSuccess;
72}
73
81template <typename T>
82inline hipError_t hipMalloc(T **ptr, [[maybe_unused]] std::size_t size) noexcept {
83 if (ptr != nullptr) {
84 *ptr = nullptr;
85 }
86 return hipErrorNoDevice;
87}
88
94inline hipError_t hipFree([[maybe_unused]] void *ptr) noexcept { return hipSuccess; }
95
100 void *dst{nullptr};
101 const void *src{nullptr};
102 std::size_t count{0};
103 int kind{0};
104};
105
111inline hipError_t hipMemcpy([[maybe_unused]] MemcpyParams params) noexcept {
112 return hipErrorNoDevice;
113}
114
123inline hipError_t hipMemcpy(void *dst, const void *src, std::size_t count, int kind) noexcept {
124 return hipMemcpy(MemcpyParams{
125 .dst = dst,
126 .src = src,
127 .count = count,
128 .kind = kind,
129 });
130}
131
133constexpr int hipMemcpyHostToDevice = 0;
135constexpr int hipMemcpyDeviceToHost = 0;
136
142
148inline const char *hipGetErrorString(hipError_t err) noexcept {
149 switch (err) {
150 case hipSuccess:
151 return "Success";
152 case hipErrorNoDevice:
153 return "No GPU device available (compiled without CUDA/HIP support)";
155 return "GPU operation not supported in host fallback mode";
156 default:
157 return "Unknown GPU error";
158 }
159}
160
169template <typename T, typename U> inline T atomicAdd(T *addr, U val) noexcept {
170 if (addr == nullptr) {
171 return T{};
172 }
173 T const old = *addr;
174 *addr += static_cast<T>(val);
175 return old;
176}
177
181struct dim3 {
182 unsigned int x{1};
183 unsigned int y{1};
184 unsigned int z{1};
185};
186
188inline constexpr dim3 threadIdx{
189 .x = 0,
190 .y = 0,
191 .z = 0,
192};
193
194inline constexpr dim3 blockIdx{
195 .x = 0,
196 .y = 0,
197 .z = 0,
198};
199
200inline constexpr dim3 blockDim{
201 .x = 1,
202 .y = 1,
203 .z = 1,
204};
205
221template <typename K, typename Grid, typename Block, typename Shared, typename Stream,
222 typename... Args>
223constexpr void hipLaunchKernelGGL([[maybe_unused]] K &&kernel, [[maybe_unused]] Grid &&grid,
224 [[maybe_unused]] Block &&block, [[maybe_unused]] Shared &&shared,
225 [[maybe_unused]] Stream &&stream,
226 [[maybe_unused]] Args &&...args) noexcept {}
227
228#endif
Unified compiler extensions, alignment, restrict, and GPU execution qualifiers.
constexpr dim3 threadIdx
Emulated thread index inside thread block.
Definition GPUPortability.hpp:188
constexpr int hipErrorNoDevice
Device unavailable error code.
Definition GPUPortability.hpp:58
constexpr dim3 blockDim
Emulated block dimension.
Definition GPUPortability.hpp:200
int hipError_t
Return error type for host fallback emulation.
Definition GPUPortability.hpp:54
constexpr int hipErrorNotSupported
Operation not supported in host fallback mode.
Definition GPUPortability.hpp:60
hipError_t hipFree(void *ptr) noexcept
Host fallback deallocation stub.
Definition GPUPortability.hpp:94
constexpr int hipMemcpyDeviceToHost
Device-to-host memory copy transfer direction flag.
Definition GPUPortability.hpp:135
hipError_t hipGetDeviceCount(int *count) noexcept
Returns the number of compute-capable GPU devices.
Definition GPUPortability.hpp:67
constexpr int hipSuccess
Successful execution code.
Definition GPUPortability.hpp:56
hipError_t hipMemcpy(MemcpyParams params) noexcept
Host fallback memory copy stub.
Definition GPUPortability.hpp:111
hipError_t hipMalloc(T **ptr, std::size_t size) noexcept
Host fallback allocation stub returning failure when GPU support is disabled.
Definition GPUPortability.hpp:82
const char * hipGetErrorString(hipError_t err) noexcept
Translates an error code to a human-readable description.
Definition GPUPortability.hpp:148
constexpr int hipMemcpyHostToDevice
Host-to-device memory copy transfer direction flag.
Definition GPUPortability.hpp:133
T atomicAdd(T *addr, U val) noexcept
Host fallback atomic addition.
Definition GPUPortability.hpp:169
constexpr void hipLaunchKernelGGL(K &&kernel, Grid &&grid, Block &&block, Shared &&shared, Stream &&stream, Args &&...args) noexcept
Host fallback stub for kernel launch when GPU acceleration is disabled.
Definition GPUPortability.hpp:223
hipError_t hipDeviceSynchronize() noexcept
Host fallback device synchronization stub.
Definition GPUPortability.hpp:141
constexpr dim3 blockIdx
Emulated block index inside grid.
Definition GPUPortability.hpp:194
Memory copy parameter struct for host fallback.
Definition GPUPortability.hpp:99
const void * src
Definition GPUPortability.hpp:101
void * dst
Definition GPUPortability.hpp:100
int kind
Definition GPUPortability.hpp:103
std::size_t count
Definition GPUPortability.hpp:102
3D vector dimension descriptor for CUDA/HIP emulation.
Definition GPUPortability.hpp:181
unsigned int z
Definition GPUPortability.hpp:184
unsigned int x
Definition GPUPortability.hpp:182
unsigned int y
Definition GPUPortability.hpp:183