Actual source code: cupmcontext.hpp

  1: #ifndef PETSCDEVICECONTEXTCUPM_HPP
  2: #define PETSCDEVICECONTEXTCUPM_HPP

  4: #include <petsc/private/deviceimpl.h>
  5: #include <petsc/private/cupmblasinterface.hpp>
  6: #include <petsc/private/logimpl.h>

  8: #include <petsc/private/cpp/array.hpp>

 10: #include "../segmentedmempool.hpp"
 11: #include "cupmallocator.hpp"
 12: #include "cupmstream.hpp"
 13: #include "cupmevent.hpp"

 15: #if defined(__cplusplus)

 17: namespace Petsc
 18: {

 20: namespace device
 21: {

 23: namespace cupm
 24: {

 26: namespace impl
 27: {

 29: template <DeviceType T>
 30: class DeviceContext : BlasInterface<T> {
 31: public:
 32:   PETSC_CUPMBLAS_INHERIT_INTERFACE_TYPEDEFS_USING(cupmBlasInterface_t, T);

 34: private:
 35:   template <typename H, std::size_t>
 36:   struct HandleTag {
 37:     using type = H;
 38:   };

 40:   using stream_tag = HandleTag<cupmStream_t, 0>;
 41:   using blas_tag   = HandleTag<cupmBlasHandle_t, 1>;
 42:   using solver_tag = HandleTag<cupmSolverHandle_t, 2>;

 44:   using stream_type = CUPMStream<T>;
 45:   using event_type  = CUPMEvent<T>;

 47: public:
 48:   // This is the canonical PETSc "impls" struct that normally resides in a standalone impls
 49:   // header, but since we are using the power of templates it must be declared part of
 50:   // this class to have easy access the same typedefs. Technically one can make a
 51:   // templated struct outside the class but it's more code for the same result.
 52:   struct PetscDeviceContext_IMPLS : memory::PoolAllocated<PetscDeviceContext_IMPLS> {
 53:     stream_type stream{};
 54:     cupmEvent_t event{};
 55:     cupmEvent_t begin{}; // timer-only
 56:     cupmEvent_t end{};   // timer-only
 57:   #if PetscDefined(USE_DEBUG)
 58:     PetscBool timerInUse{};
 59:   #endif
 60:     cupmBlasHandle_t   blas{};
 61:     cupmSolverHandle_t solver{};

 63:     constexpr PetscDeviceContext_IMPLS() noexcept = default;

 65:     PETSC_NODISCARD cupmStream_t get(stream_tag) const noexcept { return this->stream.get_stream(); }

 67:     PETSC_NODISCARD cupmBlasHandle_t get(blas_tag) const noexcept { return this->blas; }

 69:     PETSC_NODISCARD cupmSolverHandle_t get(solver_tag) const noexcept { return this->solver; }
 70:   };

 72: private:
 73:   static bool                                                     initialized_;
 74:   static std::array<cupmBlasHandle_t, PETSC_DEVICE_MAX_DEVICES>   blashandles_;
 75:   static std::array<cupmSolverHandle_t, PETSC_DEVICE_MAX_DEVICES> solverhandles_;

 77:   PETSC_NODISCARD static constexpr PetscDeviceContext_IMPLS *impls_cast_(PetscDeviceContext ptr) noexcept { return static_cast<PetscDeviceContext_IMPLS *>(ptr->data); }

 79:   PETSC_NODISCARD static constexpr CUPMEvent<T> *event_cast_(PetscEvent event) noexcept { return static_cast<CUPMEvent<T> *>(event->data); }

 81:   PETSC_NODISCARD static PetscLogEvent CUPMBLAS_HANDLE_CREATE() noexcept { return T == DeviceType::CUDA ? CUBLAS_HANDLE_CREATE : HIPBLAS_HANDLE_CREATE; }

 83:   PETSC_NODISCARD static PetscLogEvent CUPMSOLVER_HANDLE_CREATE() noexcept { return T == DeviceType::CUDA ? CUSOLVER_HANDLE_CREATE : HIPSOLVER_HANDLE_CREATE; }

 85:   // this exists purely to satisfy the compiler so the tag-based dispatch works for the other
 86:   // handles
 87:   PETSC_CXX_COMPAT_DECL(PetscErrorCode initialize_handle_(stream_tag, PetscDeviceContext)) { return 0; }

 89:   PETSC_NODISCARD static PetscErrorCode create_handle_(blas_tag, cupmBlasHandle_t &handle) noexcept
 90:   {
 91:     PetscLogEvent event;

 93:     if (PetscLikely(handle)) return 0;
 94:     PetscLogPauseCurrentEvent_Internal(&event);
 95:     PetscLogEventBegin(CUPMBLAS_HANDLE_CREATE(), 0, 0, 0, 0);
 96:     for (auto i = 0; i < 3; ++i) {
 97:       auto cberr = cupmBlasCreate(&handle);
 98:       if (PetscLikely(cberr == CUPMBLAS_STATUS_SUCCESS)) break;
 99:       if (PetscUnlikely(cberr != CUPMBLAS_STATUS_ALLOC_FAILED) && (cberr != CUPMBLAS_STATUS_NOT_INITIALIZED)) cberr;
100:       if (i != 2) {
101:         PetscSleep(3);
102:         continue;
103:       }
105:     }
106:     PetscLogEventEnd(CUPMBLAS_HANDLE_CREATE(), 0, 0, 0, 0);
107:     PetscLogEventResume_Internal(event);
108:     return 0;
109:   }

111:   PETSC_NODISCARD static PetscErrorCode initialize_handle_(blas_tag tag, PetscDeviceContext dctx) noexcept
112:   {
113:     const auto dci    = impls_cast_(dctx);
114:     auto      &handle = blashandles_[dctx->device->deviceId];

116:     create_handle_(tag, handle);
117:     cupmBlasSetStream(handle, dci->stream.get_stream());
118:     dci->blas = handle;
119:     return 0;
120:   }

122:   PETSC_CXX_COMPAT_DECL(PetscErrorCode create_handle_(solver_tag, cupmSolverHandle_t &handle))
123:   {
124:     PetscLogEvent event;

126:     PetscLogPauseCurrentEvent_Internal(&event);
127:     PetscLogEventBegin(CUPMSOLVER_HANDLE_CREATE(), 0, 0, 0, 0);
128:     cupmBlasInterface_t::InitializeHandle(handle);
129:     PetscLogEventEnd(CUPMSOLVER_HANDLE_CREATE(), 0, 0, 0, 0);
130:     PetscLogEventResume_Internal(event);
131:     return 0;
132:   }

134:   PETSC_NODISCARD static PetscErrorCode initialize_handle_(solver_tag tag, PetscDeviceContext dctx) noexcept
135:   {
136:     const auto dci    = impls_cast_(dctx);
137:     auto      &handle = solverhandles_[dctx->device->deviceId];

139:     create_handle_(tag, handle);
140:     cupmBlasInterface_t::SetHandleStream(handle, dci->stream.get_stream());
141:     dci->solver = handle;
142:     return 0;
143:   }

145:   PETSC_NODISCARD static PetscErrorCode check_current_device_(PetscDeviceContext dctxl, PetscDeviceContext dctxr) noexcept
146:   {
147:     const auto devidl = dctxl->device->deviceId, devidr = dctxr->device->deviceId;

150:                PetscObjectCast(dctxl)->id, devidl, PetscObjectCast(dctxr)->id, devidr);
151:     PetscDeviceCheckDeviceCount_Internal(devidl);
152:     PetscDeviceCheckDeviceCount_Internal(devidr);
153:     cupmSetDevice(static_cast<int>(devidl));
154:     return 0;
155:   }

157:   PETSC_NODISCARD static PetscErrorCode check_current_device_(PetscDeviceContext dctx) noexcept { return check_current_device_(dctx, dctx); }

159:   PETSC_NODISCARD static PetscErrorCode finalize_() noexcept
160:   {
161:     for (auto &&handle : blashandles_) {
162:       if (handle) {
163:         cupmBlasDestroy(handle);
164:         handle = nullptr;
165:       }
166:     }
167:     for (auto &&handle : solverhandles_) {
168:       if (handle) {
169:         cupmBlasInterface_t::DestroyHandle(handle);
170:         handle = nullptr;
171:       }
172:     }
173:     initialized_ = false;
174:     return 0;
175:   }

177:   template <typename Allocator, typename PoolType = ::Petsc::memory::SegmentedMemoryPool<typename Allocator::value_type, stream_type, Allocator, 256 * sizeof(PetscScalar)>>
178:   PETSC_NODISCARD static PoolType &default_pool_() noexcept
179:   {
180:     static PoolType pool;
181:     return pool;
182:   }

184:   PETSC_NODISCARD static PetscErrorCode check_memtype_(PetscMemType mtype, const char mess[]) noexcept
185:   {
187:     return 0;
188:   }

190: public:
191:   // All of these functions MUST be static in order to be callable from C, otherwise they
192:   // get the implicit 'this' pointer tacked on
193:   PETSC_CXX_COMPAT_DECL(PetscErrorCode destroy(PetscDeviceContext));
194:   PETSC_CXX_COMPAT_DECL(PetscErrorCode changeStreamType(PetscDeviceContext, PetscStreamType));
195:   PETSC_CXX_COMPAT_DECL(PetscErrorCode setUp(PetscDeviceContext));
196:   PETSC_CXX_COMPAT_DECL(PetscErrorCode query(PetscDeviceContext, PetscBool *));
197:   PETSC_CXX_COMPAT_DECL(PetscErrorCode waitForContext(PetscDeviceContext, PetscDeviceContext));
198:   PETSC_CXX_COMPAT_DECL(PetscErrorCode synchronize(PetscDeviceContext));
199:   template <typename Handle_t>
200:   PETSC_CXX_COMPAT_DECL(PetscErrorCode getHandle(PetscDeviceContext, void *));
201:   PETSC_CXX_COMPAT_DECL(PetscErrorCode beginTimer(PetscDeviceContext));
202:   PETSC_CXX_COMPAT_DECL(PetscErrorCode endTimer(PetscDeviceContext, PetscLogDouble *));
203:   PETSC_CXX_COMPAT_DECL(PetscErrorCode memAlloc(PetscDeviceContext, PetscBool, PetscMemType, std::size_t, std::size_t, void **));
204:   PETSC_CXX_COMPAT_DECL(PetscErrorCode memFree(PetscDeviceContext, PetscMemType, void **));
205:   PETSC_CXX_COMPAT_DECL(PetscErrorCode memCopy(PetscDeviceContext, void *PETSC_RESTRICT, const void *PETSC_RESTRICT, std::size_t, PetscDeviceCopyMode));
206:   PETSC_CXX_COMPAT_DECL(PetscErrorCode memSet(PetscDeviceContext, PetscMemType, void *, PetscInt, std::size_t));
207:   PETSC_CXX_COMPAT_DECL(PetscErrorCode createEvent(PetscDeviceContext, PetscEvent));
208:   PETSC_CXX_COMPAT_DECL(PetscErrorCode recordEvent(PetscDeviceContext, PetscEvent));
209:   PETSC_CXX_COMPAT_DECL(PetscErrorCode waitForEvent(PetscDeviceContext, PetscEvent));

211:   // not a PetscDeviceContext method, this registers the class
212:   PETSC_CXX_COMPAT_DECL(PetscErrorCode initialize());

214:   // clang-format off
215:   const _DeviceContextOps ops = {
216:     destroy,
217:     changeStreamType,
218:     setUp,
219:     query,
220:     waitForContext,
221:     synchronize,
222:     getHandle<blas_tag>,
223:     getHandle<solver_tag>,
224:     getHandle<stream_tag>,
225:     beginTimer,
226:     endTimer,
227:     memAlloc,
228:     memFree,
229:     memCopy,
230:     memSet,
231:     createEvent,
232:     recordEvent,
233:     waitForEvent
234:   };
235:   // clang-format on
236: };

238: // not a PetscDeviceContext method, this initializes the CLASS
239: template <DeviceType T>
240: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::initialize())
241: {
242:   if (PetscUnlikely(!initialized_)) {
243:     cupmMemPool_t mempool;
244:     uint64_t      threshold = UINT64_MAX;

246:     initialized_ = true;
247:     cupmDeviceGetMemPool(&mempool, 0);
248:     cupmMemPoolSetAttribute(mempool, cupmMemPoolAttrReleaseThreshold, &threshold);
249:     blashandles_.fill(nullptr);
250:     solverhandles_.fill(nullptr);
251:     PetscRegisterFinalize(finalize_);
252:   }
253:   return 0;
254: }

256: template <DeviceType T>
257: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::destroy(PetscDeviceContext dctx))
258: {
259:   if (const auto dci = impls_cast_(dctx)) {
260:     dci->stream.destroy();
261:     if (dci->event) cupm_fast_event_pool<T>().deallocate(std::move(dci->event));
262:     if (dci->begin) cupmEventDestroy(dci->begin);
263:     if (dci->end) cupmEventDestroy(dci->end);
264:     delete dci;
265:     dctx->data = nullptr;
266:   }
267:   return 0;
268: }

270: template <DeviceType T>
271: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::changeStreamType(PetscDeviceContext dctx, PETSC_UNUSED PetscStreamType stype))
272: {
273:   const auto dci = impls_cast_(dctx);

275:   dci->stream.destroy();
276:   // set these to null so they aren't usable until setup is called again
277:   dci->blas   = nullptr;
278:   dci->solver = nullptr;
279:   return 0;
280: }

