Actual source code: memory.cxx

  1: #include <petsc/private/deviceimpl.h>

  3: #include <petsc/private/cpp/register_finalize.hpp>
  4: #include <petsc/private/cpp/type_traits.hpp>
  5: #include <petsc/private/cpp/unordered_map.hpp>

  7: #include <algorithm> // std::find_if
  8: #include <cstring>   // std::memset

 10: #include <petsc/private/cpp/object_pool.hpp>

 12: namespace Petsc
 13: {

 15: namespace memory
 16: {

 18: typename PoolAllocated::allocator_type PoolAllocated::pool_{};

 20: } // namespace memory

 22: } // namespace Petsc

 24: const char *const PetscDeviceCopyModes[] = {"host_to_host", "device_to_host", "host_to_device", "device_to_device", "auto", "PetscDeviceCopyMode", "PETSC_DEVICE_COPY_", nullptr};
 25: static_assert(Petsc::util::to_underlying(PETSC_DEVICE_COPY_HTOH) == 0, "");
 26: static_assert(Petsc::util::to_underlying(PETSC_DEVICE_COPY_DTOH) == 1, "");
 27: static_assert(Petsc::util::to_underlying(PETSC_DEVICE_COPY_HTOD) == 2, "");
 28: static_assert(Petsc::util::to_underlying(PETSC_DEVICE_COPY_DTOD) == 3, "");
 29: static_assert(Petsc::util::to_underlying(PETSC_DEVICE_COPY_AUTO) == 4, "");

 31: // GCC implementation for std::hash. LLVM's libc++ is almost 2x slower because they do all
 32: // kinds of complicated murmur hashing, so we make sure to enforce GCC's version.
 33: struct PointerHash {
 34:   template <typename T>
 35:   PETSC_NODISCARD std::size_t operator()(const T *ptr) const noexcept
 36:   {
 37:     return reinterpret_cast<std::size_t>(ptr);
 38:   }
 39: };

 41: // ==========================================================================================
 42: // PointerAttributes
 43: //
 44: // A set of attributes for a pointer
 45: // ==========================================================================================

 47: struct PointerAttributes {
 48:   PetscMemType  mtype = PETSC_MEMTYPE_HOST; // memtype of allocation
 49:   PetscObjectId id    = 0;                  // id of allocation
 50:   std::size_t   size  = 0;                  // size of allocation (bytes)

 52:   // even though this is a POD and can be aggregate initialized, the STL uses () constructors
 53:   // in unordered_map and so we need to provide a trivial constructor...
 54:   constexpr PointerAttributes() = default;
 55:   constexpr PointerAttributes(PetscMemType, PetscObjectId, std::size_t) noexcept;

 57:   bool operator==(const PointerAttributes &) const noexcept;

 59:   PETSC_NODISCARD bool contains(const void *, const void *) const noexcept;
 60: };

 62: // ==========================================================================================
 63: // PointerAttributes - Public API
 64: // ==========================================================================================

 66: inline constexpr PointerAttributes::PointerAttributes(PetscMemType mtype_, PetscObjectId id_, std::size_t size_) noexcept : mtype(mtype_), id(id_), size(size_) { }

 68: inline bool PointerAttributes::operator==(const PointerAttributes &other) const noexcept
 69: {
 70:   return (mtype == other.mtype) && (id == other.id) && (size == other.size);
 71: }

 73: /*
 74:   PointerAttributes::contains - asks and answers the question, does ptr_begin contain ptr

 76:   Input Parameters:
 77: + ptr_begin - pointer to the start of the range to check
 78: - ptr       - the pointer to query

 80:   Notes:
 81:   Returns true if ptr falls within ptr_begins range, false otherwise.
 82: */
 83: inline bool PointerAttributes::contains(const void *ptr_begin, const void *ptr) const noexcept
 84: {
 85:   return (ptr >= ptr_begin) && (ptr < (static_cast<const char *>(ptr_begin) + size));
 86: }

 88: // ==========================================================================================
 89: // MemoryMap
 90: //
 91: // Since the pointers allocated via PetscDeviceAllocate_Private() may be device pointers we
 92: // cannot just store meta-data within the pointer itself (as we can't dereference them). So
 93: // instead we need to keep an extra map to keep track of them
 94: //
 95: // Each entry maps pointer -> {
 96: //   PetscMemType  - The memtype of the pointer
 97: //   PetscObjectId - A unique ID assigned at allocation or registration so auto-dep can
 98: //                   identify the pointer
 99: //   size          - The size (in bytes) of the allocation
