Actual source code: mhiphost.hip.cxx
1: #include <petscdevice_hip.h>
3: PETSC_EXTERN PetscErrorCode PetscHIPHostMalloc(size_t a, PetscBool clear, int lineno, const char function[], const char filename[], void **result)
4: {
5: PetscCallHIP(hipHostMalloc(result, a));
6: return PETSC_SUCCESS;
7: }
9: PETSC_EXTERN PetscErrorCode PetscHIPHostFree(void *aa, int lineno, const char function[], const char filename[])
10: {
11: PetscCallHIP(hipHostFree(aa));
12: return PETSC_SUCCESS;
13: }
15: PETSC_EXTERN PetscErrorCode PetscHIPHostRealloc(size_t a, int lineno, const char function[], const char filename[], void **result)
16: {
17: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_MEM, "HIP has no Realloc()");
18: }
20: static PetscErrorCode (*PetscMallocOld)(size_t, PetscBool, int, const char[], const char[], void **);
21: static PetscErrorCode (*PetscReallocOld)(size_t, int, const char[], const char[], void **);
22: static PetscErrorCode (*PetscFreeOld)(void *, int, const char[], const char[]);
24: /*@
25: PetscMallocSetHIPHost - Set `PetscMalloc()` to use `HIPHostMalloc()`
26: Switch the current malloc and free routines to the HIP malloc and free routines
28: Not Collective
30: Level: developer
32: Note:
33: This provides a way to use the HIP malloc and free routines temporarily. One
34: can switch back to the previous choice by calling `PetscMallocResetHIPHost()`.
36: .seealso: `PetscMallocSetCUDAHost()`, `PetscMallocResetHIPHost()`
37: @*/
38: PETSC_EXTERN PetscErrorCode PetscMallocSetHIPHost(void)
39: {
40: PetscFunctionBegin;
41: /* Save the previous choice */
42: PetscMallocOld = PetscTrMalloc;
43: PetscReallocOld = PetscTrRealloc;
44: PetscFreeOld = PetscTrFree;
45: PetscTrMalloc = PetscHIPHostMalloc;
46: PetscTrRealloc = PetscHIPHostRealloc;
47: PetscTrFree = PetscHIPHostFree;
48: PetscFunctionReturn(PETSC_SUCCESS);
49: }
51: /*@
52: PetscMallocResetHIPHost - Reset the changes made by `PetscMallocSetHIPHost()`
54: Not Collective
56: Level: developer
58: .seealso: `PetscMallocSetHIPHost()`
59: @*/
60: PETSC_EXTERN PetscErrorCode PetscMallocResetHIPHost(void)
61: {
62: PetscFunctionBegin;
63: PetscTrMalloc = PetscMallocOld;
64: PetscTrRealloc = PetscReallocOld;
65: PetscTrFree = PetscFreeOld;
66: PetscFunctionReturn(PETSC_SUCCESS);
67: }