282: template <DeviceType T>
283: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::setUp(PetscDeviceContext dctx))
284: {
285:   const auto dci   = impls_cast_(dctx);
286:   auto      &event = dci->event;

288:   check_current_device_(dctx);
289:   dci->stream.change_type(dctx->streamType);
290:   if (!event) cupm_fast_event_pool<T>().allocate(&event);
291:   #if PetscDefined(USE_DEBUG)
292:   dci->timerInUse = PETSC_FALSE;
293:   #endif
294:   return 0;
295: }

297: template <DeviceType T>
298: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::query(PetscDeviceContext dctx, PetscBool *idle))
299: {
300:   check_current_device_(dctx);
301:   switch (const auto cerr = cupmStreamQuery(impls_cast_(dctx)->stream.get_stream())) {
302:   case cupmSuccess:
303:     *idle = PETSC_TRUE;
304:     break;
305:   case cupmErrorNotReady:
306:     *idle = PETSC_FALSE;
307:     break;
308:   default:
309:     cerr;
310:     PetscUnreachable();
311:   }
312:   return 0;
313: }

315: template <DeviceType T>
316: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::waitForContext(PetscDeviceContext dctxa, PetscDeviceContext dctxb))
317: {
318:   const auto dcib  = impls_cast_(dctxb);
319:   const auto event = dcib->event;

321:   check_current_device_(dctxa, dctxb);
322:   cupmEventRecord(event, dcib->stream.get_stream());
323:   cupmStreamWaitEvent(impls_cast_(dctxa)->stream.get_stream(), event, 0);
324:   return 0;
325: }