100: // }
101: // ==========================================================================================

103: class MemoryMap : public Petsc::RegisterFinalizeable<MemoryMap> {
104: public:
105:   using map_type = Petsc::UnorderedMap<void *, PointerAttributes, PointerHash>;

107:   map_type map{};

109:   PETSC_NODISCARD map_type::const_iterator search_for(const void *, bool = false) const noexcept;

111: private:
112:   friend class Petsc::RegisterFinalizeable<MemoryMap>;
113:   PetscErrorCode register_finalize_() noexcept;
114:   PetscErrorCode finalize_() noexcept;
115: };

117: // ==========================================================================================
118: // MemoryMap - Private API
119: // ==========================================================================================

121: PetscErrorCode MemoryMap::register_finalize_() noexcept
122: {
123:   PetscFunctionBegin;
124:   // Preallocate, this does give a modest performance bump since unordered_map is so __dog__
125:   // slow if it needs to rehash. Experiments show that users tend not to have more than 5 or
126:   // so concurrently live pointers lying around. 10 at most.
127:   PetscCall(map.reserve(16));
128:   PetscFunctionReturn(PETSC_SUCCESS);
129: }

131: PetscErrorCode MemoryMap::finalize_() noexcept
132: {
133:   PetscFunctionBegin;
134:   PetscCall(PetscInfo(nullptr, "Finalizing memory map\n"));
135:   PetscCallCXX(map = map_type{});
136:   PetscFunctionReturn(PETSC_SUCCESS);
137: }

139: // ==========================================================================================
140: // MemoryMap - Public API
141: // ==========================================================================================

