-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgpu_runtime_api.hpp
More file actions
254 lines (233 loc) · 11.1 KB
/
gpu_runtime_api.hpp
File metadata and controls
254 lines (233 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*===================================================*
| GPU Array (gpu-array) version v0.3.0 |
| https://github.com/yosh-matsuda/gpu-array |
| |
| Copyright (c) 2026 Yoshiki Matsuda @yosh-matsuda |
| |
| This software is released under the MIT License. |
| https://opensource.org/license/mit/ |
====================================================*/
#pragma once
#if defined(__HIP_DEVICE_COMPILE__) || (defined(__CUDA_ARCH__) && __CUDA_ARCH__ != 0)
#define GPU_DEVICE_COMPILE
#endif
#if !defined(__NVCC__) || defined(__CUDA_ARCH__) || defined(_CLANGD)
#define GPU_OVERLOAD_DEVICE
#endif
#if !(defined(__NVCC__) && defined(__CUDA_ARCH__)) || defined(_CLANGD)
#define GPU_OVERLOAD_HOST
#endif
#ifdef ENABLE_HIP
#include <hip/hip_cooperative_groups.h>
#include <hip/hip_runtime.h>
#else
#include <cooperative_groups.h>
#include <cuda_runtime.h>
#endif
#include <sstream>
// NOLINTBEGIN
namespace gpu_array::api
{
#ifdef ENABLE_HIP
using gpuError_t = ::hipError_t;
#define gpuSuccess hipSuccess
__host__ inline const char* gpuGetErrorName(gpuError_t gpu_error) { return ::hipGetErrorName(gpu_error); }
__host__ inline const char* gpuGetErrorString(gpuError_t gpu_error) { return ::hipGetErrorString(gpu_error); }
using gpuMemcpyKind = ::hipMemcpyKind;
enum class gpuMemoryAdvise
{
SetReadMostly = hipMemAdviseSetReadMostly,
UnsetReadMostly = hipMemAdviseUnsetReadMostly,
SetPreferredLocation = hipMemAdviseSetPreferredLocation,
UnsetPreferredLocation = hipMemAdviseUnsetPreferredLocation,
SetAccessedBy = hipMemAdviseSetAccessedBy,
UnsetAccessedBy = hipMemAdviseUnsetAccessedBy,
};
#define gpuMemcpyHostToHost hipMemcpyHostToHost
#define gpuMemcpyHostToDevice hipMemcpyHostToDevice
#define gpuMemcpyDeviceToHost hipMemcpyDeviceToHost
#define gpuMemcpyDeviceToDevice hipMemcpyDeviceToDevice
#define gpuMemcpyDefault hipMemcpyDefault
#define gpuMemAttachGlobal hipMemAttachGlobal
#define gpuMemAttachHost hipMemAttachHost
#define gpuMemAttachSingle hipMemAttachSingle
#define gpuCpuDeviceId hipCpuDeviceId
#define gpuInvalidDeviceId hipInvalidDeviceId
using gpuStream_t = hipStream_t;
using gpuPointerAttributes = hipPointerAttribute_t;
enum class gpuMemoryType
{
Host = hipMemoryTypeHost,
Device = hipMemoryTypeDevice,
Managed = hipMemoryTypeManaged,
};
__host__ inline decltype(auto) gpuMalloc(void** ptr, std::size_t size) { return ::hipMalloc(ptr, size); }
__host__ inline decltype(auto) gpuMallocManaged(void** ptr, std::size_t size,
unsigned int flags = hipMemAttachGlobal)
{
return ::hipMallocManaged(ptr, size, flags);
}
__host__ inline decltype(auto) gpuFree(void* ptr) { return ::hipFree(ptr); }
__host__ inline decltype(auto) gpuMemcpy(void* dst, const void* src, std::size_t size, gpuMemcpyKind kind)
{
return ::hipMemcpy(dst, src, size, kind);
}
__host__ inline decltype(auto) gpuMemPrefetchAsync(const void* dev_ptr, std::size_t count, int device,
gpuStream_t stream = 0)
{
return ::hipMemPrefetchAsync(dev_ptr, count, device, stream);
}
__host__ inline decltype(auto) gpuMemAdvise(const void* devPtr, size_t count, gpuMemoryAdvise advice, int device)
{
return ::hipMemAdvise(devPtr, count, static_cast<hipMemoryAdvise>(advice), device);
}
__host__ inline decltype(auto) gpuDeviceSynchronize() { return ::hipDeviceSynchronize(); }
__host__ inline decltype(auto) gpuGetDeviceCount(int* count) { return ::hipGetDeviceCount(count); }
__host__ inline decltype(auto) gpuGetDevice(int* device) { return ::hipGetDevice(device); }
__host__ inline decltype(auto) gpuStreamCreate(gpuStream_t* stream) { return ::hipStreamCreate(stream); }
__host__ inline decltype(auto) gpuStreamDestroy(gpuStream_t stream) { return ::hipStreamDestroy(stream); }
__host__ inline decltype(auto) gpuStreamSynchronize(gpuStream_t stream) { return ::hipStreamSynchronize(stream); }
template <typename T>
__host__ decltype(auto) gpuOccupancyMaxActiveBlocksPerMultiprocessor(int* numBlocks, T f, int blockSize,
size_t dynSharedMemPerBlk)
{
return ::hipOccupancyMaxActiveBlocksPerMultiprocessor(numBlocks, f, blockSize, dynSharedMemPerBlk);
}
#ifdef __NVCC__
template <typename T>
__host__ inline decltype(auto) gpuOccupancyAvailableDynamicSMemPerBlock(size_t* dynamicSmem, T* f, int numBlocks,
int blockSize)
{
return hipCUDAErrorTohipError(
::cudaOccupancyAvailableDynamicSMemPerBlock(dynamicSmem, f, numBlocks, blockSize));
}
#endif
__host__ inline decltype(auto) gpuSetDevice(int device) { return ::hipSetDevice(device); }
__host__ inline decltype(auto) gpuGetLastError() { return ::hipGetLastError(); }
__host__ inline decltype(auto) gpuPointerGetAttributes(gpuPointerAttributes* attributes, const void* ptr)
{
return ::hipPointerGetAttributes(attributes, ptr);
}
#else
using gpuError_t = ::cudaError_t;
#define gpuSuccess cudaSuccess
__host__ inline const char* gpuGetErrorName(gpuError_t gpu_error) { return ::cudaGetErrorName(gpu_error); }
__host__ inline const char* gpuGetErrorString(gpuError_t gpu_error) { return ::cudaGetErrorString(gpu_error); }
using gpuMemcpyKind = ::cudaMemcpyKind;
enum class gpuMemoryAdvise
{
SetReadMostly = cudaMemAdviseSetReadMostly,
UnsetReadMostly = cudaMemAdviseUnsetReadMostly,
SetPreferredLocation = cudaMemAdviseSetPreferredLocation,
UnsetPreferredLocation = cudaMemAdviseUnsetPreferredLocation,
SetAccessedBy = cudaMemAdviseSetAccessedBy,
UnsetAccessedBy = cudaMemAdviseUnsetAccessedBy,
};
#define gpuMemcpyHostToHost cudaMemcpyHostToHost
#define gpuMemcpyHostToDevice cudaMemcpyHostToDevice
#define gpuMemcpyDeviceToHost cudaMemcpyDeviceToHost
#define gpuMemcpyDeviceToDevice cudaMemcpyDeviceToDevice
#define gpuMemcpyDefault cudaMemcpyDefault
#define gpuMemAttachGlobal cudaMemAttachGlobal
#define gpuMemAttachHost cudaMemAttachHost
#define gpuMemAttachSingle cudaMemAttachSingle
#define gpuCpuDeviceId cudaCpuDeviceId
#define gpuInvalidDeviceId cudaInvalidDeviceId
using gpuStream_t = cudaStream_t;
using gpuPointerAttributes = cudaPointerAttributes;
enum class gpuMemoryType
{
Host = cudaMemoryTypeHost,
Device = cudaMemoryTypeDevice,
Managed = cudaMemoryTypeManaged,
};
__host__ inline decltype(auto) gpuMalloc(void** devPtr, std::size_t size) { return ::cudaMalloc(devPtr, size); }
__host__ inline decltype(auto) gpuMallocManaged(void** devPtr, std::size_t size,
unsigned int flags = cudaMemAttachGlobal)
{
return ::cudaMallocManaged(devPtr, size, flags);
}
__host__ inline decltype(auto) gpuFree(void* devPtr) { return ::cudaFree(devPtr); }
__host__ inline decltype(auto) gpuMemcpy(void* dst, const void* src, std::size_t size, gpuMemcpyKind kind)
{
return ::cudaMemcpy(dst, src, size, kind);
}
__host__ inline decltype(auto) gpuMemPrefetchAsync(const void* devPtr, size_t count, int dstDevice,
gpuStream_t stream = 0)
{
#if !defined(__CUDACC_VER_MAJOR__) || __CUDACC_VER_MAJOR__ < 13
return ::cudaMemPrefetchAsync(devPtr, count, dstDevice, stream);
#else
return ::cudaMemPrefetchAsync(
devPtr, count,
{.type = dstDevice == cudaCpuDeviceId ? cudaMemLocationTypeHost : cudaMemLocationTypeDevice,
.id = dstDevice},
0, stream);
#endif
}
__host__ inline decltype(auto) gpuMemAdvise(const void* devPtr, size_t count, gpuMemoryAdvise advice, int device)
{
#if !defined(__CUDACC_VER_MAJOR__) || __CUDACC_VER_MAJOR__ < 13
return ::cudaMemAdvise(devPtr, count, static_cast<cudaMemoryAdvise>(advice), device);
#else
return ::cudaMemAdvise(
devPtr, count, static_cast<cudaMemoryAdvise>(advice),
{.type = device == cudaCpuDeviceId ? cudaMemLocationTypeHost : cudaMemLocationTypeDevice, .id = device});
#endif
}
__host__ inline decltype(auto) gpuDeviceSynchronize() { return ::cudaDeviceSynchronize(); }
__host__ inline decltype(auto) gpuGetDeviceCount(int* count) { return ::cudaGetDeviceCount(count); }
__host__ inline decltype(auto) gpuGetDevice(int* device) { return ::cudaGetDevice(device); }
__host__ inline decltype(auto) gpuStreamCreate(gpuStream_t* stream) { return ::cudaStreamCreate(stream); }
__host__ inline decltype(auto) gpuStreamDestroy(gpuStream_t stream) { return ::cudaStreamDestroy(stream); }
__host__ inline decltype(auto) gpuStreamSynchronize(gpuStream_t stream) { return ::cudaStreamSynchronize(stream); }
template <typename T>
__host__ decltype(auto) gpuOccupancyMaxActiveBlocksPerMultiprocessor(int* numBlocks, T f, int blockSize,
size_t dynSharedMemPerBlk)
{
return ::cudaOccupancyMaxActiveBlocksPerMultiprocessor(numBlocks, f, blockSize, dynSharedMemPerBlk);
}
template <typename T>
__host__ inline decltype(auto) gpuOccupancyAvailableDynamicSMemPerBlock(size_t* dynamicSmem, T* f, int numBlocks,
int blockSize)
{
return ::cudaOccupancyAvailableDynamicSMemPerBlock(dynamicSmem, f, numBlocks, blockSize);
}
__host__ inline decltype(auto) gpuSetDevice(int device) { return ::cudaSetDevice(device); }
__host__ inline decltype(auto) gpuGetLastError() { return ::cudaGetLastError(); }
__host__ inline decltype(auto) gpuPointerGetAttributes(gpuPointerAttributes* attributes, const void* ptr)
{
return ::cudaPointerGetAttributes(attributes, ptr);
}
#endif
} // namespace gpu_array::api
namespace gpu_array::detail
{
__host__ inline void check_gpu_error(const api::gpuError_t e)
{
using namespace gpu_array::api;
if (e != gpuSuccess)
{
std::stringstream s;
s << gpuGetErrorName(e) << " (" << static_cast<unsigned>(e) << "): " << gpuGetErrorString(e);
throw std::runtime_error{s.str()};
}
}
__host__ inline void check_gpu_error(const api::gpuError_t e, const char* f, decltype(__LINE__) n)
{
using namespace gpu_array::api;
if (e != gpuSuccess)
{
std::stringstream s;
s << gpuGetErrorName(e) << " (" << static_cast<unsigned>(e) << ")@" << f << "#L" << n << ": "
<< gpuGetErrorString(e);
throw std::runtime_error{s.str()};
}
}
} // namespace gpu_array::detail
#ifdef NDEBUG
#define GPU_CHECK_ERROR(expr) (gpu_array::detail::check_gpu_error(expr))
#else
#define GPU_CHECK_ERROR(expr) (gpu_array::detail::check_gpu_error(expr, __FILE__, __LINE__))
#endif
// NOLINTEND