327: template <DeviceType T>
328: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::synchronize(PetscDeviceContext dctx))
329: {
330:   auto idle = PETSC_TRUE;

332:   query(dctx, &idle);
333:   if (!idle) cupmStreamSynchronize(impls_cast_(dctx)->stream.get_stream());
334:   return 0;
335: }

337: template <DeviceType T>
338: template <typename handle_t>
339: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::getHandle(PetscDeviceContext dctx, void *handle))
340: {
341:   initialize_handle_(handle_t{}, dctx);
342:   *static_cast<typename handle_t::type *>(handle) = impls_cast_(dctx)->get(handle_t{});
343:   return 0;
344: }

346: template <DeviceType T>
347: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::beginTimer(PetscDeviceContext dctx))
348: {
349:   const auto dci = impls_cast_(dctx);

351:   check_current_device_(dctx);
352:   #if PetscDefined(USE_DEBUG)
354:   dci->timerInUse = PETSC_TRUE;
355:   #endif
356:   if (!dci->begin) {
357:     PetscAssert(!dci->end, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Don't have a 'begin' event, but somehow have an end event");
358:     cupmEventCreate(&dci->begin);
359:     cupmEventCreate(&dci->end);
360:   }
361:   cupmEventRecord(dci->begin, dci->stream.get_stream());
362:   return 0;
363: }

365: template <DeviceType T>
366: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::endTimer(PetscDeviceContext dctx, PetscLogDouble *elapsed))
367: {
368:   float      gtime;
369:   const auto dci = impls_cast_(dctx);
370:   const auto end = dci->end;

372:   check_current_device_(dctx);
373:   #if PetscDefined(USE_DEBUG)
375:   dci->timerInUse = PETSC_FALSE;
376:   #endif
377:   cupmEventRecord(end, dci->stream.get_stream());
378:   cupmEventSynchronize(end);
379:   cupmEventElapsedTime(&gtime, dci->begin, end);
380:   *elapsed = static_cast<util::remove_pointer_t<decltype(elapsed)>>(gtime);
381:   return 0;
382: }

