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) PetscUseTypeMethod(dctx, memfree, attr.mtype, (void **)&ptr);
    416:     else PetscCall(PetscDeviceCheckCapable_Private(dctx, PetscMemTypeHost(attr.mtype), "freeing"));
    417:     // if ptr still exists, then the device context could not handle it
    418:     PetscCall(PetscFree(ptr));
    419:     PetscCallCXX(map.erase(found_it));
    420:   }
    421:   PetscFunctionReturn(PETSC_SUCCESS);
    422: }

    424: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
    425: /*@C
    426:   PetscDeviceMemcpy - Copy memory in a device-aware manner

    428:   Not Collective, Asynchronous, Auto-dependency aware

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

    436:   Level: intermediate

    438:   Notes:
    439:   Both `dest` and `src` must have been allocated by `PetscDeviceMalloc()` or
    440:   `PetscDeviceCalloc()`.

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

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

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

    449:   DAG representation:
    450: .vb
    451:   time ->

    453:   -> dctx - |= CALL =| - dctx ->
    454:   -> dest --------------------->
    455:   -> src ---------------------->
    456: .ve

    458: .N ASYNC_API

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

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

    493: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
    494: /*@C
    495:   PetscDeviceMemset - Memset device-aware memory

    497:   Not Collective, Asynchronous, Auto-dependency aware

    499:   Input Parameters:
    500: + dctx - The `PetscDeviceContext` used to memset the memory
    501: . ptr  - The pointer to the memory
    502: . v    - The value to set
    503: - n    - The amount (in bytes) to set

    505:   Level: intermediate

    507:   Notes:
    508:   `ptr` must have been allocated by `PetscDeviceMalloc()` or `PetscDeviceCalloc()`.

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

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

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

    520:   DAG representation:
    521: .vb
    522:   time ->

    524:   -> dctx - |= CALL =| - dctx ->
    525:   -> dest --------------------->
    526: .ve

    528: .N ASYNC_API

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

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