Actual source code: cupminterface.hpp
1: #pragma once
3: #include <petscdevice_cupm.h>
4: #include <petscpkg_version.h>
6: #include <petsc/private/cpputil.hpp>
8: #include <petsc/private/cpp/array.hpp>
10: namespace Petsc
11: {
13: namespace device
14: {
16: namespace cupm
17: {
19: // enum describing available cupm devices, this is used as the template parameter to any
20: // class subclassing the Interface or using it as a member variable
21: enum class DeviceType : int {
22: CUDA,
23: HIP
24: };
26: // clang-format off
27: static constexpr std::array<const char *const, 5> DeviceTypes = {
28: "cuda",
29: "hip",
30: "Petsc::device::cupm::DeviceType",
31: "Petsc::device::cupm::DeviceType::",
32: nullptr
33: };
34: // clang-format on
36: namespace impl
37: {
39: #define PetscCallCUPM_(__abort_fn__, __comm__, ...) \
40: do { \
41: PetscStackUpdateLine; \
42: const cupmError_t cerr_p_ = __VA_ARGS__; \
43: __abort_fn__(cerr_p_ == cupmSuccess, __comm__, PETSC_ERR_GPU, "%s error %d (%s) : %s", cupmName(), static_cast<PetscErrorCode>(cerr_p_), cupmGetErrorName(cerr_p_), cupmGetErrorString(cerr_p_)); \
44: } while (0)
46: // A backend agnostic PetscCallCUPM() function, this will only work inside the member
47: // functions of a class inheriting from CUPM::Interface. Thanks to __VA_ARGS__ templated
48: // functions can also be wrapped inline:
49: //
50: // PetscCallCUPM(foo());
51: #define PetscCallCUPM(...) PetscCallCUPM_(PetscCheck, PETSC_COMM_SELF, __VA_ARGS__)
52: #define PetscCallCUPMAbort(comm_, ...) PetscCallCUPM_(PetscCheckAbort, comm_, __VA_ARGS__)
54: // PETSC_CUPM_ALIAS_FUNCTION() - declaration to alias a cuda/hip function
55: //
56: // input params:
57: // our_name - the name of the alias
58: // their_name - the name of the function being aliased
59: //
60: // notes:
61: // see PETSC_ALIAS_FUNCTION() for the exact nature of the expansion
62: //
63: // example usage:
64: // PETSC_CUPM_ALIAS_FUNCTION(cupmMalloc, cudaMalloc) ->
65: // template
66: // static constexpr auto cupmMalloc(T&&... args) *noexcept and trailing return type deduction*
67: // {
68: // return cudaMalloc(std::forward(args)...);
69: // }
70: //
71: // PETSC_CUPM_ALIAS_FUNCTION(cupmMalloc, hipMalloc) ->
72: // template
73: // static constexpr auto cupmMalloc(T&&... args) *noexcept and trailing return type deduction*
74: // {
75: // return hipMalloc(std::forward(args)...);
76: // }
77: #define PETSC_CUPM_ALIAS_FUNCTION(our_name, their_name) PETSC_ALIAS_FUNCTION(static our_name, their_name)
79: // Base class that holds functions and variables that don't require CUDA or HIP to be present
80: // on the system
81: template <DeviceType T>
82: struct InterfaceBase {
83: static const DeviceType type = T;
85: PETSC_NODISCARD static constexpr const char *cupmName() noexcept
86: {
87: static_assert(util::to_underlying(DeviceType::CUDA) == 0, "");
88: static_assert(util::to_underlying(DeviceType::HIP) == 1, "");
89: return std::get<util::to_underlying(T)>(DeviceTypes);
90: }
92: PETSC_NODISCARD static constexpr const char *cupmNAME() noexcept { return T == DeviceType::CUDA ? "CUDA" : "HIP"; }
94: PETSC_NODISCARD static constexpr PetscDeviceType PETSC_DEVICE_CUPM() noexcept { return T == DeviceType::CUDA ? PETSC_DEVICE_CUDA : PETSC_DEVICE_HIP; }
96: PETSC_NODISCARD static constexpr PetscMemType PETSC_MEMTYPE_CUPM() noexcept { return T == DeviceType::CUDA ? PETSC_MEMTYPE_CUDA : PETSC_MEMTYPE_HIP; }
97: };
99: // declare the base class static member variables
100: template <DeviceType T>
101: const DeviceType InterfaceBase<T>::type;
103: #define PETSC_CUPM_BASE_CLASS_HEADER(T) \
104: using ::Petsc::device::cupm::impl::InterfaceBase<T>::type; \
105: using ::Petsc::device::cupm::impl::InterfaceBase<T>::cupmName; \
106: using ::Petsc::device::cupm::impl::InterfaceBase<T>::cupmNAME; \
107: using ::Petsc::device::cupm::impl::InterfaceBase<T>::PETSC_DEVICE_CUPM; \
108: using ::Petsc::device::cupm::impl::InterfaceBase<T>::PETSC_MEMTYPE_CUPM
110: // A templated C++ struct that defines the entire CUPM interface. Use of templating vs
111: // preprocessor macros allows us to use both interfaces simultaneously as well as easily
112: // import them into classes.
113: template <DeviceType>
114: struct InterfaceImpl;
116: #if PetscDefined(HAVE_CUDA)
117: template <>
118: struct PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL InterfaceImpl<DeviceType::CUDA> : InterfaceBase<DeviceType::CUDA> {
119: PETSC_CUPM_BASE_CLASS_HEADER(DeviceType::CUDA);
121: // typedefs
122: using cupmError_t = cudaError_t;
123: using cupmEvent_t = cudaEvent_t;
124: using cupmStream_t = cudaStream_t;
125: using cupmDeviceProp_t = cudaDeviceProp;
126: using cupmMemcpyKind_t = cudaMemcpyKind;
127: using cupmDeviceAttr_t = cudaDeviceAttr;
128: using cupmComplex_t = util::conditional_t<PetscDefined(USE_REAL_SINGLE), cuComplex, cuDoubleComplex>;
129: using cupmPointerAttributes_t = cudaPointerAttributes;
130: using cupmMemoryType_t = enum cudaMemoryType;
131: using cupmDim3 = dim3;
132: using cupmHostFn_t = cudaHostFn_t;
133: using cupmMemPool_t = cudaMemPool_t;
134: using cupmMemPoolAttr = cudaMemPoolAttr;
136: // values
137: static const auto cupmSuccess = cudaSuccess;
138: static const auto cupmErrorNotReady = cudaErrorNotReady;
139: static const auto cupmErrorDeviceAlreadyInUse = cudaErrorDeviceAlreadyInUse;
140: static const auto cupmErrorSetOnActiveProcess = cudaErrorSetOnActiveProcess;
141: static const auto cupmErrorStubLibrary = cudaErrorStubLibrary;
143: static const auto cupmErrorNoDevice = cudaErrorNoDevice;
144: static const auto cupmStreamDefault = cudaStreamDefault;
145: static const auto cupmStreamNonBlocking = cudaStreamNonBlocking;
146: static const auto cupmDeviceMapHost = cudaDeviceMapHost;
147: static const auto cupmMemcpyHostToDevice = cudaMemcpyHostToDevice;
148: static const auto cupmMemcpyDeviceToHost = cudaMemcpyDeviceToHost;
149: static const auto cupmMemcpyDeviceToDevice = cudaMemcpyDeviceToDevice;
150: static const auto cupmMemcpyHostToHost = cudaMemcpyHostToHost;
151: static const auto cupmMemcpyDefault = cudaMemcpyDefault;
152: static const auto cupmMemoryTypeHost = cudaMemoryTypeHost;
153: static const auto cupmMemoryTypeDevice = cudaMemoryTypeDevice;
154: static const auto cupmMemoryTypeManaged = cudaMemoryTypeManaged;
155: static const auto cupmEventDisableTiming = cudaEventDisableTiming;
156: static const auto cupmHostAllocDefault = cudaHostAllocDefault;
157: static const auto cupmHostAllocWriteCombined = cudaHostAllocWriteCombined;
158: static const auto cupmMemPoolAttrReleaseThreshold = cudaMemPoolAttrReleaseThreshold;
159: static const auto cupmDevAttrClockRate = cudaDevAttrClockRate;
160: static const auto cupmDevAttrMemoryClockRate = cudaDevAttrMemoryClockRate;
162: // error functions
163: PETSC_CUPM_ALIAS_FUNCTION(cupmGetErrorName, cudaGetErrorName)
164: PETSC_CUPM_ALIAS_FUNCTION(cupmGetErrorString, cudaGetErrorString)
165: PETSC_CUPM_ALIAS_FUNCTION(cupmGetLastError, cudaGetLastError)
167: // device management
168: PETSC_CUPM_ALIAS_FUNCTION(cupmGetDeviceCount, cudaGetDeviceCount)
169: PETSC_CUPM_ALIAS_FUNCTION(cupmGetDeviceProperties, cudaGetDeviceProperties)
170: PETSC_CUPM_ALIAS_FUNCTION(cupmDeviceGetAttribute, cudaDeviceGetAttribute)
171: PETSC_CUPM_ALIAS_FUNCTION(cupmGetDevice, cudaGetDevice)
172: PETSC_CUPM_ALIAS_FUNCTION(cupmSetDevice, cudaSetDevice)
173: PETSC_CUPM_ALIAS_FUNCTION(cupmGetDeviceFlags, cudaGetDeviceFlags)
174: PETSC_CUPM_ALIAS_FUNCTION(cupmSetDeviceFlags, cudaSetDeviceFlags)
175: PETSC_CUPM_ALIAS_FUNCTION(cupmPointerGetAttributes, cudaPointerGetAttributes)
176: PETSC_CUPM_ALIAS_FUNCTION(cupmDeviceGetPCIBusId, cudaDeviceGetPCIBusId)
178: PETSC_NODISCARD static cupmMemoryType_t cupmPointerGetMemType(const cupmPointerAttributes_t &attr, PetscBool *managed) noexcept
179: {
180: if (managed) *managed = static_cast<PetscBool>(attr.type == cupmMemoryTypeManaged);
181: return attr.type;
182: }
184: PETSC_CUPM_ALIAS_FUNCTION(cupmDeviceGetMemPool, cudaDeviceGetMemPool)
185: PETSC_CUPM_ALIAS_FUNCTION(cupmMemPoolSetAttribute, cudaMemPoolSetAttribute)
186: // CUDA has no cudaInit() to match hipInit()
187: PETSC_NODISCARD static cupmError_t cupmInit(unsigned int) noexcept { return cudaFree(nullptr); }
189: // stream management
190: PETSC_CUPM_ALIAS_FUNCTION(cupmEventCreate, cudaEventCreate)
191: PETSC_CUPM_ALIAS_FUNCTION(cupmEventCreateWithFlags, cudaEventCreateWithFlags)
192: PETSC_CUPM_ALIAS_FUNCTION(cupmEventDestroy, cudaEventDestroy)
193: PETSC_CUPM_ALIAS_FUNCTION(cupmEventRecord, cudaEventRecord)
194: PETSC_CUPM_ALIAS_FUNCTION(cupmEventSynchronize, cudaEventSynchronize)
195: PETSC_CUPM_ALIAS_FUNCTION(cupmEventElapsedTime, cudaEventElapsedTime)
196: PETSC_CUPM_ALIAS_FUNCTION(cupmEventQuery, cudaEventQuery)
197: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamCreate, cudaStreamCreate)
198: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamCreateWithFlags, cudaStreamCreateWithFlags)
199: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamGetFlags, cudaStreamGetFlags)
200: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamDestroy, cudaStreamDestroy)
201: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamWaitEvent, cudaStreamWaitEvent)
202: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamQuery, cudaStreamQuery)
203: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamSynchronize, cudaStreamSynchronize)
204: PETSC_CUPM_ALIAS_FUNCTION(cupmDeviceSynchronize, cudaDeviceSynchronize)
205: PETSC_CUPM_ALIAS_FUNCTION(cupmGetSymbolAddress, cudaGetSymbolAddress)
207: // memory management
208: PETSC_CUPM_ALIAS_FUNCTION(cupmFree, cudaFree)
209: PETSC_CUPM_ALIAS_FUNCTION(cupmMalloc, cudaMalloc)
210: PETSC_CUPM_ALIAS_FUNCTION(cupmFreeAsync, cudaFreeAsync)
211: PETSC_CUPM_ALIAS_FUNCTION(cupmMallocAsync, cudaMallocAsync)
212: PETSC_CUPM_ALIAS_FUNCTION(cupmMemcpy, cudaMemcpy)
213: PETSC_CUPM_ALIAS_FUNCTION(cupmMemcpyAsync, cudaMemcpyAsync)
214: PETSC_CUPM_ALIAS_FUNCTION(cupmMallocHost, cudaMallocHost)
215: PETSC_CUPM_ALIAS_FUNCTION(cupmFreeHost, cudaFreeHost)
216: PETSC_CUPM_ALIAS_FUNCTION(cupmMemset, cudaMemset)
217: PETSC_CUPM_ALIAS_FUNCTION(cupmMemsetAsync, cudaMemsetAsync)
218: PETSC_CUPM_ALIAS_FUNCTION(cupmMemcpy2D, cudaMemcpy2D)
219: PETSC_CUPM_ALIAS_FUNCTION(cupmMemcpy2DAsync, cudaMemcpy2DAsync)
220: PETSC_CUPM_ALIAS_FUNCTION(cupmMemset2D, cudaMemset2D)
221: PETSC_CUPM_ALIAS_FUNCTION(cupmMemset2DAsync, cudaMemset2DAsync)
223: // launch control
224: PETSC_CUPM_ALIAS_FUNCTION(cupmLaunchHostFunc, cudaLaunchHostFunc)
225: template <typename FunctionT, typename... KernelArgsT>
226: PETSC_NODISCARD static cudaError_t cupmLaunchKernel(FunctionT &&func, dim3 gridDim, dim3 blockDim, std::size_t sharedMem, cudaStream_t stream, KernelArgsT &&...kernelArgs) noexcept
227: {
228: static_assert(!std::is_pointer<FunctionT>::value, "kernel function must not be passed by pointer");
229: void *args[] = {(void *)std::addressof(kernelArgs)...};
231: return cudaLaunchKernel<util::remove_reference_t<FunctionT>>(std::addressof(func), std::move(gridDim), std::move(blockDim), args, sharedMem, std::move(stream));
232: }
233: };
234: #endif // PetscDefined(HAVE_CUDA)
236: #if PetscDefined(HAVE_HIP)
237: template <>
238: struct PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL InterfaceImpl<DeviceType::HIP> : InterfaceBase<DeviceType::HIP> {
239: PETSC_CUPM_BASE_CLASS_HEADER(DeviceType::HIP);
241: // typedefs
242: using cupmError_t = hipError_t;
243: using cupmEvent_t = hipEvent_t;
244: using cupmStream_t = hipStream_t;
245: using cupmDeviceProp_t = hipDeviceProp_t;
246: using cupmMemcpyKind_t = hipMemcpyKind;
247: using cupmDeviceAttr_t = hipDeviceAttribute_t;
248: using cupmComplex_t = util::conditional_t<PetscDefined(USE_REAL_SINGLE), hipComplex, hipDoubleComplex>;
249: using cupmPointerAttributes_t = hipPointerAttribute_t;
250: using cupmMemoryType_t = enum hipMemoryType;
251: using cupmDim3 = dim3;
252: using cupmHostFn_t = hipHostFn_t;
253: using cupmMemPool_t = hipMemPool_t;
254: using cupmMemPoolAttr = hipMemPoolAttr;
256: // values
257: static const auto cupmSuccess = hipSuccess;
258: static const auto cupmErrorNotReady = hipErrorNotReady;
259: // see https://github.com/ROCm-Developer-Tools/HIP/blob/develop/bin/hipify-perl
260: static const auto cupmErrorDeviceAlreadyInUse = hipErrorContextAlreadyInUse;
261: static const auto cupmErrorSetOnActiveProcess = hipErrorSetOnActiveProcess;
262: // as of HIP v4.2 cudaErrorStubLibrary has no HIP equivalent
263: static const auto cupmErrorStubLibrary = hipErrorInsufficientDriver;
264: static const auto cupmErrorNoDevice = hipErrorNoDevice;
265: static const auto cupmStreamDefault = hipStreamDefault;
266: static const auto cupmStreamNonBlocking = hipStreamNonBlocking;
267: static const auto cupmDeviceMapHost = hipDeviceMapHost;
268: static const auto cupmMemcpyHostToDevice = hipMemcpyHostToDevice;
269: static const auto cupmMemcpyDeviceToHost = hipMemcpyDeviceToHost;
270: static const auto cupmMemcpyDeviceToDevice = hipMemcpyDeviceToDevice;
271: static const auto cupmMemcpyHostToHost = hipMemcpyHostToHost;
272: static const auto cupmMemcpyDefault = hipMemcpyDefault;
273: static const auto cupmMemoryTypeHost = hipMemoryTypeHost;
274: static const auto cupmMemoryTypeDevice = hipMemoryTypeDevice;
275: // see
276: // https://github.com/ROCm-Developer-Tools/HIP/blob/develop/include/hip/hip_runtime_api.h#L156
277: static const auto cupmMemoryTypeManaged = hipMemoryTypeUnified;
278: static const auto cupmEventDisableTiming = hipEventDisableTiming;
279: static const auto cupmHostAllocDefault = hipHostMallocDefault;
280: static const auto cupmHostAllocWriteCombined = hipHostMallocWriteCombined;
281: static const auto cupmMemPoolAttrReleaseThreshold = hipMemPoolAttrReleaseThreshold;
282: static const auto cupmDevAttrClockRate = hipDeviceAttributeClockRate;
283: static const auto cupmDevAttrMemoryClockRate = hipDeviceAttributeMemoryClockRate;
285: // error functions
286: PETSC_CUPM_ALIAS_FUNCTION(cupmGetErrorName, hipGetErrorName)
287: PETSC_CUPM_ALIAS_FUNCTION(cupmGetErrorString, hipGetErrorString)
288: PETSC_CUPM_ALIAS_FUNCTION(cupmGetLastError, hipGetLastError)
290: // device management
291: PETSC_CUPM_ALIAS_FUNCTION(cupmGetDeviceCount, hipGetDeviceCount)
292: PETSC_CUPM_ALIAS_FUNCTION(cupmGetDeviceProperties, hipGetDeviceProperties)
293: PETSC_CUPM_ALIAS_FUNCTION(cupmDeviceGetAttribute, hipDeviceGetAttribute)
294: PETSC_CUPM_ALIAS_FUNCTION(cupmGetDevice, hipGetDevice)
295: PETSC_CUPM_ALIAS_FUNCTION(cupmSetDevice, hipSetDevice)
296: PETSC_CUPM_ALIAS_FUNCTION(cupmGetDeviceFlags, hipGetDeviceFlags)
297: PETSC_CUPM_ALIAS_FUNCTION(cupmSetDeviceFlags, hipSetDeviceFlags)
298: PETSC_CUPM_ALIAS_FUNCTION(cupmPointerGetAttributes, hipPointerGetAttributes)
299: PETSC_CUPM_ALIAS_FUNCTION(cupmDeviceGetPCIBusId, hipDeviceGetPCIBusId)
301: PETSC_NODISCARD static cupmMemoryType_t cupmPointerGetMemType(const cupmPointerAttributes_t &attr, PetscBool *managed) noexcept
302: {
303: // HIP used memoryType on both backends before 5.5.
304: #if PETSC_PKG_HIP_VERSION_LT(5, 5, 0)
305: if (managed) *managed = static_cast<PetscBool>(attr.isManaged);
306: return attr.memoryType;
307: #else
308: if (managed) *managed = static_cast<PetscBool>(attr.type == cupmMemoryTypeManaged);
309: return attr.type;
310: #endif
311: }
313: PETSC_CUPM_ALIAS_FUNCTION(cupmDeviceGetMemPool, hipDeviceGetMemPool)
314: PETSC_CUPM_ALIAS_FUNCTION(cupmMemPoolSetAttribute, hipMemPoolSetAttribute)
315: PETSC_CUPM_ALIAS_FUNCTION(cupmInit, hipInit)
317: // stream management
318: PETSC_CUPM_ALIAS_FUNCTION(cupmEventCreate, hipEventCreate)
319: PETSC_CUPM_ALIAS_FUNCTION(cupmEventCreateWithFlags, hipEventCreateWithFlags)
320: PETSC_CUPM_ALIAS_FUNCTION(cupmEventDestroy, hipEventDestroy)
321: PETSC_CUPM_ALIAS_FUNCTION(cupmEventRecord, hipEventRecord)
322: PETSC_CUPM_ALIAS_FUNCTION(cupmEventSynchronize, hipEventSynchronize)
323: PETSC_CUPM_ALIAS_FUNCTION(cupmEventElapsedTime, hipEventElapsedTime)
324: PETSC_CUPM_ALIAS_FUNCTION(cupmEventQuery, hipEventQuery)
325: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamCreate, hipStreamCreate)
326: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamCreateWithFlags, hipStreamCreateWithFlags)
327: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamGetFlags, hipStreamGetFlags)
328: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamDestroy, hipStreamDestroy)
329: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamWaitEvent, hipStreamWaitEvent)
330: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamQuery, hipStreamQuery)
331: PETSC_CUPM_ALIAS_FUNCTION(cupmStreamSynchronize, hipStreamSynchronize)
332: PETSC_CUPM_ALIAS_FUNCTION(cupmDeviceSynchronize, hipDeviceSynchronize)
333: PETSC_CUPM_ALIAS_FUNCTION(cupmGetSymbolAddress, hipGetSymbolAddress)
335: // memory management
336: PETSC_CUPM_ALIAS_FUNCTION(cupmFree, hipFree)
337: PETSC_CUPM_ALIAS_FUNCTION(cupmMalloc, hipMalloc)
338: PETSC_CUPM_ALIAS_FUNCTION(cupmMallocAsync, hipMallocAsync)
339: PETSC_CUPM_ALIAS_FUNCTION(cupmFreeAsync, hipFreeAsync)
340: PETSC_CUPM_ALIAS_FUNCTION(cupmMemcpy, hipMemcpy)
341: PETSC_CUPM_ALIAS_FUNCTION(cupmMemcpyAsync, hipMemcpyAsync)
342: // hipMallocHost is deprecated
343: PETSC_CUPM_ALIAS_FUNCTION(cupmMallocHost, hipHostMalloc)
344: // hipFreeHost is deprecated
345: PETSC_CUPM_ALIAS_FUNCTION(cupmFreeHost, hipHostFree)
346: PETSC_CUPM_ALIAS_FUNCTION(cupmMemset, hipMemset)
347: PETSC_CUPM_ALIAS_FUNCTION(cupmMemsetAsync, hipMemsetAsync)
348: PETSC_CUPM_ALIAS_FUNCTION(cupmMemcpy2D, hipMemcpy2D)
349: PETSC_CUPM_ALIAS_FUNCTION(cupmMemcpy2DAsync, hipMemcpy2DAsync)
350: PETSC_CUPM_ALIAS_FUNCTION(cupmMemset2D, hipMemset2D)
351: PETSC_CUPM_ALIAS_FUNCTION(cupmMemset2DAsync, hipMemset2DAsync)
353: // launch control
354: #if PetscDefined(HAVE_HIPCUDA)
355: PETSC_NODISCARD static hipError_t cupmLaunchHostFunc(cupmStream_t stream, void (*fn)(void *), void *ctx) noexcept { return hipCUDAErrorTohipError(cudaLaunchHostFunc(stream, fn, ctx)); }
356: #else
357: PETSC_CUPM_ALIAS_FUNCTION(cupmLaunchHostFunc, hipLaunchHostFunc)
358: #endif
360: template <typename FunctionT, typename... KernelArgsT>
361: PETSC_NODISCARD static hipError_t cupmLaunchKernel(FunctionT &&func, dim3 gridDim, dim3 blockDim, std::size_t sharedMem, hipStream_t stream, KernelArgsT &&...kernelArgs) noexcept
362: {
363: void *args[] = {(void *)std::addressof(kernelArgs)...};
365: return hipLaunchKernel((void *)func, std::move(gridDim), std::move(blockDim), args, sharedMem, std::move(stream));
366: }
367: };
368: #endif // PetscDefined(HAVE_HIP)
370: // shorthand for bringing all of the typedefs from the base Interface class into your own,
371: // it's annoying that c++ doesn't have a way to do this automatically
372: #define PETSC_CUPM_IMPL_CLASS_HEADER(T) \
373: PETSC_CUPM_BASE_CLASS_HEADER(T); \
374: /* types */ \
375: using cupmError_t = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmError_t; \
376: using cupmEvent_t = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmEvent_t; \
377: using cupmStream_t = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmStream_t; \
378: using cupmDeviceProp_t = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmDeviceProp_t; \
379: using cupmMemcpyKind_t = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemcpyKind_t; \
380: using cupmDeviceAttr_t = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmDeviceAttr_t; \
381: using cupmComplex_t = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmComplex_t; \
382: using cupmPointerAttributes_t = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmPointerAttributes_t; \
383: using cupmMemoryType_t = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemoryType_t; \
384: using cupmDim3 = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmDim3; \
385: using cupmHostFn_t = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmHostFn_t; \
386: using cupmMemPool_t = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemPool_t; \
387: using cupmMemPoolAttr = typename ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemPoolAttr; \
388: /* variables */ \
389: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmSuccess; \
390: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmErrorNotReady; \
391: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmErrorDeviceAlreadyInUse; \
392: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmErrorSetOnActiveProcess; \
393: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmErrorStubLibrary; \
394: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmErrorNoDevice; \
395: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmStreamDefault; \
396: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmStreamNonBlocking; \
397: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmDeviceMapHost; \
398: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemcpyHostToDevice; \
399: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemcpyDeviceToHost; \
400: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemcpyDeviceToDevice; \
401: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemcpyHostToHost; \
402: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemcpyDefault; \
403: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemoryTypeHost; \
404: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemoryTypeDevice; \
405: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemoryTypeManaged; \
406: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmEventDisableTiming; \
407: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmHostAllocDefault; \
408: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmHostAllocWriteCombined; \
409: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemPoolAttrReleaseThreshold; \
410: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmDevAttrClockRate; \
411: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmDevAttrMemoryClockRate; \
412: /* functions */ \
413: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmGetErrorName; \
414: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmGetErrorString; \
415: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmGetLastError; \
416: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmGetDeviceCount; \
417: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmGetDeviceProperties; \
418: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmDeviceGetAttribute; \
419: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmGetDevice; \
420: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmSetDevice; \
421: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmGetDeviceFlags; \
422: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmSetDeviceFlags; \
423: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmPointerGetAttributes; \
424: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmDeviceGetPCIBusId; \
425: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmDeviceGetMemPool; \
426: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemPoolSetAttribute; \
427: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmInit; \
428: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmEventCreate; \
429: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmEventCreateWithFlags; \
430: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmEventDestroy; \
431: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmEventRecord; \
432: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmEventSynchronize; \
433: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmEventElapsedTime; \
434: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmEventQuery; \
435: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmStreamCreate; \
436: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmStreamCreateWithFlags; \
437: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmStreamGetFlags; \
438: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmStreamDestroy; \
439: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmStreamWaitEvent; \
440: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmStreamQuery; \
441: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmStreamSynchronize; \
442: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmDeviceSynchronize; \
443: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmGetSymbolAddress; \
444: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMalloc; \
445: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMallocAsync; \
446: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemcpy; \
447: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemcpyAsync; \
448: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMallocHost; \
449: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemset; \
450: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemsetAsync; \
451: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemcpy2D; \
452: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemcpy2DAsync; \
453: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemset2D; \
454: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmMemset2DAsync; \
455: using ::Petsc::device::cupm::impl::InterfaceImpl<T>::cupmLaunchHostFunc
457: #if PetscHasAttribute(always_inline)
458: // https://gcc.gnu.org/bugzilla//show_bug.cgi?id=109464
459: #define PETSC_GCC_LINKER_UNDEFINED_REFERENCE_BUG_WORKAROUND __attribute__((always_inline))
460: #else
461: #define PETSC_GCC_LINKER_UNDEFINED_REFERENCE_BUG_WORKAROUND
462: #endif
464: // The actual interface class
465: template <DeviceType T>
466: struct Interface : InterfaceImpl<T> {
467: private:
468: using interface_type = InterfaceImpl<T>;
470: public:
471: PETSC_CUPM_IMPL_CLASS_HEADER(T);
473: using cupmReal_t = util::conditional_t<PetscDefined(USE_REAL_SINGLE), float, double>;
474: using cupmScalar_t = util::conditional_t<PetscDefined(USE_COMPLEX), cupmComplex_t, cupmReal_t>;
476: PETSC_NODISCARD PETSC_GCC_LINKER_UNDEFINED_REFERENCE_BUG_WORKAROUND static constexpr cupmScalar_t cupmScalarCast(PetscScalar s) noexcept
477: {
478: #if PetscDefined(USE_COMPLEX)
479: return cupmComplex_t{PetscRealPart(s), PetscImaginaryPart(s)};
480: #else
481: return static_cast<cupmScalar_t>(s);
482: #endif
483: }
485: PETSC_NODISCARD PETSC_GCC_LINKER_UNDEFINED_REFERENCE_BUG_WORKAROUND static constexpr const cupmScalar_t *cupmScalarPtrCast(const PetscScalar *s) noexcept { return reinterpret_cast<const cupmScalar_t *>(s); }
487: PETSC_NODISCARD PETSC_GCC_LINKER_UNDEFINED_REFERENCE_BUG_WORKAROUND static constexpr cupmScalar_t *cupmScalarPtrCast(PetscScalar *s) noexcept { return reinterpret_cast<cupmScalar_t *>(s); }
489: PETSC_NODISCARD PETSC_GCC_LINKER_UNDEFINED_REFERENCE_BUG_WORKAROUND static constexpr const cupmReal_t *cupmRealPtrCast(const PetscReal *s) noexcept { return reinterpret_cast<const cupmReal_t *>(s); }
491: PETSC_NODISCARD PETSC_GCC_LINKER_UNDEFINED_REFERENCE_BUG_WORKAROUND static constexpr cupmReal_t *cupmRealPtrCast(PetscReal *s) noexcept { return reinterpret_cast<cupmReal_t *>(s); }
493: static PetscErrorCode PetscCUPMGetMemType(const void *data, PetscMemType *type, PetscBool *registered = nullptr, PetscBool *managed = nullptr) noexcept
494: {
495: cupmPointerAttributes_t attr;
496: cupmError_t cerr;
498: PetscFunctionBegin;
499: if (type) PetscAssertPointer(type, 2);
500: if (registered) {
501: PetscAssertPointer(registered, 3);
502: *registered = PETSC_FALSE;
503: }
504: if (managed) {
505: PetscAssertPointer(managed, 4);
506: *managed = PETSC_FALSE;
507: }
508: // HIP versions before 7.0 may reject unregistered host pointers; clear that error.
509: cerr = cupmPointerGetAttributes(&attr, data);
510: cerr = cupmGetLastError();
511: if (T == DeviceType::CUDA) PetscCallCUPM(cerr);
512: if (type) *type = PETSC_MEMTYPE_HOST;
513: if (cerr == cupmSuccess) {
514: const auto mtype = interface_type::cupmPointerGetMemType(attr, managed);
516: // Managed memory is device accessible, so treat it as device memory.
517: if (type && (mtype == cupmMemoryTypeDevice || mtype == cupmMemoryTypeManaged)) *type = PETSC_MEMTYPE_CUPM();
518: if (registered && (mtype == cupmMemoryTypeHost)) *registered = PETSC_TRUE;
519: }
520: PetscFunctionReturn(PETSC_SUCCESS);
521: }
522: PETSC_NODISCARD static PETSC_CONSTEXPR_14 cupmMemcpyKind_t PetscDeviceCopyModeToCUPMMemcpyKind(PetscDeviceCopyMode mode) noexcept
523: {
524: switch (mode) {
525: case PETSC_DEVICE_COPY_HTOH:
526: return cupmMemcpyHostToHost;
527: case PETSC_DEVICE_COPY_HTOD:
528: return cupmMemcpyHostToDevice;
529: case PETSC_DEVICE_COPY_DTOD:
530: return cupmMemcpyDeviceToDevice;
531: case PETSC_DEVICE_COPY_DTOH:
532: return cupmMemcpyDeviceToHost;
533: case PETSC_DEVICE_COPY_AUTO:
534: return cupmMemcpyDefault;
535: }
536: PetscUnreachable();
537: return cupmMemcpyDefault;
538: }
540: // these change what the arguments mean, so need to namespace these
541: template <typename M>
542: static PetscErrorCode PetscCUPMMallocAsync(M **ptr, std::size_t n, cupmStream_t stream = nullptr) noexcept
543: {
544: static_assert(!std::is_void<M>::value, "");
546: PetscFunctionBegin;
547: PetscAssertPointer(ptr, 1);
548: *ptr = nullptr;
549: if (n) {
550: const auto bytes = n * sizeof(M);
551: // https://developer.nvidia.com/blog/using-cuda-stream-ordered-memory-allocator-part-2/
552: //
553: // TLD;DR: cudaMallocAsync() does not work with NVIDIA GPUDirect which OPENMPI uses to
554: // underpin its cuda-aware MPI implementation, so we cannot just async allocate
555: // blindly...
556: if (stream) {
557: PetscCallCUPM(cupmMallocAsync(reinterpret_cast<void **>(ptr), bytes, stream));
558: } else {
559: PetscCallCUPM(cupmMalloc(reinterpret_cast<void **>(ptr), bytes));
560: }
561: }
562: PetscFunctionReturn(PETSC_SUCCESS);
563: }
565: template <typename M>
566: static PetscErrorCode PetscCUPMMalloc(M **ptr, std::size_t n) noexcept
567: {
568: PetscFunctionBegin;
569: PetscCall(PetscCUPMMallocAsync(ptr, n));
570: PetscFunctionReturn(PETSC_SUCCESS);
571: }
573: template <typename M>
574: static PetscErrorCode PetscCUPMMallocHost(M **ptr, std::size_t n, unsigned int flags = cupmHostAllocDefault) noexcept
575: {
576: static_assert(!std::is_void<M>::value, "");
578: PetscFunctionBegin;
579: PetscAssertPointer(ptr, 1);
580: *ptr = nullptr;
581: if (n) PetscCallCUPM(cupmMallocHost(reinterpret_cast<void **>(ptr), n * sizeof(M), flags));
582: PetscFunctionReturn(PETSC_SUCCESS);
583: }
585: template <typename D>
586: static PetscErrorCode PetscCUPMMemcpyAsync(D *dest, const util::type_identity_t<D> *src, std::size_t n, cupmMemcpyKind_t kind, cupmStream_t stream = nullptr, bool use_async = false) noexcept
587: {
588: static_assert(!std::is_void<D>::value, "");
589: const auto size = n * sizeof(D);
591: PetscFunctionBegin;
592: if (PetscUnlikely(!n)) PetscFunctionReturn(PETSC_SUCCESS);
593: // cannot dereference (i.e. cannot call PetscAssertPointer() here)
594: PetscCheck(dest, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Trying to copy to a NULL pointer");
595: PetscCheck(src, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Trying to copy from a NULL pointer");
596: if (kind == cupmMemcpyHostToHost) {
597: // If we are HTOH it is cheaper to check if the stream is idle and do a basic mempcy()
598: // than it is to just call the vendor functions. This assumes of course that the stream
599: // accounts for both memory regions being "idle"
600: if (cupmStreamQuery(stream) == cupmSuccess) {
601: PetscCall(PetscMemcpy(dest, src, size));
602: PetscFunctionReturn(PETSC_SUCCESS);
603: }
604: // need to clear the potential cupmErrorNotReady generated by query above...
605: auto cerr = cupmGetLastError();
607: if (PetscUnlikely(cerr != cupmErrorNotReady)) PetscCallCUPM(cerr);
608: }
609: if (use_async || stream || (kind != cupmMemcpyDeviceToHost)) {
610: PetscCallCUPM(cupmMemcpyAsync(dest, src, size, kind, stream));
611: } else {
612: PetscCallCUPM(cupmMemcpy(dest, src, size, kind));
613: }
614: PetscCall(PetscLogCUPMMemcpyTransfer(kind, size));
615: PetscFunctionReturn(PETSC_SUCCESS);
616: }
618: template <typename D>
619: static PetscErrorCode PetscCUPMMemcpy(D *dest, const util::type_identity_t<D> *src, std::size_t n, cupmMemcpyKind_t kind) noexcept
620: {
621: PetscFunctionBegin;
622: PetscCall(PetscCUPMMemcpyAsync(dest, src, n, kind));
623: PetscFunctionReturn(PETSC_SUCCESS);
624: }
626: template <typename D>
627: static PetscErrorCode PetscCUPMMemcpy2DAsync(D *dest, std::size_t dest_pitch, const util::type_identity_t<D> *src, std::size_t src_pitch, std::size_t width, std::size_t height, cupmMemcpyKind_t kind, cupmStream_t stream = nullptr)
628: {
629: static_assert(!std::is_void<D>::value, "");
630: const auto dest_pitch_bytes = dest_pitch * sizeof(D);
631: const auto src_pitch_bytes = src_pitch * sizeof(D);
632: const auto width_bytes = width * sizeof(D);
633: const auto size = height * width_bytes;
635: PetscFunctionBegin;
636: if (PetscUnlikely(!size)) PetscFunctionReturn(PETSC_SUCCESS);
637: PetscCheck(dest, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Trying to copy to a NULL pointer");
638: PetscCheck(src, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Trying to copy from a NULL pointer");
639: if (stream || (kind != cupmMemcpyDeviceToHost)) {
640: PetscCallCUPM(cupmMemcpy2DAsync(dest, dest_pitch_bytes, src, src_pitch_bytes, width_bytes, height, kind, stream));
641: } else {
642: PetscCallCUPM(cupmMemcpy2D(dest, dest_pitch_bytes, src, src_pitch_bytes, width_bytes, height, kind));
643: }
644: PetscCall(PetscLogCUPMMemcpyTransfer(kind, size));
645: PetscFunctionReturn(PETSC_SUCCESS);
646: }
648: template <typename D>
649: static PetscErrorCode PetscCUPMMemcpy2D(D *dest, std::size_t dest_pitch, const util::type_identity_t<D> *src, std::size_t src_pitch, std::size_t width, std::size_t height, cupmMemcpyKind_t kind)
650: {
651: PetscFunctionBegin;
652: PetscCall(PetscCUPMMemcpy2DAsync(dest, dest_pitch, src, src_pitch, width, height, kind));
653: PetscFunctionReturn(PETSC_SUCCESS);
654: }
656: template <typename M>
657: static PetscErrorCode PetscCUPMMemsetAsync(M *ptr, int value, std::size_t n, cupmStream_t stream = nullptr, bool use_async = false) noexcept
658: {
659: static_assert(!std::is_void<M>::value, "");
661: PetscFunctionBegin;
662: if (PetscLikely(n)) {
663: const auto bytes = n * sizeof(M);
665: PetscCheck(ptr, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Trying to memset a NULL pointer with size %zu != 0", n);
666: if (stream || use_async) {
667: PetscCallCUPM(cupmMemsetAsync(ptr, value, bytes, stream));
668: } else {
669: PetscCallCUPM(cupmMemset(ptr, value, bytes));
670: }
671: }
672: PetscFunctionReturn(PETSC_SUCCESS);
673: }
675: template <typename M>
676: static PetscErrorCode PetscCUPMMemset(M *ptr, int value, std::size_t n) noexcept
677: {
678: PetscFunctionBegin;
679: PetscCall(PetscCUPMMemsetAsync(ptr, value, n));
680: PetscFunctionReturn(PETSC_SUCCESS);
681: }
683: template <typename D>
684: static PetscErrorCode PetscCUPMMemset2DAsync(D *ptr, std::size_t pitch, int value, std::size_t width, std::size_t height, cupmStream_t stream = nullptr)
685: {
686: static_assert(!std::is_void<D>::value, "");
687: const auto pitch_bytes = pitch * sizeof(D);
688: const auto width_bytes = width * sizeof(D);
689: const auto size = width_bytes * height;
691: PetscFunctionBegin;
692: if (PetscUnlikely(!size)) PetscFunctionReturn(PETSC_SUCCESS);
693: PetscAssert(ptr, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Trying to memset a NULL pointer with size %zu != 0", size);
694: if (stream) {
695: PetscCallCUPM(cupmMemset2DAsync(ptr, pitch_bytes, value, width_bytes, height, stream));
696: } else {
697: PetscCallCUPM(cupmMemset2D(ptr, pitch_bytes, value, width_bytes, height));
698: }
699: PetscFunctionReturn(PETSC_SUCCESS);
700: }
702: // these we can transparently wrap, no need to namespace it to Petsc
703: template <typename M>
704: PETSC_NODISCARD static cupmError_t cupmFreeAsync(M &ptr, cupmStream_t stream = nullptr) noexcept
705: {
706: static_assert(std::is_pointer<util::decay_t<M>>::value, "");
707: static_assert(!std::is_const<M>::value, "");
709: if (ptr) {
710: auto cerr = interface_type::cupmFreeAsync(std::forward<M>(ptr), stream);
712: ptr = nullptr;
713: if (PetscUnlikely(cerr != cupmSuccess)) return cerr;
714: }
715: return cupmSuccess;
716: }
718: PETSC_NODISCARD static cupmError_t cupmFreeAsync(std::nullptr_t ptr, cupmStream_t stream = nullptr) { return interface_type::cupmFreeAsync(ptr, stream); }
720: template <typename M>
721: PETSC_NODISCARD static cupmError_t cupmFree(M &ptr) noexcept
722: {
723: return cupmFreeAsync(ptr);
724: }
726: PETSC_NODISCARD static cupmError_t cupmFree(std::nullptr_t ptr) { return cupmFreeAsync(ptr); }
728: template <typename M>
729: PETSC_NODISCARD static cupmError_t cupmFreeHost(M &ptr) noexcept
730: {
731: static_assert(std::is_pointer<util::decay_t<M>>::value, "");
732: const auto cerr = interface_type::cupmFreeHost(std::forward<M>(ptr));
733: ptr = nullptr;
734: return cerr;
735: }
737: PETSC_NODISCARD static cupmError_t cupmFreeHost(std::nullptr_t ptr) { return interface_type::cupmFreeHost(ptr); }
739: // specific wrapper for device launch function, as the real function is a C routine and
740: // doesn't have variable arguments. The actual mechanics of this are a bit complicated but
741: // boils down to the fact that ultimately we pass a
742: //
743: // void *args[] = {(void*)&kernel_args...};
744: //
745: // to the kernel launcher. Since we pass void* this means implicit conversion does **not**
746: // happen to the kernel arguments so we must do it ourselves here. This function does this in
747: // 3 stages:
748: // 1. Enumerate the kernel arguments (cupmLaunchKernel)
749: // 2. Deduce the signature of func() and static_cast the kernel arguments to the type
750: // expected by func() using the enumeration above (deduceKernelCall)
751: // 3. Form the void* array with the converted arguments and call cuda/hipLaunchKernel with
752: // it. (interface_type::cupmLaunchKernel)
753: template <typename F, typename... Args>
754: PETSC_NODISCARD static cupmError_t cupmLaunchKernel(F &&func, cupmDim3 gridDim, cupmDim3 blockDim, std::size_t sharedMem, cupmStream_t stream, Args &&...kernelArgs) noexcept
755: {
756: return deduceKernelCall(util::index_sequence_for<Args...>{}, std::forward<F>(func), std::move(gridDim), std::move(blockDim), std::move(sharedMem), std::move(stream), std::forward<Args>(kernelArgs)...);
757: }
759: template <std::size_t block_size = 256, std::size_t warp_size = 32, typename F, typename... Args>
760: static PetscErrorCode PetscCUPMLaunchKernel1D(std::size_t n, std::size_t sharedMem, cupmStream_t stream, F &&func, Args &&...kernelArgs) noexcept
761: {
762: static_assert(block_size > 0, "");
763: static_assert(warp_size > 0, "");
764: // want block_size to be a multiple of the warp_size
765: static_assert(block_size % warp_size == 0, "");
766: const auto nthread = std::min(n, block_size);
767: const auto nblock = (n + block_size - 1) / block_size;
769: PetscFunctionBegin;
770: // if n = 0 then nthread = 0, which is not allowed. rather than letting the user try to
771: // decipher cryptic 'cuda/hipErrorLaunchFailure' we explicitly check for zero here
772: PetscAssert(nthread, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Trying to launch kernel with grid/block size 0");
773: PetscCallCUPM(cupmLaunchKernel(std::forward<F>(func), (unsigned int)nblock, (unsigned int)nthread, sharedMem, stream, std::forward<Args>(kernelArgs)...));
774: PetscFunctionReturn(PETSC_SUCCESS);
775: }
777: private:
778: template <typename S, typename D, typename = void>
779: struct is_static_castable : std::false_type { };
781: template <typename S, typename D>
782: struct is_static_castable<S, D, util::void_t<decltype(static_cast<D>(std::declval<S>()))>> : std::true_type { };
784: template <typename D, typename S>
785: static constexpr util::enable_if_t<is_static_castable<S, D>::value, D> cast_to(S &&src) noexcept
786: {
787: return static_cast<D>(std::forward<S>(src));
788: }
790: template <typename D, typename S>
791: static constexpr util::enable_if_t<!is_static_castable<S, D>::value, D> cast_to(S &&src) noexcept
792: {
793: return const_cast<D>(std::forward<S>(src));
794: }
796: template <typename F, typename... Args, std::size_t... Idx>
797: PETSC_NODISCARD static cupmError_t deduceKernelCall(util::index_sequence<Idx...>, F &&func, cupmDim3 gridDim, cupmDim3 blockDim, std::size_t sharedMem, cupmStream_t stream, Args &&...kernelArgs) noexcept
798: {
799: // clang-format off
800: return interface_type::cupmLaunchKernel(
801: std::forward<F>(func),
802: std::move(gridDim), std::move(blockDim), std::move(sharedMem), std::move(stream),
803: // can't static_cast() here since the function argument type may be cv-qualified, in
804: // which case we would need to const_cast(). But you can only const_cast() indirect types
805: // (pointers, references). So we need a SFINAE monster that is a static_cast() if
806: // possible, and a const_cast() if not. We could just use a C-style cast which *would*
807: // work here since it tries the following and uses the first one that succeeds:
808: //
809: // 1. const_cast()
810: // 2. static_cast()
811: // 3. static_cast() then const_cast()
812: // 4. reinterpret_cast()...
813: //
814: // the issue however is the final reinterpret_cast(). We absolutely cannot get there
815: // because doing so would silently hide a ton of bugs, for example casting a PetscScalar
816: // * to double * in complex builds, a PetscInt * to int * in 64idx builds, etc.
817: cast_to<typename util::func_traits<F>::template arg<Idx>::type>(std::forward<Args>(kernelArgs))...
818: );
819: // clang-format on
820: }
822: static PetscErrorCode PetscLogCUPMMemcpyTransfer(cupmMemcpyKind_t kind, std::size_t size) noexcept
823: {
824: PetscFunctionBegin;
825: // only the explicit HTOD or DTOH are handled, since we either don't log the other cases
826: // (yet) or don't know the direction
827: if (kind == cupmMemcpyDeviceToHost) PetscCall(PetscLogGpuToCpu(static_cast<PetscLogDouble>(size)));
828: else if (kind == cupmMemcpyHostToDevice) PetscCall(PetscLogCpuToGpu(static_cast<PetscLogDouble>(size)));
829: else (void)size;
830: PetscFunctionReturn(PETSC_SUCCESS);
831: }
832: };
834: #undef PETSC_GCC_LINKER_UNDEFINED_REFERENCE_BUG_WORKAROUND
836: #define PETSC_CUPM_INHERIT_INTERFACE_TYPEDEFS_USING(T) \
837: PETSC_CUPM_IMPL_CLASS_HEADER(T); \
838: using cupmReal_t = typename ::Petsc::device::cupm::impl::Interface<T>::cupmReal_t; \
839: using cupmScalar_t = typename ::Petsc::device::cupm::impl::Interface<T>::cupmScalar_t; \
840: using ::Petsc::device::cupm::impl::Interface<T>::cupmScalarCast; \
841: using ::Petsc::device::cupm::impl::Interface<T>::cupmScalarPtrCast; \
842: using ::Petsc::device::cupm::impl::Interface<T>::cupmRealPtrCast; \
843: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMGetMemType; \
844: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMMemset; \
845: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMMemsetAsync; \
846: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMMalloc; \
847: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMMallocAsync; \
848: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMMallocHost; \
849: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMMemcpy; \
850: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMMemcpyAsync; \
851: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMMemcpy2D; \
852: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMMemcpy2DAsync; \
853: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMMemset2DAsync; \
854: using ::Petsc::device::cupm::impl::Interface<T>::cupmFree; \
855: using ::Petsc::device::cupm::impl::Interface<T>::cupmFreeAsync; \
856: using ::Petsc::device::cupm::impl::Interface<T>::cupmFreeHost; \
857: using ::Petsc::device::cupm::impl::Interface<T>::cupmLaunchKernel; \
858: using ::Petsc::device::cupm::impl::Interface<T>::PetscCUPMLaunchKernel1D; \
859: using ::Petsc::device::cupm::impl::Interface<T>::PetscDeviceCopyModeToCUPMMemcpyKind
861: #if PetscDefined(HAVE_CUDA)
862: extern template struct PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL Interface<DeviceType::CUDA>;
863: #endif
865: #if PetscDefined(HAVE_HIP)
866: extern template struct PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL Interface<DeviceType::HIP>;
867: #endif
869: } // namespace impl
871: } // namespace cupm
873: } // namespace device
875: } // namespace Petsc