Actual source code: cupmobject.hpp

  1: #pragma once

  3: #include <petsc/private/deviceimpl.h>
  4: #include <petsc/private/cupmsolverinterface.hpp>

  6: #include <cstring> // std::memset

  8: inline PetscErrorCode PetscStrFreeAllocpy(const char target[], char **dest) noexcept
  9: {
 10:   PetscFunctionBegin;
 11:   PetscAssertPointer(dest, 2);
 12:   if (*dest) {
 13:     PetscAssertPointer(*dest, 2);
 14:     PetscCall(PetscFree(*dest));
 15:   }
 16:   PetscCall(PetscStrallocpy(target, dest));
 17:   PetscFunctionReturn(PETSC_SUCCESS);
 18: }

 20: namespace Petsc
 21: {

 23: namespace device
 24: {

 26: namespace cupm
 27: {

 29: namespace impl
 30: {

 32: namespace
 33: {

 35: // ==========================================================================================
 36: // UseCUPMHostAllocGuard
 37: //
 38: // A simple RAII helper for PetscMallocSet[CUDA|HIP]Host(). it exists because integrating the
 39: // regular versions would be an enormous pain to square with the templated types...
 40: // ==========================================================================================
 41: template <DeviceType T>
 42: class PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL UseCUPMHostAllocGuard : Interface<T> {
 43: public:
 44:   PETSC_CUPM_INHERIT_INTERFACE_TYPEDEFS_USING(T);

 46:   UseCUPMHostAllocGuard(bool) noexcept;
 47:   ~UseCUPMHostAllocGuard() noexcept;

 49:   PETSC_NODISCARD bool value() const noexcept;

 51: private:
 52:   // would have loved to just do
 53:   //
 54:   // const auto oldmalloc = PetscTrMalloc;
 55:   //
 56:   // but in order to use auto the member needs to be static; in order to be static it must
 57:   // also be constexpr -- which in turn requires an initializer (also implicitly required by
 58:   // auto). But constexpr needs a constant expression initializer, so we can't initialize it
 59:   // with global (mutable) variables...
 60: #define DECLTYPE_AUTO(left, right) decltype(right) left = right
 61:   const DECLTYPE_AUTO(oldmalloc_, PetscTrMalloc);
 62:   const DECLTYPE_AUTO(oldfree_, PetscTrFree);
 63:   const DECLTYPE_AUTO(oldrealloc_, PetscTrRealloc);
 64: #undef DECLTYPE_AUTO
 65:   bool v_;
 66: };

 68: // ==========================================================================================
 69: // UseCUPMHostAllocGuard -- Public API
 70: // ==========================================================================================

 72: template <DeviceType T>
 73: inline UseCUPMHostAllocGuard<T>::UseCUPMHostAllocGuard(bool useit) noexcept : v_(useit)
 74: {
 75:   PetscFunctionBegin;
 76:   if (useit) {
 77:     // all unused arguments are un-named, this saves having to add PETSC_UNUSED to them all
 78:     PetscTrMalloc = [](std::size_t sz, PetscBool clear, int, const char *, const char *, void **ptr) {
 79:       PetscFunctionBegin;
 80:       PetscCallCUPM(cupmMallocHost(ptr, sz));
 81:       if (clear) std::memset(*ptr, 0, sz);
 82:       PetscFunctionReturn(PETSC_SUCCESS);
 83:     };
 84:     PetscTrFree = [](void *ptr, int, const char *, const char *) {
 85:       PetscFunctionBegin;
 86:       PetscCallCUPM(cupmFreeHost(ptr));
 87:       PetscFunctionReturn(PETSC_SUCCESS);
 88:     };
 89:     PetscTrRealloc = [](std::size_t, int, const char *, const char *, void **) {
 90:       // REVIEW ME: can be implemented by malloc->copy->free?
 91:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "%s has no realloc()", cupmName());
 92:     };
 93:   }
 94:   PetscFunctionReturnVoid();
 95: }

 97: template <DeviceType T>
 98: inline UseCUPMHostAllocGuard<T>::~UseCUPMHostAllocGuard() noexcept
 99: {
100:   PetscFunctionBegin;
101:   if (value()) {
102:     PetscTrMalloc  = oldmalloc_;
103:     PetscTrFree    = oldfree_;
104:     PetscTrRealloc = oldrealloc_;
105:   }
106:   PetscFunctionReturnVoid();
107: }

109: template <DeviceType T>
110: inline bool UseCUPMHostAllocGuard<T>::value() const noexcept
111: {
112:   return v_;
113: }

115: } // anonymous namespace

117: template <DeviceType T, PetscMemType MemoryType, PetscMemoryAccessMode AccessMode>
118: class PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL RestoreableArray : Interface<T> {
119: public:
120:   PETSC_CUPM_INHERIT_INTERFACE_TYPEDEFS_USING(T);

122:   static constexpr auto memory_type = MemoryType;
123:   static constexpr auto access_type = AccessMode;

125:   using value_type        = PetscScalar;
126:   using pointer_type      = value_type *;
127:   using cupm_pointer_type = cupmScalar_t *;

129:   PETSC_NODISCARD pointer_type      data() const noexcept;
130:   PETSC_NODISCARD cupm_pointer_type cupmdata() const noexcept;

132:   operator pointer_type() const noexcept;
133:   // in case pointer_type == cupmscalar_pointer_type we don't want this overload to exist, so
134:   // we make a dummy template parameter to allow SFINAE to nix it for us
135:   template <typename U = pointer_type, typename = util::enable_if_t<!std::is_same<U, cupm_pointer_type>::value>>
136:   operator cupm_pointer_type() const noexcept;

138: protected:
139:   constexpr explicit RestoreableArray(PetscDeviceContext) noexcept;

141:   value_type        *ptr_  = nullptr;
142:   PetscDeviceContext dctx_ = nullptr;
143: };

145: // ==========================================================================================
146: // RestoreableArray - Static Variables
147: // ==========================================================================================

149: template <DeviceType T, PetscMemType MT, PetscMemoryAccessMode MA>
150: const PetscMemType RestoreableArray<T, MT, MA>::memory_type;

152: template <DeviceType T, PetscMemType MT, PetscMemoryAccessMode MA>
153: const PetscMemoryAccessMode RestoreableArray<T, MT, MA>::access_type;

155: // ==========================================================================================
156: // RestoreableArray - Public API
157: // ==========================================================================================

159: template <DeviceType T, PetscMemType MT, PetscMemoryAccessMode MA>
160: constexpr inline RestoreableArray<T, MT, MA>::RestoreableArray(PetscDeviceContext dctx) noexcept : dctx_{dctx}
161: {
162: }

164: template <DeviceType T, PetscMemType MT, PetscMemoryAccessMode MA>
165: inline typename RestoreableArray<T, MT, MA>::pointer_type RestoreableArray<T, MT, MA>::data() const noexcept
166: {
167:   return ptr_;
168: }

170: template <DeviceType T, PetscMemType MT, PetscMemoryAccessMode MA>
171: inline typename RestoreableArray<T, MT, MA>::cupm_pointer_type RestoreableArray<T, MT, MA>::cupmdata() const noexcept
172: {
173:   return cupmScalarPtrCast(data());
174: }

176: template <DeviceType T, PetscMemType MT, PetscMemoryAccessMode MA>
177: inline RestoreableArray<T, MT, MA>::operator pointer_type() const noexcept
178: {
179:   return data();
180: }

182: // in case pointer_type == cupmscalar_pointer_type we don't want this overload to exist, so
183: // we make a dummy template parameter to allow SFINAE to nix it for us
184: template <DeviceType T, PetscMemType MT, PetscMemoryAccessMode MA>
185: template <typename U, typename>
186: inline RestoreableArray<T, MT, MA>::operator cupm_pointer_type() const noexcept
187: {
188:   return cupmdata();
189: }

191: template <DeviceType T>
192: class PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL CUPMObject : SolverInterface<T> {
193: protected:
194:   PETSC_CUPMSOLVER_INHERIT_INTERFACE_TYPEDEFS_USING(T);

196: private:
197:   // The final stop in the GetHandles_/GetFromHandles_ chain. This retrieves the various
198:   // compute handles and ensure the given PetscDeviceContext is of the right type
199:   static PetscErrorCode GetFromHandleDispatch_(PetscDeviceContext, cupmBlasHandle_t *, cupmSparseHandle_t *, cupmSolverHandle_t *, cupmStream_t *) noexcept;
200:   static PetscErrorCode GetHandleDispatch_(PetscDeviceContext *, cupmBlasHandle_t *, cupmSparseHandle_t *, cupmSolverHandle_t *, cupmStream_t *) noexcept;

202: protected:
203:   PETSC_NODISCARD static constexpr PetscRandomType PETSCDEVICERAND() noexcept;

205:   // Helper routines to retrieve various combinations of handles. The first set (GetHandles_)
206:   // gets a PetscDeviceContext along with it, while the second set (GetHandlesFrom_) assumes
207:   // you've gotten the PetscDeviceContext already, and retrieves the handles from it. All of
208:   // them check that the PetscDeviceContext is of the appropriate type
209:   static PetscErrorCode GetHandles_(PetscDeviceContext *, cupmBlasHandle_t * = nullptr, cupmSolverHandle_t * = nullptr, cupmStream_t * = nullptr) noexcept;

211:   // triple
212:   static PetscErrorCode GetHandles_(PetscDeviceContext *, cupmBlasHandle_t *, cupmStream_t *) noexcept;
213:   static PetscErrorCode GetHandles_(PetscDeviceContext *, cupmSolverHandle_t *, cupmStream_t *) noexcept;

215:   // double
216:   static PetscErrorCode GetHandles_(PetscDeviceContext *, cupmSolverHandle_t *) noexcept;
217:   static PetscErrorCode GetHandles_(PetscDeviceContext *, cupmStream_t *) noexcept;

219:   // single
220:   static PetscErrorCode GetHandles_(cupmBlasHandle_t *) noexcept;
221:   static PetscErrorCode GetHandles_(cupmSolverHandle_t *) noexcept;
222:   static PetscErrorCode GetHandles_(cupmStream_t *) noexcept;

224:   static PetscErrorCode GetHandlesFrom_(PetscDeviceContext, cupmBlasHandle_t *, cupmSolverHandle_t * = nullptr, cupmStream_t * = nullptr) noexcept;
225:   static PetscErrorCode GetHandlesFrom_(PetscDeviceContext, cupmSolverHandle_t *, cupmStream_t * = nullptr) noexcept;
226:   static PetscErrorCode GetHandlesFrom_(PetscDeviceContext, cupmStream_t *) noexcept;

228:   // HIP sparse and solver handles can both be void *, so their getters need distinct names.
229:   static PetscErrorCode GetSparseHandles_(PetscDeviceContext *, cupmSparseHandle_t *, cupmStream_t *) noexcept;
230:   static PetscErrorCode GetSparseHandles_(PetscDeviceContext *, cupmSparseHandle_t *) noexcept;
231:   static PetscErrorCode GetSparseHandles_(cupmSparseHandle_t *) noexcept;
232:   static PetscErrorCode GetSparseHandlesFrom_(PetscDeviceContext, cupmSparseHandle_t *, cupmStream_t * = nullptr) noexcept;

234:   // disallow implicit conversion
235:   template <typename U>
236:   PETSC_NODISCARD static UseCUPMHostAllocGuard<T> UseCUPMHostAlloc(U) noexcept = delete;
237:   // utility for using cupmHostAlloc()
238:   PETSC_NODISCARD static UseCUPMHostAllocGuard<T> UseCUPMHostAlloc(bool) noexcept;

240:   // A debug check to ensure that a given pointer-memtype pairing taken from user-land is
241:   // actually correct. Errors on mismatch
242:   static PetscErrorCode CheckPointerMatchesMemType_(const void *, PetscMemType) noexcept;
243: };

245: template <DeviceType T>
246: inline constexpr PetscRandomType CUPMObject<T>::PETSCDEVICERAND() noexcept
247: {
248:   // REVIEW ME: HIP default rng?
249:   return T == DeviceType::CUDA ? PETSCCURAND : PETSCRANDER48;
250: }

252: template <DeviceType T>
253: inline PetscErrorCode CUPMObject<T>::GetFromHandleDispatch_(PetscDeviceContext dctx, cupmBlasHandle_t *blas_handle, cupmSparseHandle_t *sparse_handle, cupmSolverHandle_t *solver_handle, cupmStream_t *stream_handle) noexcept
254: {
255:   PetscFunctionBegin;
257:   if (blas_handle) {
258:     PetscAssertPointer(blas_handle, 2);
259:     *blas_handle = nullptr;
260:   }
261:   if (sparse_handle) {
262:     PetscAssertPointer(sparse_handle, 3);
263:     *sparse_handle = nullptr;
264:   }
265:   if (solver_handle) {
266:     PetscAssertPointer(solver_handle, 4);
267:     *solver_handle = nullptr;
268:   }
269:   if (stream_handle) {
270:     PetscAssertPointer(stream_handle, 5);
271:     *stream_handle = nullptr;
272:   }
273:   if (PetscDefined(USE_DEBUG)) {
274:     PetscDeviceType dtype;

276:     PetscCall(PetscDeviceContextGetDeviceType(dctx, &dtype));
277:     PetscCheckCompatibleDeviceTypes(PETSC_DEVICE_CUPM(), -1, dtype, 1);
278:   }
279:   if (blas_handle) PetscCall(PetscDeviceContextGetBLASHandle_Internal(dctx, blas_handle));
280:   if (sparse_handle) PetscCall(PetscDeviceContextGetSPARSEHandle_Internal(dctx, sparse_handle));
281:   if (solver_handle) PetscCall(PetscDeviceContextGetSOLVERHandle_Internal(dctx, solver_handle));
282:   if (stream_handle) {
283:     cupmStream_t *stream = nullptr;

285:     PetscCall(PetscDeviceContextGetStreamHandle_Internal(dctx, (void **)&stream));
286:     *stream_handle = *stream;
287:   }
288:   PetscFunctionReturn(PETSC_SUCCESS);
289: }

291: template <DeviceType T>
292: inline PetscErrorCode CUPMObject<T>::GetHandleDispatch_(PetscDeviceContext *dctx, cupmBlasHandle_t *blas_handle, cupmSparseHandle_t *sparse_handle, cupmSolverHandle_t *solver_handle, cupmStream_t *stream) noexcept
293: {
294:   PetscDeviceContext dctx_loc = nullptr;

296:   PetscFunctionBegin;
297:   // silence uninitialized variable warnings
298:   if (dctx) *dctx = nullptr;
299:   PetscCall(PetscDeviceContextGetCurrentContext(&dctx_loc));
300:   PetscCall(GetFromHandleDispatch_(dctx_loc, blas_handle, sparse_handle, solver_handle, stream));
301:   if (dctx) *dctx = dctx_loc;
302:   PetscFunctionReturn(PETSC_SUCCESS);
303: }

305: template <DeviceType T>
306: inline PetscErrorCode CUPMObject<T>::GetHandles_(PetscDeviceContext *dctx, cupmBlasHandle_t *blas_handle, cupmSolverHandle_t *solver_handle, cupmStream_t *stream) noexcept
307: {
308:   return GetHandleDispatch_(dctx, blas_handle, nullptr, solver_handle, stream);
309: }

311: template <DeviceType T>
312: inline PetscErrorCode CUPMObject<T>::GetHandles_(PetscDeviceContext *dctx, cupmBlasHandle_t *blas_handle, cupmStream_t *stream) noexcept
313: {
314:   return GetHandleDispatch_(dctx, blas_handle, nullptr, nullptr, stream);
315: }

317: template <DeviceType T>
318: inline PetscErrorCode CUPMObject<T>::GetHandles_(PetscDeviceContext *dctx, cupmSolverHandle_t *solver_handle, cupmStream_t *stream) noexcept
319: {
320:   return GetHandleDispatch_(dctx, nullptr, nullptr, solver_handle, stream);
321: }

323: template <DeviceType T>
324: inline PetscErrorCode CUPMObject<T>::GetHandles_(PetscDeviceContext *dctx, cupmStream_t *stream) noexcept
325: {
326:   return GetHandleDispatch_(dctx, nullptr, nullptr, nullptr, stream);
327: }

329: template <DeviceType T>
330: inline PetscErrorCode CUPMObject<T>::GetHandles_(cupmBlasHandle_t *handle) noexcept
331: {
332:   return GetHandleDispatch_(nullptr, handle, nullptr, nullptr, nullptr);
333: }

335: template <DeviceType T>
336: inline PetscErrorCode CUPMObject<T>::GetHandles_(cupmSolverHandle_t *handle) noexcept
337: {
338:   return GetHandleDispatch_(nullptr, nullptr, nullptr, handle, nullptr);
339: }

341: template <DeviceType T>
342: inline PetscErrorCode CUPMObject<T>::GetHandles_(cupmStream_t *stream) noexcept
343: {
344:   return GetHandleDispatch_(nullptr, nullptr, nullptr, nullptr, stream);
345: }

347: template <DeviceType T>
348: inline PetscErrorCode CUPMObject<T>::GetHandlesFrom_(PetscDeviceContext dctx, cupmBlasHandle_t *blas_handle, cupmSolverHandle_t *solver_handle, cupmStream_t *stream) noexcept
349: {
350:   return GetFromHandleDispatch_(dctx, blas_handle, nullptr, solver_handle, stream);
351: }

353: template <DeviceType T>
354: inline PetscErrorCode CUPMObject<T>::GetHandlesFrom_(PetscDeviceContext dctx, cupmSolverHandle_t *solver_handle, cupmStream_t *stream) noexcept
355: {
356:   return GetFromHandleDispatch_(dctx, nullptr, nullptr, solver_handle, stream);
357: }

359: template <DeviceType T>
360: inline PetscErrorCode CUPMObject<T>::GetHandlesFrom_(PetscDeviceContext dctx, cupmStream_t *stream) noexcept
361: {
362:   return GetFromHandleDispatch_(dctx, nullptr, nullptr, nullptr, stream);
363: }

365: template <DeviceType T>
366: inline PetscErrorCode CUPMObject<T>::GetSparseHandles_(PetscDeviceContext *dctx, cupmSparseHandle_t *handle, cupmStream_t *stream) noexcept
367: {
368:   return GetHandleDispatch_(dctx, nullptr, handle, nullptr, stream);
369: }

371: template <DeviceType T>
372: inline PetscErrorCode CUPMObject<T>::GetSparseHandles_(PetscDeviceContext *dctx, cupmSparseHandle_t *handle) noexcept
373: {
374:   return GetHandleDispatch_(dctx, nullptr, handle, nullptr, nullptr);
375: }

377: template <DeviceType T>
378: inline PetscErrorCode CUPMObject<T>::GetSparseHandles_(cupmSparseHandle_t *handle) noexcept
379: {
380:   return GetHandleDispatch_(nullptr, nullptr, handle, nullptr, nullptr);
381: }

383: template <DeviceType T>
384: inline PetscErrorCode CUPMObject<T>::GetSparseHandlesFrom_(PetscDeviceContext dctx, cupmSparseHandle_t *handle, cupmStream_t *stream) noexcept
385: {
386:   return GetFromHandleDispatch_(dctx, nullptr, handle, nullptr, stream);
387: }

389: template <DeviceType T>
390: inline UseCUPMHostAllocGuard<T> CUPMObject<T>::UseCUPMHostAlloc(bool b) noexcept
391: {
392:   return {b};
393: }

395: template <DeviceType T>
396: inline PetscErrorCode CUPMObject<T>::CheckPointerMatchesMemType_(const void *ptr, PetscMemType mtype) noexcept
397: {
398:   PetscFunctionBegin;
399:   if (PetscDefined(USE_DEBUG) && ptr) {
400:     PetscMemType ptr_mtype;
401:     PetscBool    managed;

403:     PetscCall(PetscCUPMGetMemType(ptr, &ptr_mtype, nullptr, &managed));
404:     if (mtype == PETSC_MEMTYPE_HOST) {
405:       PetscCheck(managed || PetscMemTypeHost(ptr_mtype), PETSC_COMM_SELF, PETSC_ERR_POINTER, "Pointer %p declared as %s does not match actual memtype %s", ptr, PetscMemTypeToString(mtype), PetscMemTypeToString(ptr_mtype));
406:     } else if (mtype == PETSC_MEMTYPE_DEVICE) {
407:       // generic "device" memory should only care if the actual memtype is also generically
408:       // "device"
409:       PetscCheck(managed || PetscMemTypeDevice(ptr_mtype), PETSC_COMM_SELF, PETSC_ERR_POINTER, "Pointer %p declared as %s does not match actual memtype %s", ptr, PetscMemTypeToString(mtype), PetscMemTypeToString(ptr_mtype));
410:     } else {
411:       PetscCheck(mtype == ptr_mtype, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Pointer %p declared as %s does not match actual memtype %s", ptr, PetscMemTypeToString(mtype), PetscMemTypeToString(ptr_mtype));
412:     }
413:   }
414:   PetscFunctionReturn(PETSC_SUCCESS);
415: }

417: #define PETSC_CUPMOBJECT_HEADER(T) \
418:   PETSC_CUPMSOLVER_INHERIT_INTERFACE_TYPEDEFS_USING(T); \
419:   using ::Petsc::device::cupm::impl::CUPMObject<T>::UseCUPMHostAlloc; \
420:   using ::Petsc::device::cupm::impl::CUPMObject<T>::GetHandles_; \
421:   using ::Petsc::device::cupm::impl::CUPMObject<T>::GetHandlesFrom_; \
422:   using ::Petsc::device::cupm::impl::CUPMObject<T>::GetSparseHandles_; \
423:   using ::Petsc::device::cupm::impl::CUPMObject<T>::GetSparseHandlesFrom_; \
424:   using ::Petsc::device::cupm::impl::CUPMObject<T>::PETSCDEVICERAND; \
425:   using ::Petsc::device::cupm::impl::CUPMObject<T>::CheckPointerMatchesMemType_

427: } // namespace impl

429: } // namespace cupm

431: } // namespace device

433: } // namespace Petsc