384: template <DeviceType T>
385: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::memAlloc(PetscDeviceContext dctx, PetscBool clear, PetscMemType mtype, std::size_t n, std::size_t alignment, void **dest))
386: {
387:   const auto &stream = impls_cast_(dctx)->stream;

389:   check_current_device_(dctx);
390:   check_memtype_(mtype, "allocating");
391:   if (PetscMemTypeHost(mtype)) {
392:     default_pool_<HostAllocator<T>>().allocate(n, reinterpret_cast<char **>(dest), &stream, alignment);
393:   } else {
394:     default_pool_<DeviceAllocator<T>>().allocate(n, reinterpret_cast<char **>(dest), &stream, alignment);
395:   }
396:   if (clear) cupmMemsetAsync(*dest, 0, n, stream.get_stream());
397:   return 0;
398: }

400: template <DeviceType T>
401: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::memFree(PetscDeviceContext dctx, PetscMemType mtype, void **ptr))
402: {
403:   const auto &stream = impls_cast_(dctx)->stream;

405:   check_current_device_(dctx);
406:   check_memtype_(mtype, "freeing");
407:   if (!*ptr) return 0;
408:   if (PetscMemTypeHost(mtype)) {
409:     default_pool_<HostAllocator<T>>().deallocate(reinterpret_cast<char **>(ptr), &stream);
410:     // if ptr exists still exists the pool didn't own it
411:     if (*ptr) {
412:       auto registered = PETSC_FALSE, managed = PETSC_FALSE;

414:       PetscCUPMGetMemType(*ptr, nullptr, &registered, &managed);
415:       if (registered) {
416:         cupmFreeHost(*ptr);
417:       } else if (managed) {
418:         cupmFreeAsync(*ptr, stream.get_stream());
419:       }
420:     }
421:   } else {
422:     default_pool_<DeviceAllocator<T>>().deallocate(reinterpret_cast<char **>(ptr), &stream);
423:     // if ptr exists still exists the pool didn't own it
424:     if (*ptr) cupmFreeAsync(*ptr, stream.get_stream());
425:   }
426:   return 0;
427: }

