Actual source code: memory.c
1: #include <petsc/private/deviceimpl.h>
2: #include <petscdevice_cupm.h>
4: PetscErrorCode PetscDeviceGetAllocationAlignment_Private(size_t requested, size_t type_alignment, PetscBool host_fallback, size_t *alignment)
5: {
6: PetscFunctionBegin;
7: PetscAssertPointer(alignment, 4);
8: PetscCheck(requested > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Alignment must be PETSC_DECIDE or a positive power of two, got %zu", requested);
9: PetscCheck(requested == (size_t)PETSC_DECIDE || !(requested & (requested - 1)), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Alignment %zu must be a power of two", requested);
10: PetscCheck(type_alignment && !(type_alignment & (type_alignment - 1)), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Type alignment %zu must be a positive power of two", type_alignment);
11: if (host_fallback) {
12: PetscCheck(requested == (size_t)PETSC_DECIDE || !(PETSC_MEMALIGN % requested), PETSC_COMM_SELF, PETSC_ERR_SUP, "Host allocation fallback guarantees %d-byte alignment; requested %zu-byte alignment is not supported", PETSC_MEMALIGN, requested);
13: *alignment = PETSC_MEMALIGN;
14: } else *alignment = requested == (size_t)PETSC_DECIDE ? type_alignment : PetscMax(requested, type_alignment);
15: PetscFunctionReturn(PETSC_SUCCESS);
16: }
18: // REVIEW ME: this should probably return PETSC_MEMTYPE_CUDA and PETSC_MEMTYPE_HIP
20: /*@
21: PetscGetMemType - Query the `PetscMemType` of a pointer
23: Not Collective, No Fortran Support
25: Input Parameter:
26: . ptr - The pointer to query (may be `NULL`)
28: Output Parameter:
29: . type - The `PetscMemType` of the pointer
31: Level: beginner
33: Notes:
34: Currently only CUDA and HIP memtypes are supported.
36: The CUDA and HIP calls needed to determine the `PetscMemType` take a non-trivial amount of time, thus for optimal GPU performance this
37: routine should be used sparingly and instead the code should track the `PetscMemType` for its important arrays.
39: .seealso: `PetscMemType`, `PetscDeviceMalloc()`, `PetscDeviceCalloc()`, `PetscDeviceFree()`,
40: `PetscDeviceArrayCopy()`, `PetscDeviceArrayZero()`
41: @*/
42: PetscErrorCode PetscGetMemType(const void *ptr, PetscMemType *type)
43: {
44: PetscFunctionBegin;
45: PetscAssertPointer(type, 2);
46: *type = PETSC_MEMTYPE_HOST;
47: if (!ptr) PetscFunctionReturn(PETSC_SUCCESS);
48: #if PetscDefined(HAVE_CUDA)
49: if (PetscDeviceInitialized(PETSC_DEVICE_CUDA)) {
50: cudaError_t cerr;
51: struct cudaPointerAttributes attr;
52: enum cudaMemoryType mtype;
53: cerr = cudaPointerGetAttributes(&attr, ptr);
54: if (cerr) cerr = cudaGetLastError(); /* If there was an error, return it and then reset it */
55: mtype = attr.type;
56: // CUDA managed memory is device accessible, so we treat it as device memory?
57: if (cerr == cudaSuccess && (mtype == cudaMemoryTypeDevice || mtype == cudaMemoryTypeManaged)) *type = PETSC_MEMTYPE_DEVICE;
58: PetscFunctionReturn(PETSC_SUCCESS);
59: }
60: #endif
62: #if PetscDefined(HAVE_HIP)
63: if (PetscDeviceInitialized(PETSC_DEVICE_HIP)) {
64: hipError_t cerr;
65: struct hipPointerAttribute_t attr;
66: enum hipMemoryType mtype;
67: cerr = hipPointerGetAttributes(&attr, ptr);
68: if (cerr) cerr = hipGetLastError();
69: #if PETSC_PKG_HIP_VERSION_GE(5, 5, 0)
70: mtype = attr.type;
71: #else
72: mtype = attr.memoryType;
73: #endif
74: if (cerr == hipSuccess && (mtype == hipMemoryTypeDevice || mtype == hipMemoryTypeManaged)) *type = PETSC_MEMTYPE_DEVICE;
75: }
76: #endif
77: PetscFunctionReturn(PETSC_SUCCESS);
78: }