PetscDeviceMalloc#
Allocate device-aware memory
Synopsis#
#include <petscdevice.h>
PetscErrorCode PetscDeviceMalloc(PetscDeviceContext dctx, PetscMemType mtype, size_t n, size_t alignment, Type **ptr)
Not Collective, Asynchronous, Auto-dependency aware
Input Parameters#
dctx - The
PetscDeviceContextused to allocate the memory, orPetscDeviceContextDefaultmtype - The type of memory to allocate
n - The amount (in elements) to allocate
alignment - The requested alignment in bytes, a positive power of two or
PETSC_DECIDE
Output Parameter#
ptr - The pointer to store the result in
Notes#
Memory allocated with this function must be freed with PetscDeviceFree().
If n is zero, then ptr is set to PETSC_NULLPTR.
CUDA and HIP contexts allocate PETSC_MEMTYPE_HOST memory from a pinned host pool and
PETSC_MEMTYPE_DEVICE memory from their device pool. There is no separate pinned host
memory type. The supplied context selects the backend. Obtain the current context with
PetscDeviceContextGetCurrentContext() to use the same context as high-level PETSc operations.
If the context has no memory allocation operation, only PETSC_MEMTYPE_HOST is supported
and allocation uses PetscMalloc1(). In the C device-interface fallback, dctx and mtype
are ignored and allocation always uses PetscMalloc1(). Backend allocation failures do
not trigger a retry with host allocation.
For CUDA and HIP pools, PETSC_DECIDE uses the alignment inferred for the pointed-to type
in the calling translation unit. When type inference is unavailable, PETSc uses the alignment
of max_align_t or PETSC_MEMALIGN. An explicit alignment is honored, or increased to the
inferred alignment if necessary. This applies to both pinned host and device memory.
The alignment inferred from a C complex type may be smaller than that required by the
corresponding CUDA or HIP complex type. If the memory will be accessed using the CUDA
or HIP type, pass that type’s required alignment as alignment.
The PetscMalloc1() fallback guarantees PETSC_MEMALIGN alignment. An explicit request
that does not divide PETSC_MEMALIGN produces PETSC_ERR_SUP; PETSC_DECIDE retains the
ordinary host allocation behavior. A zero element count returns NULL without allocating.
Alignment of an allocation does not guarantee the same alignment for interior pointers. For structures shared with device code, the element layout and stride must also agree.
Allocation, initialization, and pool reuse are ordered on dctx, including for pinned host
memory. Contexts with PETSC_STREAM_DEFAULT_WITH_BARRIER or PETSC_STREAM_NONBLOCKING_WITH_BARRIER
synchronize before returning. For other stream types, synchronize dctx before accessing the
contents from the CPU. The alignment argument does not change this synchronization policy.
This routine uses the sizeof() of the memory type requested to determine the total memory
to be allocated, therefore you should not multiply the number of elements requested by the
sizeof() the type:
PetscInt *arr;
// correct
PetscDeviceMalloc(dctx, PETSC_MEMTYPE_DEVICE, n, PETSC_DECIDE, &arr);
// incorrect
PetscDeviceMalloc(dctx, PETSC_MEMTYPE_DEVICE, n * sizeof(*arr), PETSC_DECIDE, &arr);
Note result stored ptr is immediately valid and the user may freely inspect or manipulate
its value on function return, i.e.:
PetscInt *ptr;
PetscDeviceMalloc(dctx, PETSC_MEMTYPE_DEVICE, 20, PETSC_DECIDE, &ptr);
PetscInt *sub_ptr = ptr + 10; // OK, no need to synchronize
ptr[0] = 10; // ERROR, directly accessing contents of ptr is undefined until synchronization
DAG representation#
time ->
-> dctx - |= CALL =| -\- dctx -->
\- ptr ->
Asynchronous API Notes#
This routine is explicitly marked as exhibiting asynchronous behavior. Asynchronous
behavior implies that routines launching operations on (or associated with) a
PetscDeviceContext may return to the caller before the operation has completed.
Sequential Consistency:
Operations using the same PetscDeviceContext which access objects or memory regions
are ordered per the language specification.
Operations using separate PetscDeviceContexts which access the same object or
memory region are strongly write-ordered. That is, the following operations:
write-writewrite-readread-write
are strongly ordered. Formally:
Given an operation A-B (e.g. A = write, B = read) on an object or memory
region M such that A “happens-before” B, where A uses PetscDeviceContext X
and B uses PetscDeviceContext Y, then B shall not begin before A
completes. This implies that any side-effects resulting from A are also observed by
B.
Note the omission of read-read; there is no implied ordering between separate
PetscDeviceContexts for consecutive reads.
Operations using separate PetscDeviceContexts which access separate objects or
memory regions may execute in an arbitrary order and offer no guarantee of sequential
consistency.
Memory Consistency:
If this routine modifies the participating object(s) then – unless otherwise stated –
the contents of any externally held references to internal data structures should be
considered to be in an undefined state. A well-defined state can only be restored by
re-acquiring these references through the appropriate API or by calling
PetscDeviceContextSynchronize().
Unless otherwise stated, exceptions to this rule are:
References returned by the routine itself. If a routine returns a pointer, the value of the top-most pointer is guaranteed to always be valid. For example, given a routine which asynchronously allocates memory and returns a pointer to the memory, the value of said pointer is immediately valid but dereferencing the pointer may not be.
References to structures. If a routine returns a
PetscFoo, or array thereof then the objects themselves are always valid (though their member variablesPetscFoo->datamay not be).
See Also#
PetscDeviceFree(), PetscDeviceCalloc(), PetscDeviceArrayCopy(),
PetscDeviceArrayZero()
Level#
beginner
Location#
Examples#
src/sys/objects/device/tests/ex13.cxx
src/sys/objects/device/tests/ex7.c
src/sys/objects/device/tests/ex9.c
src/mat/tests/ex254.c
src/mat/tests/ex324.c
src/vec/is/sf/tests/ex23.c
src/vec/is/sf/tests/ex26.c
src/vec/vec/tests/ex69.c
Index of all Device routines
Table of Contents for all manual pages
Index of all manual pages