429: template <DeviceType T>
430: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::memCopy(PetscDeviceContext dctx, void *PETSC_RESTRICT dest, const void *PETSC_RESTRICT src, std::size_t n, PetscDeviceCopyMode mode))
431: {
432:   const auto stream = impls_cast_(dctx)->stream.get_stream();

434:   // can't use PetscCUPMMemcpyAsync here since we don't know sizeof(*src)...
435:   if (mode == PETSC_DEVICE_COPY_HTOH) {
436:     // yes this is faster
437:     if (cupmStreamQuery(stream) == cupmSuccess) {
438:       PetscMemcpy(dest, src, n);
439:       return 0;
440:     }
441:     // in case cupmStreamQuery() did not return cupmErrorNotReady
442:     cupmGetLastError();
443:   }
444:   cupmMemcpyAsync(dest, src, n, PetscDeviceCopyModeToCUPMMemcpyKind(mode), stream);
445:   return 0;
446: }

448: template <DeviceType T>
449: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::memSet(PetscDeviceContext dctx, PetscMemType mtype, void *ptr, PetscInt v, std::size_t n))
450: {
451:   check_current_device_(dctx);
452:   check_memtype_(mtype, "zeroing");
453:   cupmMemsetAsync(ptr, static_cast<int>(v), n, impls_cast_(dctx)->stream.get_stream());
454:   return 0;
455: }

457: template <DeviceType T>
458: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::createEvent(PetscDeviceContext dctx, PetscEvent event))
459: {
460:   event->data = new event_type();
461:   event->destroy = [](PetscEvent event) {
462:     delete event_cast_(event);
463:     event->data = nullptr;
464:     return 0;
465:   };
466:   return 0;
467: }

469: template <DeviceType T>
470: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::recordEvent(PetscDeviceContext dctx, PetscEvent event))
471: {
472:   impls_cast_(dctx)->stream.record_event(*event_cast_(event));
473:   return 0;
474: }

476: template <DeviceType T>
477: PETSC_CXX_COMPAT_DEFN(PetscErrorCode DeviceContext<T>::waitForEvent(PetscDeviceContext dctx, PetscEvent event))
478: {
479:   impls_cast_(dctx)->stream.wait_for_event(*event_cast_(event));
480:   return 0;
481: }

483: // initialize the static member variables
484: template <DeviceType T>
485: bool DeviceContext<T>::initialized_ = false;

487: template <DeviceType T>
488: std::array<typename DeviceContext<T>::cupmBlasHandle_t, PETSC_DEVICE_MAX_DEVICES> DeviceContext<T>::blashandles_ = {};

490: template <DeviceType T>
491: std::array<typename DeviceContext<T>::cupmSolverHandle_t, PETSC_DEVICE_MAX_DEVICES> DeviceContext<T>::solverhandles_ = {};

493: } // namespace impl

495: // shorten this one up a bit (and instantiate the templates)
496: using CUPMContextCuda = impl::DeviceContext<DeviceType::CUDA>;
497: using CUPMContextHip  = impl::DeviceContext<DeviceType::HIP>;

499:   // shorthand for what is an EXTREMELY long name
500:   #define PetscDeviceContext_(IMPLS) ::Petsc::device::cupm::impl::DeviceContext<::Petsc::device::cupm::DeviceType::IMPLS>::PetscDeviceContext_IMPLS

502: } // namespace cupm

504: } // namespace device

506: } // namespace Petsc

508: #endif // __cplusplus

510: #endif // PETSCDEVICECONTEXTCUDA_HPP