1 #ifndef PETSCDEVICECONTEXTCUPM_HPP 2 #define PETSCDEVICECONTEXTCUPM_HPP 3 4 #include <petsc/private/deviceimpl.h> 5 #include <petsc/private/cupmsolverinterface.hpp> 6 #include <petsc/private/logimpl.h> 7 8 #include <petsc/private/cpp/array.hpp> 9 10 #include "../segmentedmempool.hpp" 11 #include "cupmallocator.hpp" 12 #include "cupmstream.hpp" 13 #include "cupmevent.hpp" 14 15 namespace Petsc 16 { 17 18 namespace device 19 { 20 21 namespace cupm 22 { 23 24 namespace impl 25 { 26 27 template <DeviceType T> 28 class DeviceContext : SolverInterface<T> { 29 public: 30 PETSC_CUPMSOLVER_INHERIT_INTERFACE_TYPEDEFS_USING(T); 31 32 private: 33 template <typename H, std::size_t> 34 struct HandleTag { 35 using type = H; 36 }; 37 38 using stream_tag = HandleTag<cupmStream_t, 0>; 39 using blas_tag = HandleTag<cupmBlasHandle_t, 1>; 40 using solver_tag = HandleTag<cupmSolverHandle_t, 2>; 41 42 using stream_type = CUPMStream<T>; 43 using event_type = CUPMEvent<T>; 44 45 public: 46 // This is the canonical PETSc "impls" struct that normally resides in a standalone impls 47 // header, but since we are using the power of templates it must be declared part of 48 // this class to have easy access the same typedefs. Technically one can make a 49 // templated struct outside the class but it's more code for the same result. 50 struct PetscDeviceContext_IMPLS : memory::PoolAllocated<PetscDeviceContext_IMPLS> { 51 stream_type stream{}; 52 cupmEvent_t event{}; 53 cupmEvent_t begin{}; // timer-only 54 cupmEvent_t end{}; // timer-only 55 #if PetscDefined(USE_DEBUG) 56 PetscBool timerInUse{}; 57 #endif 58 cupmBlasHandle_t blas{}; 59 cupmSolverHandle_t solver{}; 60 61 constexpr PetscDeviceContext_IMPLS() noexcept = default; 62 63 PETSC_NODISCARD const cupmStream_t &get(stream_tag) const noexcept { return this->stream.get_stream(); } 64 65 PETSC_NODISCARD const cupmBlasHandle_t &get(blas_tag) const noexcept { return this->blas; } 66 67 PETSC_NODISCARD const cupmSolverHandle_t &get(solver_tag) const noexcept { return this->solver; } 68 }; 69 70 private: 71 static bool initialized_; 72 73 static std::array<cupmBlasHandle_t, PETSC_DEVICE_MAX_DEVICES> blashandles_; 74 static std::array<cupmSolverHandle_t, PETSC_DEVICE_MAX_DEVICES> solverhandles_; 75 76 PETSC_NODISCARD static constexpr PetscDeviceContext_IMPLS *impls_cast_(PetscDeviceContext ptr) noexcept { return static_cast<PetscDeviceContext_IMPLS *>(ptr->data); } 77 78 PETSC_NODISCARD static constexpr CUPMEvent<T> *event_cast_(PetscEvent event) noexcept { return static_cast<CUPMEvent<T> *>(event->data); } 79 80 PETSC_NODISCARD static PetscLogEvent CUPMBLAS_HANDLE_CREATE() noexcept { return T == DeviceType::CUDA ? CUBLAS_HANDLE_CREATE : HIPBLAS_HANDLE_CREATE; } 81 82 PETSC_NODISCARD static PetscLogEvent CUPMSOLVER_HANDLE_CREATE() noexcept { return T == DeviceType::CUDA ? CUSOLVER_HANDLE_CREATE : HIPSOLVER_HANDLE_CREATE; } 83 84 // this exists purely to satisfy the compiler so the tag-based dispatch works for the other 85 // handles 86 static PetscErrorCode initialize_handle_(stream_tag, PetscDeviceContext) noexcept { return PETSC_SUCCESS; } 87 88 static PetscErrorCode initialize_handle_(blas_tag, PetscDeviceContext dctx) noexcept 89 { 90 const auto dci = impls_cast_(dctx); 91 auto &handle = blashandles_[dctx->device->deviceId]; 92 93 PetscFunctionBegin; 94 if (!handle) { 95 PetscCall(PetscLogEventsPause()); 96 PetscCall(PetscLogEventBegin(CUPMBLAS_HANDLE_CREATE(), 0, 0, 0, 0)); 97 for (auto i = 0; i < 3; ++i) { 98 const auto cberr = cupmBlasCreate(handle.ptr_to()); 99 if (PetscLikely(cberr == CUPMBLAS_STATUS_SUCCESS)) break; 100 if (PetscUnlikely(cberr != CUPMBLAS_STATUS_ALLOC_FAILED) && (cberr != CUPMBLAS_STATUS_NOT_INITIALIZED)) PetscCallCUPMBLAS(cberr); 101 if (i != 2) { 102 PetscCall(PetscSleep(3)); 103 continue; 104 } 105 PetscCheck(cberr == CUPMBLAS_STATUS_SUCCESS, PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, "Unable to initialize %s", cupmBlasName()); 106 } 107 PetscCall(PetscLogEventEnd(CUPMBLAS_HANDLE_CREATE(), 0, 0, 0, 0)); 108 PetscCall(PetscLogEventsResume()); 109 } 110 PetscCallCUPMBLAS(cupmBlasSetStream(handle, dci->stream.get_stream())); 111 dci->blas = handle; 112 PetscFunctionReturn(PETSC_SUCCESS); 113 } 114 115 static PetscErrorCode initialize_handle_(solver_tag, PetscDeviceContext dctx) noexcept 116 { 117 const auto dci = impls_cast_(dctx); 118 auto &handle = solverhandles_[dctx->device->deviceId]; 119 120 PetscFunctionBegin; 121 if (!handle) { 122 PetscCall(PetscLogEventsPause()); 123 PetscCall(PetscLogEventBegin(CUPMSOLVER_HANDLE_CREATE(), 0, 0, 0, 0)); 124 for (auto i = 0; i < 3; ++i) { 125 const auto cerr = cupmSolverCreate(&handle); 126 if (PetscLikely(cerr == CUPMSOLVER_STATUS_SUCCESS)) break; 127 if ((cerr != CUPMSOLVER_STATUS_NOT_INITIALIZED) && (cerr != CUPMSOLVER_STATUS_ALLOC_FAILED)) PetscCallCUPMSOLVER(cerr); 128 if (i < 2) { 129 PetscCall(PetscSleep(3)); 130 continue; 131 } 132 PetscCheck(cerr == CUPMSOLVER_STATUS_SUCCESS, PETSC_COMM_SELF, PETSC_ERR_GPU_RESOURCE, "Unable to initialize %s", cupmSolverName()); 133 } 134 PetscCall(PetscLogEventEnd(CUPMSOLVER_HANDLE_CREATE(), 0, 0, 0, 0)); 135 PetscCall(PetscLogEventsResume()); 136 } 137 PetscCallCUPMSOLVER(cupmSolverSetStream(handle, dci->stream.get_stream())); 138 dci->solver = handle; 139 PetscFunctionReturn(PETSC_SUCCESS); 140 } 141 142 static PetscErrorCode check_current_device_(PetscDeviceContext dctxl, PetscDeviceContext dctxr) noexcept 143 { 144 const auto devidl = dctxl->device->deviceId, devidr = dctxr->device->deviceId; 145 146 PetscFunctionBegin; 147 PetscCheck(devidl == devidr, PETSC_COMM_SELF, PETSC_ERR_GPU, "Device contexts must be on the same device; dctx A (id %" PetscInt64_FMT " device id %" PetscInt_FMT ") dctx B (id %" PetscInt64_FMT " device id %" PetscInt_FMT ")", 148 PetscObjectCast(dctxl)->id, devidl, PetscObjectCast(dctxr)->id, devidr); 149 PetscCall(PetscDeviceCheckDeviceCount_Internal(devidl)); 150 PetscCall(PetscDeviceCheckDeviceCount_Internal(devidr)); 151 PetscCallCUPM(cupmSetDevice(static_cast<int>(devidl))); 152 PetscFunctionReturn(PETSC_SUCCESS); 153 } 154 155 static PetscErrorCode check_current_device_(PetscDeviceContext dctx) noexcept { return check_current_device_(dctx, dctx); } 156 157 static PetscErrorCode finalize_() noexcept 158 { 159 PetscFunctionBegin; 160 for (auto &&handle : blashandles_) { 161 if (handle) { 162 PetscCallCUPMBLAS(cupmBlasDestroy(handle)); 163 handle = nullptr; 164 } 165 } 166 167 for (auto &&handle : solverhandles_) { 168 if (handle) { 169 PetscCallCUPMSOLVER(cupmSolverDestroy(handle)); 170 handle = nullptr; 171 } 172 } 173 initialized_ = false; 174 PetscFunctionReturn(PETSC_SUCCESS); 175 } 176 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 } 183 184 static PetscErrorCode check_memtype_(PetscMemType mtype, const char mess[]) noexcept 185 { 186 PetscFunctionBegin; 187 PetscCheck(PetscMemTypeHost(mtype) || (mtype == PETSC_MEMTYPE_DEVICE) || (mtype == PETSC_MEMTYPE_CUPM()), PETSC_COMM_SELF, PETSC_ERR_SUP, "%s device context can only handle %s (pinned) host or device memory", cupmName(), mess); 188 PetscFunctionReturn(PETSC_SUCCESS); 189 } 190 191 public: 192 // All of these functions MUST be static in order to be callable from C, otherwise they 193 // get the implicit 'this' pointer tacked on 194 static PetscErrorCode destroy(PetscDeviceContext) noexcept; 195 static PetscErrorCode changeStreamType(PetscDeviceContext, PetscStreamType) noexcept; 196 static PetscErrorCode setUp(PetscDeviceContext) noexcept; 197 static PetscErrorCode query(PetscDeviceContext, PetscBool *) noexcept; 198 static PetscErrorCode waitForContext(PetscDeviceContext, PetscDeviceContext) noexcept; 199 static PetscErrorCode synchronize(PetscDeviceContext) noexcept; 200 template <typename Handle_t> 201 static PetscErrorCode getHandle(PetscDeviceContext, void *) noexcept; 202 template <typename Handle_t> 203 static PetscErrorCode getHandlePtr(PetscDeviceContext, void **) noexcept; 204 static PetscErrorCode beginTimer(PetscDeviceContext) noexcept; 205 static PetscErrorCode endTimer(PetscDeviceContext, PetscLogDouble *) noexcept; 206 static PetscErrorCode memAlloc(PetscDeviceContext, PetscBool, PetscMemType, std::size_t, std::size_t, void **) noexcept; 207 static PetscErrorCode memFree(PetscDeviceContext, PetscMemType, void **) noexcept; 208 static PetscErrorCode memCopy(PetscDeviceContext, void *PETSC_RESTRICT, const void *PETSC_RESTRICT, std::size_t, PetscDeviceCopyMode) noexcept; 209 static PetscErrorCode memSet(PetscDeviceContext, PetscMemType, void *, PetscInt, std::size_t) noexcept; 210 static PetscErrorCode createEvent(PetscDeviceContext, PetscEvent) noexcept; 211 static PetscErrorCode recordEvent(PetscDeviceContext, PetscEvent) noexcept; 212 static PetscErrorCode waitForEvent(PetscDeviceContext, PetscEvent) noexcept; 213 214 // not a PetscDeviceContext method, this registers the class 215 static PetscErrorCode initialize(PetscDevice) noexcept; 216 217 // clang-format off 218 static constexpr _DeviceContextOps ops = { 219 PetscDesignatedInitializer(destroy, destroy), 220 PetscDesignatedInitializer(changestreamtype, changeStreamType), 221 PetscDesignatedInitializer(setup, setUp), 222 PetscDesignatedInitializer(query, query), 223 PetscDesignatedInitializer(waitforcontext, waitForContext), 224 PetscDesignatedInitializer(synchronize, synchronize), 225 PetscDesignatedInitializer(getblashandle, getHandle<blas_tag>), 226 PetscDesignatedInitializer(getsolverhandle, getHandle<solver_tag>), 227 PetscDesignatedInitializer(getstreamhandle, getHandlePtr<stream_tag>), 228 PetscDesignatedInitializer(begintimer, beginTimer), 229 PetscDesignatedInitializer(endtimer, endTimer), 230 PetscDesignatedInitializer(memalloc, memAlloc), 231 PetscDesignatedInitializer(memfree, memFree), 232 PetscDesignatedInitializer(memcopy, memCopy), 233 PetscDesignatedInitializer(memset, memSet), 234 PetscDesignatedInitializer(createevent, createEvent), 235 PetscDesignatedInitializer(recordevent, recordEvent), 236 PetscDesignatedInitializer(waitforevent, waitForEvent) 237 }; 238 // clang-format on 239 }; 240 241 // not a PetscDeviceContext method, this initializes the CLASS 242 template <DeviceType T> 243 inline PetscErrorCode DeviceContext<T>::initialize(PetscDevice device) noexcept 244 { 245 PetscFunctionBegin; 246 if (PetscUnlikely(!initialized_)) { 247 uint64_t threshold = UINT64_MAX; 248 cupmMemPool_t mempool; 249 250 initialized_ = true; 251 PetscCallCUPM(cupmDeviceGetMemPool(&mempool, static_cast<int>(device->deviceId))); 252 PetscCallCUPM(cupmMemPoolSetAttribute(mempool, cupmMemPoolAttrReleaseThreshold, &threshold)); 253 blashandles_.fill(nullptr); 254 solverhandles_.fill(nullptr); 255 PetscCall(PetscRegisterFinalize(finalize_)); 256 } 257 PetscFunctionReturn(PETSC_SUCCESS); 258 } 259 260 template <DeviceType T> 261 inline PetscErrorCode DeviceContext<T>::destroy(PetscDeviceContext dctx) noexcept 262 { 263 PetscFunctionBegin; 264 if (const auto dci = impls_cast_(dctx)) { 265 PetscCall(dci->stream.destroy()); 266 if (dci->event) PetscCall(cupm_fast_event_pool<T>().deallocate(&dci->event)); 267 if (dci->begin) PetscCallCUPM(cupmEventDestroy(dci->begin)); 268 if (dci->end) PetscCallCUPM(cupmEventDestroy(dci->end)); 269 delete dci; 270 dctx->data = nullptr; 271 } 272 PetscFunctionReturn(PETSC_SUCCESS); 273 } 274 275 template <DeviceType T> 276 inline PetscErrorCode DeviceContext<T>::changeStreamType(PetscDeviceContext dctx, PETSC_UNUSED PetscStreamType stype) noexcept 277 { 278 const auto dci = impls_cast_(dctx); 279 280 PetscFunctionBegin; 281 PetscCall(dci->stream.destroy()); 282 // set these to null so they aren't usable until setup is called again 283 dci->blas = nullptr; 284 dci->solver = nullptr; 285 PetscFunctionReturn(PETSC_SUCCESS); 286 } 287 288 template <DeviceType T> 289 inline PetscErrorCode DeviceContext<T>::setUp(PetscDeviceContext dctx) noexcept 290 { 291 const auto dci = impls_cast_(dctx); 292 auto &event = dci->event; 293 294 PetscFunctionBegin; 295 PetscCall(check_current_device_(dctx)); 296 PetscCall(dci->stream.change_type(dctx->streamType)); 297 if (!event) PetscCall(cupm_fast_event_pool<T>().allocate(&event)); 298 #if PetscDefined(USE_DEBUG) 299 dci->timerInUse = PETSC_FALSE; 300 #endif 301 PetscFunctionReturn(PETSC_SUCCESS); 302 } 303 304 template <DeviceType T> 305 inline PetscErrorCode DeviceContext<T>::query(PetscDeviceContext dctx, PetscBool *idle) noexcept 306 { 307 PetscFunctionBegin; 308 PetscCall(check_current_device_(dctx)); 309 switch (auto cerr = cupmStreamQuery(impls_cast_(dctx)->stream.get_stream())) { 310 case cupmSuccess: 311 *idle = PETSC_TRUE; 312 break; 313 case cupmErrorNotReady: 314 *idle = PETSC_FALSE; 315 // reset the error 316 cerr = cupmGetLastError(); 317 static_cast<void>(cerr); 318 break; 319 default: 320 PetscCallCUPM(cerr); 321 PetscUnreachable(); 322 } 323 PetscFunctionReturn(PETSC_SUCCESS); 324 } 325 326 template <DeviceType T> 327 inline PetscErrorCode DeviceContext<T>::waitForContext(PetscDeviceContext dctxa, PetscDeviceContext dctxb) noexcept 328 { 329 const auto dcib = impls_cast_(dctxb); 330 const auto event = dcib->event; 331 332 PetscFunctionBegin; 333 PetscCall(check_current_device_(dctxa, dctxb)); 334 PetscCallCUPM(cupmEventRecord(event, dcib->stream.get_stream())); 335 PetscCallCUPM(cupmStreamWaitEvent(impls_cast_(dctxa)->stream.get_stream(), event, 0)); 336 PetscFunctionReturn(PETSC_SUCCESS); 337 } 338 339 template <DeviceType T> 340 inline PetscErrorCode DeviceContext<T>::synchronize(PetscDeviceContext dctx) noexcept 341 { 342 auto idle = PETSC_TRUE; 343 344 PetscFunctionBegin; 345 PetscCall(query(dctx, &idle)); 346 if (!idle) PetscCallCUPM(cupmStreamSynchronize(impls_cast_(dctx)->stream.get_stream())); 347 PetscFunctionReturn(PETSC_SUCCESS); 348 } 349 350 template <DeviceType T> 351 template <typename handle_t> 352 inline PetscErrorCode DeviceContext<T>::getHandle(PetscDeviceContext dctx, void *handle) noexcept 353 { 354 PetscFunctionBegin; 355 PetscCall(initialize_handle_(handle_t{}, dctx)); 356 *static_cast<typename handle_t::type *>(handle) = impls_cast_(dctx)->get(handle_t{}); 357 PetscFunctionReturn(PETSC_SUCCESS); 358 } 359 360 template <DeviceType T> 361 template <typename handle_t> 362 inline PetscErrorCode DeviceContext<T>::getHandlePtr(PetscDeviceContext dctx, void **handle) noexcept 363 { 364 using handle_type = typename handle_t::type; 365 366 PetscFunctionBegin; 367 PetscCall(initialize_handle_(handle_t{}, dctx)); 368 *reinterpret_cast<handle_type **>(handle) = const_cast<handle_type *>(std::addressof(impls_cast_(dctx)->get(handle_t{}))); 369 PetscFunctionReturn(PETSC_SUCCESS); 370 } 371 372 template <DeviceType T> 373 inline PetscErrorCode DeviceContext<T>::beginTimer(PetscDeviceContext dctx) noexcept 374 { 375 const auto dci = impls_cast_(dctx); 376 377 PetscFunctionBegin; 378 PetscCall(check_current_device_(dctx)); 379 #if PetscDefined(USE_DEBUG) 380 PetscCheck(!dci->timerInUse, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Forgot to call PetscLogGpuTimeEnd()?"); 381 dci->timerInUse = PETSC_TRUE; 382 #endif 383 if (!dci->begin) { 384 PetscAssert(!dci->end, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Don't have a 'begin' event, but somehow have an end event"); 385 PetscCallCUPM(cupmEventCreate(&dci->begin)); 386 PetscCallCUPM(cupmEventCreate(&dci->end)); 387 } 388 PetscCallCUPM(cupmEventRecord(dci->begin, dci->stream.get_stream())); 389 PetscFunctionReturn(PETSC_SUCCESS); 390 } 391 392 template <DeviceType T> 393 inline PetscErrorCode DeviceContext<T>::endTimer(PetscDeviceContext dctx, PetscLogDouble *elapsed) noexcept 394 { 395 float gtime; 396 const auto dci = impls_cast_(dctx); 397 const auto end = dci->end; 398 399 PetscFunctionBegin; 400 PetscCall(check_current_device_(dctx)); 401 #if PetscDefined(USE_DEBUG) 402 PetscCheck(dci->timerInUse, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Forgot to call PetscLogGpuTimeBegin()?"); 403 dci->timerInUse = PETSC_FALSE; 404 #endif 405 PetscCallCUPM(cupmEventRecord(end, dci->stream.get_stream())); 406 PetscCallCUPM(cupmEventSynchronize(end)); 407 PetscCallCUPM(cupmEventElapsedTime(>ime, dci->begin, end)); 408 *elapsed = static_cast<util::remove_pointer_t<decltype(elapsed)>>(gtime); 409 PetscFunctionReturn(PETSC_SUCCESS); 410 } 411 412 template <DeviceType T> 413 inline PetscErrorCode DeviceContext<T>::memAlloc(PetscDeviceContext dctx, PetscBool clear, PetscMemType mtype, std::size_t n, std::size_t alignment, void **dest) noexcept 414 { 415 const auto &stream = impls_cast_(dctx)->stream; 416 417 PetscFunctionBegin; 418 PetscCall(check_current_device_(dctx)); 419 PetscCall(check_memtype_(mtype, "allocating")); 420 if (PetscMemTypeHost(mtype)) { 421 PetscCall(default_pool_<HostAllocator<T>>().allocate(n, reinterpret_cast<char **>(dest), &stream, alignment)); 422 } else { 423 PetscCall(default_pool_<DeviceAllocator<T>>().allocate(n, reinterpret_cast<char **>(dest), &stream, alignment)); 424 } 425 if (clear) PetscCallCUPM(cupmMemsetAsync(*dest, 0, n, stream.get_stream())); 426 PetscFunctionReturn(PETSC_SUCCESS); 427 } 428 429 template <DeviceType T> 430 inline PetscErrorCode DeviceContext<T>::memFree(PetscDeviceContext dctx, PetscMemType mtype, void **ptr) noexcept 431 { 432 const auto &stream = impls_cast_(dctx)->stream; 433 434 PetscFunctionBegin; 435 PetscCall(check_current_device_(dctx)); 436 PetscCall(check_memtype_(mtype, "freeing")); 437 if (!*ptr) PetscFunctionReturn(PETSC_SUCCESS); 438 if (PetscMemTypeHost(mtype)) { 439 PetscCall(default_pool_<HostAllocator<T>>().deallocate(reinterpret_cast<char **>(ptr), &stream)); 440 // if ptr exists still exists the pool didn't own it 441 if (*ptr) { 442 auto registered = PETSC_FALSE, managed = PETSC_FALSE; 443 444 PetscCall(PetscCUPMGetMemType(*ptr, nullptr, ®istered, &managed)); 445 if (registered) { 446 PetscCallCUPM(cupmFreeHost(*ptr)); 447 } else if (managed) { 448 PetscCallCUPM(cupmFreeAsync(*ptr, stream.get_stream())); 449 } 450 } 451 } else { 452 PetscCall(default_pool_<DeviceAllocator<T>>().deallocate(reinterpret_cast<char **>(ptr), &stream)); 453 // if ptr still exists the pool didn't own it 454 if (*ptr) PetscCallCUPM(cupmFreeAsync(*ptr, stream.get_stream())); 455 } 456 PetscFunctionReturn(PETSC_SUCCESS); 457 } 458 459 template <DeviceType T> 460 inline PetscErrorCode DeviceContext<T>::memCopy(PetscDeviceContext dctx, void *PETSC_RESTRICT dest, const void *PETSC_RESTRICT src, std::size_t n, PetscDeviceCopyMode mode) noexcept 461 { 462 const auto stream = impls_cast_(dctx)->stream.get_stream(); 463 464 PetscFunctionBegin; 465 // can't use PetscCUPMMemcpyAsync here since we don't know sizeof(*src)... 466 if (mode == PETSC_DEVICE_COPY_HTOH) { 467 const auto cerr = cupmStreamQuery(stream); 468 469 // yes this is faster 470 if (cerr == cupmSuccess) { 471 PetscCall(PetscMemcpy(dest, src, n)); 472 PetscFunctionReturn(PETSC_SUCCESS); 473 } else if (cerr == cupmErrorNotReady) { 474 auto PETSC_UNUSED unused = cupmGetLastError(); 475 476 static_cast<void>(unused); 477 } else { 478 PetscCallCUPM(cerr); 479 } 480 } 481 PetscCallCUPM(cupmMemcpyAsync(dest, src, n, PetscDeviceCopyModeToCUPMMemcpyKind(mode), stream)); 482 PetscFunctionReturn(PETSC_SUCCESS); 483 } 484 485 template <DeviceType T> 486 inline PetscErrorCode DeviceContext<T>::memSet(PetscDeviceContext dctx, PetscMemType mtype, void *ptr, PetscInt v, std::size_t n) noexcept 487 { 488 PetscFunctionBegin; 489 PetscCall(check_current_device_(dctx)); 490 PetscCall(check_memtype_(mtype, "zeroing")); 491 PetscCallCUPM(cupmMemsetAsync(ptr, static_cast<int>(v), n, impls_cast_(dctx)->stream.get_stream())); 492 PetscFunctionReturn(PETSC_SUCCESS); 493 } 494 495 template <DeviceType T> 496 inline PetscErrorCode DeviceContext<T>::createEvent(PetscDeviceContext, PetscEvent event) noexcept 497 { 498 PetscFunctionBegin; 499 PetscCallCXX(event->data = new event_type()); 500 event->destroy = [](PetscEvent event) { 501 PetscFunctionBegin; 502 delete event_cast_(event); 503 event->data = nullptr; 504 PetscFunctionReturn(PETSC_SUCCESS); 505 }; 506 PetscFunctionReturn(PETSC_SUCCESS); 507 } 508 509 template <DeviceType T> 510 inline PetscErrorCode DeviceContext<T>::recordEvent(PetscDeviceContext dctx, PetscEvent event) noexcept 511 { 512 PetscFunctionBegin; 513 PetscCall(impls_cast_(dctx)->stream.record_event(*event_cast_(event))); 514 PetscFunctionReturn(PETSC_SUCCESS); 515 } 516 517 template <DeviceType T> 518 inline PetscErrorCode DeviceContext<T>::waitForEvent(PetscDeviceContext dctx, PetscEvent event) noexcept 519 { 520 PetscFunctionBegin; 521 PetscCall(impls_cast_(dctx)->stream.wait_for_event(*event_cast_(event))); 522 PetscFunctionReturn(PETSC_SUCCESS); 523 } 524 525 // initialize the static member variables 526 template <DeviceType T> 527 bool DeviceContext<T>::initialized_ = false; 528 529 template <DeviceType T> 530 std::array<typename DeviceContext<T>::cupmBlasHandle_t, PETSC_DEVICE_MAX_DEVICES> DeviceContext<T>::blashandles_ = {}; 531 532 template <DeviceType T> 533 std::array<typename DeviceContext<T>::cupmSolverHandle_t, PETSC_DEVICE_MAX_DEVICES> DeviceContext<T>::solverhandles_ = {}; 534 535 template <DeviceType T> 536 constexpr _DeviceContextOps DeviceContext<T>::ops; 537 538 } // namespace impl 539 540 // shorten this one up a bit (and instantiate the templates) 541 using CUPMContextCuda = impl::DeviceContext<DeviceType::CUDA>; 542 using CUPMContextHip = impl::DeviceContext<DeviceType::HIP>; 543 544 // shorthand for what is an EXTREMELY long name 545 #define PetscDeviceContext_(IMPLS) ::Petsc::device::cupm::impl::DeviceContext<::Petsc::device::cupm::DeviceType::IMPLS>::PetscDeviceContext_IMPLS 546 547 } // namespace cupm 548 549 } // namespace device 550 551 } // namespace Petsc 552 553 #endif // PETSCDEVICECONTEXTCUDA_HPP 554