device/hip/api.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <filesystem> | ||
| 7 | #include <string> | ||
| 8 | |||
| 9 | #include "chrono_batch_render/device/compute/runtime_compiler.h" | ||
| 10 | #include "chrono_batch_render/util/version.h" | ||
| 11 | |||
| 12 | namespace chrono::batch_render::device::hip { | ||
| 13 | |||
| 14 | using hipError_t = int; | ||
| 15 | using hipDevice_t = int; | ||
| 16 | using hipDeviceptr_t = void*; | ||
| 17 | using hipStream_t = struct ihipStream_t*; | ||
| 18 | using hipEvent_t = struct ihipEvent_t*; | ||
| 19 | using hipModule_t = struct ihipModule_t*; | ||
| 20 | using hipFunction_t = struct ihipModuleSymbol_t*; | ||
| 21 | using hipMemPool_t = struct ihipMemPoolHandle_t*; | ||
| 22 | |||
| 23 | struct hipMemLocation { | ||
| 24 | int type; | ||
| 25 | int id; | ||
| 26 | }; | ||
| 27 | |||
| 28 | struct hipMemPoolProps { | ||
| 29 | int allocType; | ||
| 30 | int handleTypes; | ||
| 31 | hipMemLocation location; | ||
| 32 | void* win32SecurityAttributes; | ||
| 33 | size_t maxSize; | ||
| 34 | unsigned char reserved[56]; | ||
| 35 | }; | ||
| 36 | |||
| 37 | using hipGraph_t = struct ihipGraph*; | ||
| 38 | using hipGraphExec_t = struct hipGraphExec*; | ||
| 39 | |||
| 40 | struct hipUUID { | ||
| 41 | char bytes[16]; | ||
| 42 | }; | ||
| 43 | |||
| 44 | using hipExternalSemaphore_t = void*; | ||
| 45 | constexpr int hipExternalSemaphoreHandleTypeTimelineSemaphoreFd = 9; | ||
| 46 | |||
| 47 | struct hipExternalSemaphoreHandleDesc { | ||
| 48 | int type; | ||
| 49 | |||
| 50 | union { | ||
| 51 | int fd; | ||
| 52 | |||
| 53 | struct { | ||
| 54 | void* handle; | ||
| 55 | const void* name; | ||
| 56 | } win32; | ||
| 57 | |||
| 58 | const void* NvSciSyncObj; | ||
| 59 | } handle; | ||
| 60 | |||
| 61 | unsigned flags; | ||
| 62 | unsigned reserved[16]; | ||
| 63 | }; | ||
| 64 | |||
| 65 | struct hipExternalSemaphoreSignalParams { | ||
| 66 | struct { | ||
| 67 | struct { | ||
| 68 | unsigned long long value; | ||
| 69 | } fence; | ||
| 70 | |||
| 71 | union { | ||
| 72 | void* fence; | ||
| 73 | unsigned long long reserved; | ||
| 74 | } nvSciSync; | ||
| 75 | |||
| 76 | struct { | ||
| 77 | unsigned long long key; | ||
| 78 | } keyedMutex; | ||
| 79 | |||
| 80 | unsigned reserved[12]; | ||
| 81 | } params; | ||
| 82 | |||
| 83 | unsigned flags; | ||
| 84 | unsigned reserved[16]; | ||
| 85 | }; | ||
| 86 | |||
| 87 | struct hipExternalSemaphoreWaitParams { | ||
| 88 | struct { | ||
| 89 | struct { | ||
| 90 | unsigned long long value; | ||
| 91 | } fence; | ||
| 92 | |||
| 93 | union { | ||
| 94 | void* fence; | ||
| 95 | unsigned long long reserved; | ||
| 96 | } nvSciSync; | ||
| 97 | |||
| 98 | struct { | ||
| 99 | unsigned long long key; | ||
| 100 | unsigned timeoutMs; | ||
| 101 | } keyedMutex; | ||
| 102 | |||
| 103 | unsigned reserved[10]; | ||
| 104 | } params; | ||
| 105 | |||
| 106 | unsigned flags; | ||
| 107 | unsigned reserved[16]; | ||
| 108 | }; | ||
| 109 | |||
| 110 | using hipDeviceAttribute_t = int; | ||
| 111 | |||
| 112 | using hipExternalMemory_t = void*; | ||
| 113 | constexpr int hipExternalMemoryHandleTypeOpaqueFd = 1; | ||
| 114 | constexpr unsigned hipExternalMemoryDedicated = 0x1; | ||
| 115 | |||
| 116 | struct hipExternalMemoryHandleDesc { | ||
| 117 | int type; | ||
| 118 | |||
| 119 | union { | ||
| 120 | int fd; | ||
| 121 | |||
| 122 | struct { | ||
| 123 | void* handle; | ||
| 124 | const void* name; | ||
| 125 | } win32; | ||
| 126 | |||
| 127 | const void* nvSciBufObject; | ||
| 128 | } handle; | ||
| 129 | |||
| 130 | unsigned long long size; | ||
| 131 | unsigned flags; | ||
| 132 | unsigned reserved[16]; | ||
| 133 | }; | ||
| 134 | |||
| 135 | static_assert(sizeof(hipExternalMemoryHandleDesc) == 104); | ||
| 136 | |||
| 137 | struct hipExternalMemoryBufferDesc { | ||
| 138 | unsigned long long offset; | ||
| 139 | unsigned long long size; | ||
| 140 | unsigned flags; | ||
| 141 | unsigned reserved[16]; | ||
| 142 | }; | ||
| 143 | |||
| 144 | static_assert(sizeof(hipExternalMemoryBufferDesc) == 88); | ||
| 145 | |||
| 146 | constexpr hipError_t hipSuccess = 0; | ||
| 147 | constexpr hipError_t hipErrorOutOfMemory = 2; | ||
| 148 | constexpr hipError_t hipErrorNoDevice = 100; | ||
| 149 | constexpr hipError_t hipErrorNotFound = 500; | ||
| 150 | constexpr hipError_t hipErrorNotReady = 600; | ||
| 151 | |||
| 152 | constexpr unsigned hipHostMallocMapped = 2; | ||
| 153 | constexpr unsigned hipHostMallocWriteCombined = 4; | ||
| 154 | constexpr unsigned hipStreamNonBlocking = 1; | ||
| 155 | constexpr unsigned hipEventDisableTiming = 2; | ||
| 156 | constexpr int hipMemAllocationTypePinned = 1; | ||
| 157 | constexpr int hipMemLocationTypeDevice = 1; | ||
| 158 | constexpr int hipMemPoolAttrReleaseThreshold = 4; | ||
| 159 | constexpr unsigned hipEventRecordExternal = 1; | ||
| 160 | constexpr int hipStreamCaptureModeThreadLocal = 1; | ||
| 161 | constexpr int hipMemcpyDefault = 4; | ||
| 162 | |||
| 163 | constexpr int HIP_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES = 1; | ||
| 164 | constexpr int HIP_FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES = 3; | ||
| 165 | constexpr int HIP_FUNC_ATTRIBUTE_NUM_REGS = 4; | ||
| 166 | |||
| 167 | constexpr uintptr_t HIP_LAUNCH_PARAM_BUFFER_POINTER = 1; | ||
| 168 | constexpr uintptr_t HIP_LAUNCH_PARAM_BUFFER_SIZE = 2; | ||
| 169 | constexpr uintptr_t HIP_LAUNCH_PARAM_END = 3; | ||
| 170 | |||
| 171 | ✗ | constexpr bool LostDevice(hipError_t error) { | |
| 172 | ✗ | return error == 214 || error == 700 || error == 702 || (error >= 714 && error <= 719); | |
| 173 | } | ||
| 174 | |||
| 175 | constexpr hipDeviceAttribute_t hipDeviceAttributeIntegrated = 16; | ||
| 176 | constexpr hipDeviceAttribute_t hipDeviceAttributeMaxThreadsPerBlock = 56; | ||
| 177 | constexpr hipDeviceAttribute_t hipDeviceAttributeMultiprocessorCount = 63; | ||
| 178 | constexpr hipDeviceAttribute_t hipDeviceAttributeWarpSize = 87; | ||
| 179 | constexpr hipDeviceAttribute_t hipDeviceAttributeMemoryPoolsSupported = 88; | ||
| 180 | constexpr hipDeviceAttribute_t hipDeviceAttributeImageSupport = 10014; | ||
| 181 | |||
| 182 | constexpr size_t kDevicePropertiesR0600Bytes = 1472; | ||
| 183 | constexpr size_t kDevicePropertiesR0600ArchNameOffset = 1160; | ||
| 184 | |||
| 185 | constexpr Version kMinimumRuntimeVersion{6, 2}; | ||
| 186 | |||
| 187 | ✗ | constexpr Version DecodeVersion(int packed) { | |
| 188 | ✗ | return {packed / 10000000, packed / 100000 % 100, packed % 100000}; | |
| 189 | } | ||
| 190 | |||
| 191 | struct HIP_ARRAY3D_DESCRIPTOR { | ||
| 192 | size_t Width; | ||
| 193 | size_t Height; | ||
| 194 | size_t Depth; | ||
| 195 | int Format; | ||
| 196 | unsigned NumChannels; | ||
| 197 | unsigned Flags; | ||
| 198 | }; | ||
| 199 | |||
| 200 | struct HIP_RESOURCE_DESC { | ||
| 201 | int resType; | ||
| 202 | |||
| 203 | union { | ||
| 204 | struct { | ||
| 205 | void* hMipmappedArray; | ||
| 206 | } mipmap; | ||
| 207 | |||
| 208 | int reserved[32]; | ||
| 209 | } res; | ||
| 210 | |||
| 211 | unsigned flags; | ||
| 212 | }; | ||
| 213 | |||
| 214 | struct HIP_TEXTURE_DESC { | ||
| 215 | int addressMode[3]; | ||
| 216 | int filterMode; | ||
| 217 | unsigned flags; | ||
| 218 | unsigned maxAnisotropy; | ||
| 219 | int mipmapFilterMode; | ||
| 220 | float mipmapLevelBias; | ||
| 221 | float minMipmapLevelClamp; | ||
| 222 | float maxMipmapLevelClamp; | ||
| 223 | float borderColor[4]; | ||
| 224 | int reserved[12]; | ||
| 225 | }; | ||
| 226 | |||
| 227 | struct hip_Memcpy2D { | ||
| 228 | size_t srcXInBytes; | ||
| 229 | size_t srcY; | ||
| 230 | int srcMemoryType; | ||
| 231 | const void* srcHost; | ||
| 232 | hipDeviceptr_t srcDevice; | ||
| 233 | void* srcArray; | ||
| 234 | size_t srcPitch; | ||
| 235 | size_t dstXInBytes; | ||
| 236 | size_t dstY; | ||
| 237 | int dstMemoryType; | ||
| 238 | void* dstHost; | ||
| 239 | hipDeviceptr_t dstDevice; | ||
| 240 | void* dstArray; | ||
| 241 | size_t dstPitch; | ||
| 242 | size_t WidthInBytes; | ||
| 243 | size_t Height; | ||
| 244 | }; | ||
| 245 | |||
| 246 | constexpr int hipMemoryTypeDevice = 2; | ||
| 247 | constexpr int hipMemoryTypeArray = 10; | ||
| 248 | |||
| 249 | struct Api { | ||
| 250 | std::string library; | ||
| 251 | std::filesystem::path directory; | ||
| 252 | Version runtimeVersion; | ||
| 253 | Version driverVersion; | ||
| 254 | |||
| 255 | hipError_t (*hipInit)(unsigned flags) = nullptr; | ||
| 256 | hipError_t (*hipRuntimeGetVersion)(int* version) = nullptr; | ||
| 257 | hipError_t (*hipDriverGetVersion)(int* version) = nullptr; | ||
| 258 | hipError_t (*hipGetDeviceCount)(int* count) = nullptr; | ||
| 259 | const char* (*hipGetErrorName)(hipError_t error) = nullptr; | ||
| 260 | const char* (*hipGetErrorString)(hipError_t error) = nullptr; | ||
| 261 | hipError_t (*hipGetLastError)() = nullptr; | ||
| 262 | hipError_t (*hipPeekAtLastError)() = nullptr; | ||
| 263 | hipError_t (*hipDeviceGetName)(char* name, int length, hipDevice_t device) = nullptr; | ||
| 264 | hipError_t (*hipDeviceGetUuid)(hipUUID* uuid, hipDevice_t device) = nullptr; | ||
| 265 | hipError_t (*hipDeviceTotalMem)(size_t* bytes, hipDevice_t device) = nullptr; | ||
| 266 | hipError_t (*hipDeviceGetAttribute)(int* value, hipDeviceAttribute_t attribute, | ||
| 267 | int device) = nullptr; | ||
| 268 | hipError_t (*hipGetDevicePropertiesR0600)(void* properties, int device) = nullptr; | ||
| 269 | hipError_t (*hipGetDevice)(int* device) = nullptr; | ||
| 270 | hipError_t (*hipSetDevice)(int device) = nullptr; | ||
| 271 | hipError_t (*hipMalloc)(void** pointer, size_t bytes) = nullptr; | ||
| 272 | hipError_t (*hipFree)(void* pointer) = nullptr; | ||
| 273 | hipError_t (*hipMemGetInfo)(size_t* free, size_t* total) = nullptr; | ||
| 274 | hipError_t (*hipMemPoolCreate)(hipMemPool_t* pool, const hipMemPoolProps* properties) = nullptr; | ||
| 275 | hipError_t (*hipMemPoolDestroy)(hipMemPool_t pool) = nullptr; | ||
| 276 | hipError_t (*hipMemPoolSetAttribute)(hipMemPool_t pool, int attribute, void* value) = nullptr; | ||
| 277 | hipError_t (*hipMallocFromPoolAsync)(void** pointer, size_t bytes, hipMemPool_t pool, | ||
| 278 | hipStream_t stream) = nullptr; | ||
| 279 | hipError_t (*hipFreeAsync)(void* pointer, hipStream_t stream) = nullptr; | ||
| 280 | hipError_t (*hipHostMalloc)(void** pointer, size_t bytes, unsigned flags) = nullptr; | ||
| 281 | hipError_t (*hipHostFree)(void* pointer) = nullptr; | ||
| 282 | hipError_t (*hipHostGetDevicePointer)(void** device, void* host, unsigned flags) = nullptr; | ||
| 283 | hipError_t (*hipStreamCreateWithFlags)(hipStream_t* stream, unsigned flags) = nullptr; | ||
| 284 | hipError_t (*hipStreamDestroy)(hipStream_t stream) = nullptr; | ||
| 285 | hipError_t (*hipStreamSynchronize)(hipStream_t stream) = nullptr; | ||
| 286 | hipError_t (*hipEventCreateWithFlags)(hipEvent_t* event, unsigned flags) = nullptr; | ||
| 287 | hipError_t (*hipEventDestroy)(hipEvent_t event) = nullptr; | ||
| 288 | hipError_t (*hipEventQuery)(hipEvent_t event) = nullptr; | ||
| 289 | hipError_t (*hipEventSynchronize)(hipEvent_t event) = nullptr; | ||
| 290 | hipError_t (*hipEventRecordWithFlags)(hipEvent_t event, hipStream_t stream, | ||
| 291 | unsigned flags) = nullptr; | ||
| 292 | hipError_t (*hipStreamWaitEvent)(hipStream_t stream, hipEvent_t event, | ||
| 293 | unsigned flags) = nullptr; | ||
| 294 | hipError_t (*hipLaunchHostFunc)(hipStream_t stream, void (*function)(void*), | ||
| 295 | void* data) = nullptr; | ||
| 296 | hipError_t (*hipImportExternalSemaphore)(hipExternalSemaphore_t* semaphore, | ||
| 297 | const hipExternalSemaphoreHandleDesc* desc) = nullptr; | ||
| 298 | hipError_t (*hipDestroyExternalSemaphore)(hipExternalSemaphore_t semaphore) = nullptr; | ||
| 299 | hipError_t (*hipImportExternalMemory)(hipExternalMemory_t* memory, | ||
| 300 | const hipExternalMemoryHandleDesc* desc) = nullptr; | ||
| 301 | hipError_t (*hipExternalMemoryGetMappedBuffer)(void** pointer, hipExternalMemory_t memory, | ||
| 302 | const hipExternalMemoryBufferDesc* desc) = | ||
| 303 | nullptr; | ||
| 304 | hipError_t (*hipDestroyExternalMemory)(hipExternalMemory_t memory) = nullptr; | ||
| 305 | hipError_t (*hipMemcpyDtoH)(void* dst, hipDeviceptr_t src, size_t bytes) = nullptr; | ||
| 306 | hipError_t (*hipSignalExternalSemaphoresAsync)(const hipExternalSemaphore_t* semaphores, | ||
| 307 | const hipExternalSemaphoreSignalParams* params, | ||
| 308 | unsigned count, hipStream_t stream) = nullptr; | ||
| 309 | hipError_t (*hipWaitExternalSemaphoresAsync)(const hipExternalSemaphore_t* semaphores, | ||
| 310 | const hipExternalSemaphoreWaitParams* params, | ||
| 311 | unsigned count, hipStream_t stream) = nullptr; | ||
| 312 | hipError_t (*hipEventElapsedTime)(float* milliseconds, hipEvent_t from, | ||
| 313 | hipEvent_t to) = nullptr; | ||
| 314 | hipError_t (*hipMemcpyAsync)(void* dst, const void* src, size_t bytes, int kind, | ||
| 315 | hipStream_t stream) = nullptr; | ||
| 316 | hipError_t (*hipMemsetD32Async)(hipDeviceptr_t dst, int value, size_t count, | ||
| 317 | hipStream_t stream) = nullptr; | ||
| 318 | hipError_t (*hipMemcpyHtoDAsync)(hipDeviceptr_t dst, const void* src, size_t bytes, | ||
| 319 | hipStream_t stream) = nullptr; | ||
| 320 | hipError_t (*hipModuleLoadData)(hipModule_t* module, const void* image) = nullptr; | ||
| 321 | hipError_t (*hipModuleUnload)(hipModule_t module) = nullptr; | ||
| 322 | hipError_t (*hipModuleGetGlobal)(hipDeviceptr_t* pointer, size_t* bytes, hipModule_t module, | ||
| 323 | const char* name) = nullptr; | ||
| 324 | hipError_t (*hipMemcpyHtoD)(hipDeviceptr_t dst, const void* src, size_t bytes) = nullptr; | ||
| 325 | hipError_t (*hipModuleGetFunction)(hipFunction_t* function, hipModule_t module, | ||
| 326 | const char* name) = nullptr; | ||
| 327 | hipError_t (*hipFuncGetAttribute)(int* value, int attribute, hipFunction_t function) = nullptr; | ||
| 328 | hipError_t (*hipModuleLaunchKernel)(hipFunction_t function, unsigned groupsX, unsigned groupsY, | ||
| 329 | unsigned groupsZ, unsigned threadsX, unsigned threadsY, | ||
| 330 | unsigned threadsZ, unsigned sharedBytes, hipStream_t stream, | ||
| 331 | void** params, void** extra) = nullptr; | ||
| 332 | hipError_t (*hipStreamBeginCapture)(hipStream_t stream, int mode) = nullptr; | ||
| 333 | hipError_t (*hipStreamEndCapture)(hipStream_t stream, hipGraph_t* graph) = nullptr; | ||
| 334 | hipError_t (*hipGraphInstantiateWithFlags)(hipGraphExec_t* exec, hipGraph_t graph, | ||
| 335 | unsigned long long flags) = nullptr; | ||
| 336 | hipError_t (*hipGraphLaunch)(hipGraphExec_t exec, hipStream_t stream) = nullptr; | ||
| 337 | hipError_t (*hipGraphExecDestroy)(hipGraphExec_t exec) = nullptr; | ||
| 338 | hipError_t (*hipGraphDestroy)(hipGraph_t graph) = nullptr; | ||
| 339 | hipError_t (*hipMipmappedArrayCreate)(void** array, const HIP_ARRAY3D_DESCRIPTOR* desc, | ||
| 340 | unsigned levels) = nullptr; | ||
| 341 | hipError_t (*hipMipmappedArrayGetLevel)(void** level, void* array, unsigned index) = nullptr; | ||
| 342 | hipError_t (*hipMipmappedArrayDestroy)(void* array) = nullptr; | ||
| 343 | hipError_t (*hipMemcpyParam2DAsync)(const hip_Memcpy2D* copy, hipStream_t stream) = nullptr; | ||
| 344 | hipError_t (*hipTexObjectCreate)(void** object, const HIP_RESOURCE_DESC* resource, | ||
| 345 | const HIP_TEXTURE_DESC* sampler, const void* view) = nullptr; | ||
| 346 | hipError_t (*hipTexObjectDestroy)(void* object) = nullptr; | ||
| 347 | |||
| 348 | const char* ErrorName(hipError_t error) const; | ||
| 349 | |||
| 350 | void Warn(hipError_t error, const char* what) const; | ||
| 351 | |||
| 352 | void Check(hipError_t error, const char* what) const; | ||
| 353 | }; | ||
| 354 | |||
| 355 | bool Available(); | ||
| 356 | |||
| 357 | const Api& GetApi(); | ||
| 358 | |||
| 359 | const compute::RuntimeCompiler& GetHiprtc(); | ||
| 360 | |||
| 361 | } | ||
| 362 |