143: /*
144:   MemoryMap::search_for - retrieve an iterator to the key-value pair for a pointer in the map

146:   Input Parameters:
147: + ptr       - pointer to search for
148: - must_find - true if an error is raised if the pointer is not found (default: false)

150:   Notes:
151:   Accounts for sub-regions, i.e. if ptr is contained within another pointers region, it returns
152:   the iterator to the super-pointers key-value pair.

154:   If ptr is not found and must_find is false returns map.end(), otherwise raises an error
155: */
156: MemoryMap::map_type::const_iterator MemoryMap::search_for(const void *ptr, bool must_find) const noexcept
157: {
158:   const auto end_it = map.end();
159:   auto       it     = map.find(const_cast<map_type::key_type>(ptr));

161:   // ptr was found, and points to an entire block
162:   PetscFunctionBegin;
163:   if (it != end_it) PetscFunctionReturn(it);
164:   // wasn't found, but maybe its part of a block. have to search every block for it
165:   it = std::find_if(map.begin(), end_it, [ptr](map_type::const_iterator::reference map_it) { return map_it.second.contains(map_it.first, ptr); });
166:   PetscCheckAbort(!must_find || it != end_it, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Pointer %p was not registered with the memory tracker, call PetscDeviceRegisterMemory() on it", ptr);
167:   PetscFunctionReturn(it);
168: }

170: static MemoryMap memory_map;

172: // ==========================================================================================
173: // Utility functions
174: // ==========================================================================================

176: static PetscErrorCode PetscDeviceCheckCapable_Private(PetscDeviceContext dctx, bool cond, const char descr[])
177: {
178:   PetscFunctionBegin;
179:   PetscCheck(cond, PETSC_COMM_SELF, PETSC_ERR_SUP, "Device context (id: %" PetscInt64_FMT ", name: %s, type: %s) can only handle %s host memory", PetscObjectCast(dctx)->id, PetscObjectCast(dctx)->name, dctx->device ? PetscDeviceTypes[dctx->device->type] : "unknown", descr);
180:   PetscFunctionReturn(PETSC_SUCCESS);
181: }

183: // A helper utility, since register is called from PetscDeviceRegisterMemory() and
184: // PetscDevicAllocate(). The latter also needs the generated id, so instead of making it search
185: // the map again we just return it here
186: static PetscErrorCode PetscDeviceRegisterMemory_Private(const void *PETSC_RESTRICT ptr, PetscMemType mtype, std::size_t size, PetscObjectId *PETSC_RESTRICT id = nullptr)
187: {
188:   auto      &map = memory_map.map;
189:   const auto it  = memory_map.search_for(ptr);

191:   PetscFunctionBegin;
192:   if (it == map.cend()) {
193:     // pointer was never registered with the map, insert it and bail
194:     const auto newid = PetscObjectNewId_Internal();

196:     if (PetscDefined(USE_DEBUG)) {
197:       const auto tmp = PointerAttributes(mtype, newid, size);

199:       for (const auto &entry : map) {
200:         auto &&attr = entry.second;

202:         // REVIEW ME: maybe this should just be handled...
203:         PetscCheck(!tmp.contains(ptr, entry.first), PETSC_COMM_SELF, PETSC_ERR_ORDER, "Trying to register pointer %p (memtype %s, size %zu) but it appears you have already registered a sub-region of it (pointer %p, memtype %s, size %zu). Must register the larger region first", ptr, PetscMemTypeToString(mtype), size,
204:                    entry.first, PetscMemTypeToString(attr.mtype), attr.size);
205:       }
206:     }
207:     // clang-format off
208:     if (id) *id = newid;
209:     PetscCallCXX(map.emplace(
210:       std::piecewise_construct,
211:       std::forward_as_tuple(const_cast<MemoryMap::map_type::key_type>(ptr)),
212:       std::forward_as_tuple(mtype, newid, size)
213:     ));
214:     // clang-format on
215:     PetscFunctionReturn(PETSC_SUCCESS);
216:   }
217:   if (PetscDefined(USE_DEBUG)) {
218:     const auto &old = it->second;

220:     PetscCheck(PointerAttributes(mtype, old.id, size) == old, PETSC_COMM_SELF, PETSC_ERR_LIB, "Pointer %p appears to have been previously allocated with memtype %s, size %zu and assigned id %" PetscInt64_FMT ", which does not match new values: (mtype %s, size %zu, id %" PetscInt64_FMT ")", it->first,
221:                PetscMemTypeToString(old.mtype), old.size, old.id, PetscMemTypeToString(mtype), size, old.id);
222:   }
223:   if (id) *id = it->second.id;
224:   PetscFunctionReturn(PETSC_SUCCESS);
225: }

227: /*@C
228:   PetscDeviceRegisterMemory - Register a pointer for use with device-aware memory system

230:   Not Collective

232:   Input Parameters:
233: + ptr   - The pointer to register
234: . mtype - The `PetscMemType` of the pointer
235: - size  - The size (in bytes) of the memory region

237:   Notes:
238:   `ptr` need not point to the beginning of the memory range, however the user should register
239:   the

241:   It's OK to re-register the same `ptr` repeatedly (subsequent registrations do nothing)
242:   however the given `mtype` and `size` must match the original registration.

244:   `size` may be 0 (in which case this routine does nothing).

246:   Level: intermediate

248: .seealso: `PetscDeviceMalloc()`, `PetscDeviceArrayCopy()`, `PetscDeviceFree()`,
249: `PetscDeviceArrayZero()`
250: @*/
251: PetscErrorCode PetscDeviceRegisterMemory(const void *PETSC_RESTRICT ptr, PetscMemType mtype, std::size_t size)
252: {
253:   PetscFunctionBegin;
254:   if (PetscMemTypeHost(mtype)) PetscAssertPointer(ptr, 1);
255:   if (PetscUnlikely(!size)) PetscFunctionReturn(PETSC_SUCCESS); // there is no point registering empty range
256:   PetscCall(PetscDeviceRegisterMemory_Private(ptr, mtype, size));
257:   PetscFunctionReturn(PETSC_SUCCESS);
258: }

260: /*
261:   PetscDeviceAllocate_Private - Allocate device-aware memory

263:   Not Collective, Asynchronous, Auto-dependency aware

265:   Input Parameters:
266: + dctx      - The `PetscDeviceContext` used to allocate the memory
267: . clear     - Whether or not the memory should be zeroed
268: . mtype     - The type of memory to allocate
269: . n         - The amount (in bytes) to allocate
270: - alignment - The alignment requirement (in bytes) of the allocated pointer

272:   Output Parameter:
273: . ptr - The pointer to store the result in

275:   Notes:
276:   The user should prefer `PetscDeviceMalloc()` over this routine as it automatically computes
277:   the size of the allocation and alignment based on the size of the datatype.

279:   If the user is unsure about `alignment` -- or unable to compute it -- passing
280:   `PETSC_MEMALIGN` will always work, though the user should beware that this may be quite
281:   wasteful for very small allocations.

283:   Memory allocated with this function must be freed with `PetscDeviceFree()` (or
284:   `PetscDeviceDeallocate_Private()`).

286:   If `n` is zero, then `ptr` is set to `PETSC_NULLPTR`.

288:   This routine falls back to using `PetscMalloc1()` or `PetscCalloc1()` (depending on the value
289:   of `clear`) if PETSc was not configured with device support. The user should note that
290:   `mtype` and `alignment` are ignored in this case, as these routines allocate only host memory
291:   aligned to `PETSC_MEMALIGN`.

293:   Note result stored `ptr` is immediately valid and the user may freely inspect or manipulate
294:   its value on function return, i.e.\:

296: .vb
297:   PetscInt *ptr;

299:   PetscDeviceAllocate_Private(dctx, PETSC_FALSE, PETSC_MEMTYPE_DEVICE, 20, alignof(PetscInt), (void**)&ptr);

301:   PetscInt *sub_ptr = ptr + 10; // OK, no need to synchronize

303:   ptr[0] = 10; // ERROR, directly accessing contents of ptr is undefined until synchronization
304: .ve

306:   DAG representation:
307: .vb
308:   time ->

310:   -> dctx - |= CALL =| -\- dctx -->
311:                          \- ptr ->
312: .ve

314:   Level: intermediate

316: .N ASYNC_API

318: .seealso: `PetscDeviceMalloc()`, `PetscDeviceFree()`, `PetscDeviceDeallocate_Private()`,
319: `PetscDeviceArrayCopy()`, `PetscDeviceArrayZero()`, `PetscMemType`
320: */
321: PetscErrorCode PetscDeviceAllocate_Private(PetscDeviceContext dctx, PetscBool clear, PetscMemType mtype, std::size_t n, std::size_t alignment, void **PETSC_RESTRICT ptr)
322: {
323:   PetscObjectId id = 0;

325:   PetscFunctionBegin;
326:   if (PetscDefined(USE_DEBUG)) {
327:     const auto is_power_of_2 = [](std::size_t num) { return (num & (num - 1)) == 0; };

329:     PetscCheck(alignment != 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Requested alignment %zu cannot be 0", alignment);
330:     PetscCheck(is_power_of_2(alignment), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Requested alignment %zu must be a power of 2", alignment);
331:   }
332:   PetscAssertPointer(ptr, 6);
333:   *ptr = nullptr;
334:   if (PetscUnlikely(!n)) PetscFunctionReturn(PETSC_SUCCESS);
335:   PetscCall(memory_map.register_finalize());
336:   PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));

338:   // get our pointer here
339:   if (dctx->ops->memalloc) {
340:     PetscUseTypeMethod(dctx, memalloc, clear, mtype, n, alignment, ptr);
341:   } else {
342:     PetscCall(PetscDeviceCheckCapable_Private(dctx, PetscMemTypeHost(mtype), "allocating"));
343:     PetscCall(PetscMallocA(1, clear, __LINE__, PETSC_FUNCTION_NAME, __FILE__, n, ptr));
344:   }
345:   PetscCall(PetscDeviceRegisterMemory_Private(*ptr, mtype, n, &id));
346:   // Note this is a "write" so that the next dctx to try and read from the pointer has to wait
347:   // for the allocation to be ready
348:   PetscCall(PetscDeviceContextMarkIntentFromID(dctx, id, PETSC_MEMORY_ACCESS_WRITE, "memory allocation"));
349:   PetscFunctionReturn(PETSC_SUCCESS);
350: }

352: /*
353:   PetscDeviceDeallocate_Private - Free device-aware memory

355:   Not Collective, Asynchronous, Auto-dependency aware

357:   Input Parameters:
358: + dctx  - The `PetscDeviceContext` used to free the memory
359: - ptr   - The pointer to free

361:   Level: intermediate

363:   Notes:
364:   `ptr` must have been allocated using any of `PetscDeviceMalloc()`, `PetscDeviceCalloc()` or
365:   `PetscDeviceAllocate_Private()`, or registered with the system via `PetscDeviceRegisterMemory()`.

367:   The user should prefer `PetscDeviceFree()` over this routine as it automatically sets `ptr`
368:   to `PETSC_NULLPTR` on successful deallocation.

370:   `ptr` may be `NULL`.

372:   This routine falls back to using `PetscFree()` if PETSc was not configured with device
373:   support. The user should note that `PetscFree()` frees only host memory.

375:   DAG representation:
376: .vb
377:   time ->

379:   -> dctx -/- |= CALL =| - dctx ->
380:   -> ptr -/
381: .ve

383: .N ASYNC_API

385: .seealso: `PetscDeviceFree()`, `PetscDeviceAllocate_Private()`
386: */
387: PetscErrorCode PetscDeviceDeallocate_Private(PetscDeviceContext dctx, void *PETSC_RESTRICT ptr)
388: {
389:   PetscFunctionBegin;
390:   if (ptr) {
391:     auto      &map      = memory_map.map;
392:     const auto found_it = map.find(const_cast<MemoryMap::map_type::key_type>(ptr));

394:     if (PetscUnlikelyDebug(found_it == map.end())) {
395:       // OK this is a bad pointer, now determine why
396:       const auto it = memory_map.search_for(ptr);

398:       // if it is map.cend() then no allocation owns it, meaning it was not allocated by us!
399:       PetscCheck(it != map.cend(), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Pointer %p was not allocated via PetscDeviceAllocate_Private()", ptr);
400:       // if we are here then we did allocate it but the user has tried to do something along
401:       // the lines of:
402:       //
403:       // allocate(&ptr, size);
404:       // deallocate(ptr+5);
405:       //
406:       auto &&attr = it->second;
407:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Attempting to deallocate pointer %p which is a suballocation of %p (memtype %s, id %" PetscInt64_FMT ", size %zu bytes)", ptr, it->first, PetscMemTypeToString(attr.mtype), attr.id, attr.size);
408:     }
409:     auto &&attr = found_it->second;
410:     PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
411:     // mark intent BEFORE we free, note we mark as write so that we are made to wait on any
412:     // outstanding reads (don't want to kill the pointer before they are done)
413:     PetscCall(PetscDeviceContextMarkIntentFromID(dctx, attr.id, PETSC_MEMORY_ACCESS_WRITE, "memory deallocation"));
414:     // do free
415:     if (dctx->ops->memfree) {
416:       PetscUseTypeMethod(dctx, memfree, attr.mtype, (void **)&ptr);
417:     } else {
418:       PetscCall(PetscDeviceCheckCapable_Private(dctx, PetscMemTypeHost(attr.mtype), "freeing"));
419:     }
420:     // if ptr still exists, then the device context could not handle it
421:     if (ptr) PetscCall(PetscFree(ptr));
422:     PetscCallCXX(map.erase(found_it));
423:   }
424:   PetscFunctionReturn(PETSC_SUCCESS);
425: }

427: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
428: /*@C
429:   PetscDeviceMemcpy - Copy memory in a device-aware manner

431:   Not Collective, Asynchronous, Auto-dependency aware

433:   Input Parameters:
434: + dctx - The `PetscDeviceContext` used to copy the memory
435: . dest - The pointer to copy to
436: . src  - The pointer to copy from
437: - n    - The amount (in bytes) to copy

439:   Level: intermediate

441:   Notes:
442:   Both `dest` and `src` must have been allocated by `PetscDeviceMalloc()` or
443:   `PetscDeviceCalloc()`.

445:   `src` and `dest` cannot overlap.

447:   If both `src` and `dest` are on the host this routine is fully synchronous.

449:   The user should prefer `PetscDeviceArrayCopy()` over this routine as it automatically
450:   computes the number of bytes to copy from the size of the pointer types.

452:   DAG representation:
453: .vb
454:   time ->

456:   -> dctx - |= CALL =| - dctx ->
457:   -> dest --------------------->
458:   -> src ---------------------->
459: .ve

461: .N ASYNC_API

463: .seealso: `PetscDeviceArrayCopy()`, `PetscDeviceMalloc()`, `PetscDeviceCalloc()`,
464: `PetscDeviceFree()`
465: @*/
466: PetscErrorCode PetscDeviceMemcpy(PetscDeviceContext dctx, void *PETSC_RESTRICT dest, const void *PETSC_RESTRICT src, std::size_t n)
467: {
468:   PetscFunctionBegin;
469:   if (!n) PetscFunctionReturn(PETSC_SUCCESS);
470:   PetscCheck(dest, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Trying to copy to a NULL pointer");
471:   PetscCheck(src, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Trying to copy from a NULL pointer");
472:   if (dest == src) PetscFunctionReturn(PETSC_SUCCESS);
473:   PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
474:   {
475:     const auto &dest_attr = memory_map.search_for(dest, true)->second;
476:     const auto &src_attr  = memory_map.search_for(src, true)->second;
477:     const auto  mode      = PetscMemTypeToDeviceCopyMode(dest_attr.mtype, src_attr.mtype);

479:     PetscCall(PetscDeviceContextMarkIntentFromID(dctx, src_attr.id, PETSC_MEMORY_ACCESS_READ, "memory copy (src)"));
480:     PetscCall(PetscDeviceContextMarkIntentFromID(dctx, dest_attr.id, PETSC_MEMORY_ACCESS_WRITE, "memory copy (dest)"));
481:     // perform the copy
482:     if (dctx->ops->memcopy) {
483:       PetscUseTypeMethod(dctx, memcopy, dest, src, n, mode);
484:       if (mode == PETSC_DEVICE_COPY_HTOD) {
485:         PetscCall(PetscLogCpuToGpu(n));
486:       } else if (mode == PETSC_DEVICE_COPY_DTOH) {
487:         PetscCall(PetscLogGpuToCpu(n));
488:       }
489:     } else {
490:       // REVIEW ME: we might potentially need to sync here if the memory is device-allocated
491:       // (pinned) but being copied by a host dctx
492:       PetscCall(PetscDeviceCheckCapable_Private(dctx, mode == PETSC_DEVICE_COPY_HTOH, "copying"));
493:       PetscCall(PetscMemcpy(dest, src, n));
494:     }
495:   }
496:   PetscFunctionReturn(PETSC_SUCCESS);
497: }

499: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
500: /*@C
501:   PetscDeviceMemset - Memset device-aware memory

503:   Not Collective, Asynchronous, Auto-dependency aware

505:   Input Parameters:
506: + dctx - The `PetscDeviceContext` used to memset the memory
507: . ptr  - The pointer to the memory
508: . v    - The value to set
509: - n    - The amount (in bytes) to set

511:   Level: intermediate

513:   Notes:
514:   `ptr` must have been allocated by `PetscDeviceMalloc()` or `PetscDeviceCalloc()`.

516:   The user should prefer `PetscDeviceArrayZero()` over this routine as it automatically
517:   computes the number of bytes to copy from the size of the pointer types, though they should
518:   note that it only zeros memory.

520:   This routine is analogous to `memset()`. That is, this routine copies the value
521:   `static_cast<unsigned char>(v)` into each of the first count characters of the object pointed
522:   to by `dest`.

524:   If `dest` is on device, this routine is asynchronous.

526:   DAG representation:
527: .vb
528:   time ->

530:   -> dctx - |= CALL =| - dctx ->
531:   -> dest --------------------->
532: .ve

534: .N ASYNC_API

536: .seealso: `PetscDeviceArrayZero()`, `PetscDeviceMalloc()`, `PetscDeviceCalloc()`,
537: `PetscDeviceFree()`
538: @*/
539: PetscErrorCode PetscDeviceMemset(PetscDeviceContext dctx, void *ptr, PetscInt v, std::size_t n)
540: {
541:   PetscFunctionBegin;
542:   if (PetscUnlikely(!n)) PetscFunctionReturn(PETSC_SUCCESS);
543:   PetscCheck(ptr, PETSC_COMM_SELF, PETSC_ERR_POINTER, "Trying to memset a NULL pointer");
544:   PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx));
545:   {
546:     const auto &attr = memory_map.search_for(ptr, true)->second;

548:     PetscCall(PetscDeviceContextMarkIntentFromID(dctx, attr.id, PETSC_MEMORY_ACCESS_WRITE, "memory set"));
549:     if (dctx->ops->memset) {
550:       PetscUseTypeMethod(dctx, memset, attr.mtype, ptr, v, n);
551:     } else {
552:       // REVIEW ME: we might potentially need to sync here if the memory is device-allocated
553:       // (pinned) but being memset by a host dctx
554:       PetscCall(PetscDeviceCheckCapable_Private(dctx, PetscMemTypeHost(attr.mtype), "memsetting"));
555:       std::memset(ptr, static_cast<int>(v), n);
556:     }
557:   }
558:   PetscFunctionReturn(PETSC_SUCCESS);
559: }