alpaka
Abstraction Library for Parallel Kernel Acceleration
ApiHipRt.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Andrea Bocci
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include <boost/predef.h>
8 
9 #ifdef ALPAKA_ACC_GPU_HIP_ENABLED
10 
11 # include <hip/hip_runtime_api.h>
12 # include <hip/hip_version.h>
13 
14 namespace alpaka
15 {
16  struct ApiHipRt
17  {
18  // Names
19  static constexpr char name[] = "Hip";
20  static constexpr auto version = BOOST_VERSION_NUMBER(HIP_VERSION_MAJOR, HIP_VERSION_MINOR, 0);
21 
22  // Types
23  using DeviceAttr_t = ::hipDeviceAttribute_t;
24  using DeviceProp_t = ::hipDeviceProp_t;
25  using Error_t = ::hipError_t;
26  using Event_t = ::hipEvent_t;
27  using Extent_t = ::hipExtent;
28  using Flag_t = unsigned int;
29  using FuncAttributes_t = ::hipFuncAttributes;
30  using HostFn_t = void (*)(void* data); // same as hipHostFn_t
31  using Limit_t = ::hipLimit_t;
32  using Memcpy3DParms_t = ::hipMemcpy3DParms;
33  using MemcpyKind_t = ::hipMemcpyKind;
34  using PitchedPtr_t = ::hipPitchedPtr;
35  using Pos_t = ::hipPos;
36  using Stream_t = ::hipStream_t;
37 
38  // Constants
39  static constexpr Error_t success = ::hipSuccess;
40  static constexpr Error_t errorNotReady = ::hipErrorNotReady;
41  static constexpr Error_t errorHostMemoryAlreadyRegistered = ::hipErrorHostMemoryAlreadyRegistered;
42  static constexpr Error_t errorHostMemoryNotRegistered = ::hipErrorHostMemoryNotRegistered;
43  static constexpr Error_t errorUnsupportedLimit = ::hipErrorUnsupportedLimit;
44  static constexpr Error_t errorUnknown = ::hipErrorUnknown;
45 
46  static constexpr Flag_t eventDefault = hipEventDefault;
47  static constexpr Flag_t eventBlockingSync = hipEventBlockingSync;
48  static constexpr Flag_t eventDisableTiming = hipEventDisableTiming;
49  static constexpr Flag_t eventInterprocess = hipEventInterprocess;
50 
51  static constexpr Flag_t hostMallocDefault = hipHostMallocDefault;
52  static constexpr Flag_t hostMallocMapped = hipHostMallocMapped;
53  static constexpr Flag_t hostMallocPortable = hipHostMallocPortable;
54  static constexpr Flag_t hostMallocWriteCombined = hipHostMallocWriteCombined;
55  static constexpr Flag_t hostMallocCoherent = hipHostMallocCoherent;
56  static constexpr Flag_t hostMallocNonCoherent = hipHostMallocNonCoherent;
57 
58  static constexpr Flag_t hostRegisterDefault = hipHostRegisterDefault;
59  static constexpr Flag_t hostRegisterPortable = hipHostRegisterPortable;
60  static constexpr Flag_t hostRegisterMapped = hipHostRegisterMapped;
61  static constexpr Flag_t hostRegisterIoMemory = hipHostRegisterIoMemory;
62 
63  static constexpr MemcpyKind_t memcpyDefault = ::hipMemcpyDefault;
64  static constexpr MemcpyKind_t memcpyDeviceToDevice = ::hipMemcpyDeviceToDevice;
65  static constexpr MemcpyKind_t memcpyDeviceToHost = ::hipMemcpyDeviceToHost;
66  static constexpr MemcpyKind_t memcpyHostToDevice = ::hipMemcpyHostToDevice;
67 
68  static constexpr Flag_t streamDefault = hipStreamDefault;
69  static constexpr Flag_t streamNonBlocking = hipStreamNonBlocking;
70 
71  static constexpr DeviceAttr_t deviceAttributeMaxBlockDimX = ::hipDeviceAttributeMaxBlockDimX;
72  static constexpr DeviceAttr_t deviceAttributeMaxBlockDimY = ::hipDeviceAttributeMaxBlockDimY;
73  static constexpr DeviceAttr_t deviceAttributeMaxBlockDimZ = ::hipDeviceAttributeMaxBlockDimZ;
74  static constexpr DeviceAttr_t deviceAttributeMaxGridDimX = ::hipDeviceAttributeMaxGridDimX;
75  static constexpr DeviceAttr_t deviceAttributeMaxGridDimY = ::hipDeviceAttributeMaxGridDimY;
76  static constexpr DeviceAttr_t deviceAttributeMaxGridDimZ = ::hipDeviceAttributeMaxGridDimZ;
77  static constexpr DeviceAttr_t deviceAttributeMaxSharedMemoryPerBlock
78  = ::hipDeviceAttributeMaxSharedMemoryPerBlock;
79  static constexpr DeviceAttr_t deviceAttributeMaxThreadsPerBlock = ::hipDeviceAttributeMaxThreadsPerBlock;
80  static constexpr DeviceAttr_t deviceAttributeMultiprocessorCount = ::hipDeviceAttributeMultiprocessorCount;
81  static constexpr DeviceAttr_t deviceAttributeWarpSize = ::hipDeviceAttributeWarpSize;
82 
83 # if HIP_VERSION >= 40'500'000
84  static constexpr Limit_t limitPrintfFifoSize = ::hipLimitPrintfFifoSize;
85 # else
86  static constexpr Limit_t limitPrintfFifoSize
87  = static_cast<Limit_t>(0x01); // Implemented only in ROCm 4.5.0 and later.
88 # endif
89  static constexpr Limit_t limitMallocHeapSize = ::hipLimitMallocHeapSize;
90 
91  // Host function helper
92  // Encapsulates the different function signatures used by hipStreamAddCallback and hipLaunchHostFn, and the
93  // different calling conventions used by CUDA (__stdcall on Win32) and HIP (standard).
94  struct HostFnAdaptor
95  {
96  HostFn_t func_;
97  void* data_;
98 
99  static void hostFunction(void* data)
100  {
101  auto ptr = reinterpret_cast<HostFnAdaptor*>(data);
102  ptr->func_(ptr->data_);
103  delete ptr;
104  }
105 
106  static void streamCallback(Stream_t, Error_t, void* data)
107  {
108  auto ptr = reinterpret_cast<HostFnAdaptor*>(data);
109  ptr->func_(ptr->data_);
110  delete ptr;
111  }
112  };
113 
114  // Runtime API
115  static inline Error_t deviceGetAttribute(int* value, DeviceAttr_t attr, int device)
116  {
117  return ::hipDeviceGetAttribute(value, attr, device);
118  }
119 
120  static inline Error_t deviceGetLimit(size_t* pValue, Limit_t limit)
121  {
122 # if HIP_VERSION < 40'500'000
123  if(limit == limitPrintfFifoSize)
124  {
125  // Implemented only in ROCm 4.5.0 and later.
126  return errorUnsupportedLimit;
127  }
128 # endif
129  return ::hipDeviceGetLimit(pValue, limit);
130  }
131 
132  static inline Error_t deviceReset()
133  {
134  return ::hipDeviceReset();
135  }
136 
137  static inline Error_t deviceSetLimit(Limit_t /* limit */, size_t /* value */)
138  {
139  // Not implemented.
140  return errorUnsupportedLimit;
141  }
142 
143  static inline Error_t deviceSynchronize()
144  {
145  return ::hipDeviceSynchronize();
146  }
147 
148  static inline Error_t eventCreate(Event_t* event)
149  {
150  return ::hipEventCreate(event);
151  }
152 
153  static inline Error_t eventCreateWithFlags(Event_t* event, Flag_t flags)
154  {
155  return ::hipEventCreateWithFlags(event, flags);
156  }
157 
158  static inline Error_t eventDestroy(Event_t event)
159  {
160  return ::hipEventDestroy(event);
161  }
162 
163  static inline Error_t eventQuery(Event_t event)
164  {
165  return ::hipEventQuery(event);
166  }
167 
168  static inline Error_t eventRecord(Event_t event, Stream_t stream)
169  {
170  return ::hipEventRecord(event, stream);
171  }
172 
173  static inline Error_t eventSynchronize(Event_t event)
174  {
175  return ::hipEventSynchronize(event);
176  }
177 
178  static inline Error_t free(void* devPtr)
179  {
180  return ::hipFree(devPtr);
181  }
182 
183  static inline Error_t freeAsync([[maybe_unused]] void* devPtr, [[maybe_unused]] Stream_t stream)
184  {
185  // stream-ordered memory operations are fully implemented only in ROCm 5.3.0 and later.
186 # if HIP_VERSION >= 50'300'000
187  // hipFreeAsync fails on a null pointer deallocation
188  if(devPtr)
189  {
190  return ::hipFreeAsync(devPtr, stream);
191  }
192  else
193  {
194  return ::hipSuccess;
195  }
196 # else
197  // Not implemented.
198  return errorUnknown;
199 # endif
200  }
201 
202  static inline Error_t funcGetAttributes(FuncAttributes_t* attr, void const* func)
203  {
204  return ::hipFuncGetAttributes(attr, func);
205  }
206 
207  template<typename T>
208  static inline Error_t funcGetAttributes(FuncAttributes_t* attr, T* func)
209  {
210  return ::hipFuncGetAttributes(attr, reinterpret_cast<void const*>(func));
211  }
212 
213  static inline Error_t getDeviceCount(int* count)
214  {
215  return ::hipGetDeviceCount(count);
216  }
217 
218  static inline Error_t getDeviceProperties(DeviceProp_t* prop, int device)
219  {
220  return ::hipGetDeviceProperties(prop, device);
221  }
222 
223  static inline char const* getErrorName(Error_t error)
224  {
225  return ::hipGetErrorName(error);
226  }
227 
228  static inline char const* getErrorString(Error_t error)
229  {
230  return ::hipGetErrorString(error);
231  }
232 
233  static inline Error_t getLastError()
234  {
235  return ::hipGetLastError();
236  }
237 
238  static inline Error_t getSymbolAddress(void** devPtr, void const* symbol)
239  {
240  return ::hipGetSymbolAddress(devPtr, symbol);
241  }
242 
243  template<class T>
244  static inline Error_t getSymbolAddress(void** devPtr, T const& symbol)
245  {
246  return ::hipGetSymbolAddress(devPtr, symbol);
247  }
248 
249  static inline Error_t hostGetDevicePointer(void** pDevice, void* pHost, Flag_t flags)
250  {
251  return ::hipHostGetDevicePointer(pDevice, pHost, flags);
252  }
253 
254  static inline Error_t hostFree(void* ptr)
255  {
256  return ::hipHostFree(ptr);
257  }
258 
259  static inline Error_t hostMalloc(void** ptr, size_t size, Flag_t flags)
260  {
261  return ::hipHostMalloc(ptr, size, flags);
262  }
263 
264  static inline Error_t hostRegister(void* ptr, size_t size, Flag_t flags)
265  {
266  return ::hipHostRegister(ptr, size, flags);
267  }
268 
269  static inline Error_t hostUnregister(void* ptr)
270  {
271  return ::hipHostUnregister(ptr);
272  }
273 
274  static inline Error_t launchHostFunc(Stream_t stream, HostFn_t fn, void* userData)
275  {
276  // hipLaunchHostFunc is implemented only in ROCm 5.4.0 and later.
277 # if HIP_VERSION >= 50'400'000
278  // Wrap the host function using the proper calling convention.
279  return ::hipLaunchHostFunc(stream, HostFnAdaptor::hostFunction, new HostFnAdaptor{fn, userData});
280 # else
281  // Emulate hipLaunchHostFunc using hipStreamAddCallback with a callback adaptor.
282  return ::hipStreamAddCallback(stream, HostFnAdaptor::streamCallback, new HostFnAdaptor{fn, userData}, 0);
283 # endif
284  }
285 
286  static inline Error_t malloc(void** devPtr, size_t size)
287  {
288  return ::hipMalloc(devPtr, size);
289  }
290 
291  static inline Error_t malloc3D(PitchedPtr_t* pitchedDevPtr, Extent_t extent)
292  {
293  return ::hipMalloc3D(pitchedDevPtr, extent);
294  }
295 
296  static inline Error_t mallocAsync(
297  [[maybe_unused]] void** devPtr,
298  [[maybe_unused]] size_t size,
299  [[maybe_unused]] Stream_t stream)
300  {
301  // stream-ordered memory operations are fully implemented only in ROCm 5.3.0 and later.
302 # if HIP_VERSION >= 50'600'000
303  return ::hipMallocAsync(devPtr, size, stream);
304 # elif HIP_VERSION >= 50'300'000
305  // before ROCm 5.6.0, hipMallocAsync fails for an allocation of 0 bytes
306  if(size > 0)
307  {
308  return ::hipMallocAsync(devPtr, size, stream);
309  }
310  else
311  {
312  // make sure the pointer can safely be passed to hipFreeAsync
313  *devPtr = nullptr;
314  return ::hipSuccess;
315  }
316 # else
317  // Not implemented.
318  return errorUnknown;
319 # endif
320  }
321 
322  static inline Error_t mallocPitch(void** devPtr, size_t* pitch, size_t width, size_t height)
323  {
324  return ::hipMallocPitch(devPtr, pitch, width, height);
325  }
326 
327  static inline Error_t memGetInfo(size_t* free, size_t* total)
328  {
329  return ::hipMemGetInfo(free, total);
330  }
331 
332  static inline Error_t memcpy(void* dst, void const* src, size_t count, MemcpyKind_t kind)
333  {
334  return ::hipMemcpy(dst, src, count, kind);
335  }
336 
337  static inline Error_t memcpy2DAsync(
338  void* dst,
339  size_t dpitch,
340  void const* src,
341  size_t spitch,
342  size_t width,
343  size_t height,
344  MemcpyKind_t kind,
345  Stream_t stream)
346  {
347  return ::hipMemcpy2DAsync(dst, dpitch, src, spitch, width, height, kind, stream);
348  }
349 
350  static inline Error_t memcpy3DAsync(Memcpy3DParms_t const* p, Stream_t stream)
351  {
352  return ::hipMemcpy3DAsync(p, stream);
353  }
354 
355  static inline Error_t memcpyAsync(void* dst, void const* src, size_t count, MemcpyKind_t kind, Stream_t stream)
356  {
357  return ::hipMemcpyAsync(dst, src, count, kind, stream);
358  }
359 
360  static inline Error_t memset2DAsync(
361  void* devPtr,
362  size_t pitch,
363  int value,
364  size_t width,
365  size_t height,
366  Stream_t stream)
367  {
368  return ::hipMemset2DAsync(devPtr, pitch, value, width, height, stream);
369  }
370 
371  static inline Error_t memset3DAsync(PitchedPtr_t pitchedDevPtr, int value, Extent_t extent, Stream_t stream)
372  {
373  return ::hipMemset3DAsync(pitchedDevPtr, value, extent, stream);
374  }
375 
376  static inline Error_t memsetAsync(void* devPtr, int value, size_t count, Stream_t stream)
377  {
378  return ::hipMemsetAsync(devPtr, value, count, stream);
379  }
380 
381  static inline Error_t setDevice(int device)
382  {
383  return ::hipSetDevice(device);
384  }
385 
386  static inline Error_t streamCreate(Stream_t* pStream)
387  {
388  return ::hipStreamCreate(pStream);
389  }
390 
391  static inline Error_t streamCreateWithFlags(Stream_t* pStream, Flag_t flags)
392  {
393  return ::hipStreamCreateWithFlags(pStream, flags);
394  }
395 
396  static inline Error_t streamDestroy(Stream_t stream)
397  {
398  return ::hipStreamDestroy(stream);
399  }
400 
401  static inline Error_t streamQuery(Stream_t stream)
402  {
403  return ::hipStreamQuery(stream);
404  }
405 
406  static inline Error_t streamSynchronize(Stream_t stream)
407  {
408  return ::hipStreamSynchronize(stream);
409  }
410 
411  static inline Error_t streamWaitEvent(Stream_t stream, Event_t event, Flag_t flags)
412  {
413  return ::hipStreamWaitEvent(stream, event, flags);
414  }
415 
416  static inline PitchedPtr_t makePitchedPtr(void* d, size_t p, size_t xsz, size_t ysz)
417  {
418  return ::make_hipPitchedPtr(d, p, xsz, ysz);
419  }
420 
421  static inline Pos_t makePos(size_t x, size_t y, size_t z)
422  {
423  return ::make_hipPos(x, y, z);
424  }
425 
426  static inline Extent_t makeExtent(size_t w, size_t h, size_t d)
427  {
428  return ::make_hipExtent(w, h, d);
429  }
430  };
431 
432 } // namespace alpaka
433 
434 #endif // ALPAKA_ACC_GPU_HIP_ENABLED
The alpaka accelerator library.
ALPAKA_FN_HOST auto free(TAlloc const &alloc, T const *const ptr) -> void
Frees the memory identified by the given pointer.
Definition: Traits.hpp:41
ALPAKA_FN_HOST auto memcpy(TQueue &queue, alpaka::detail::DevGlobalImplGeneric< TTag, TTypeDst > &viewDst, TViewSrc const &viewSrc) -> void
ALPAKA_FN_HOST auto malloc(TAlloc const &alloc, std::size_t const &sizeElems) -> T *
Definition: Traits.hpp:33