Actual source code: mhiphost.hip.cpp
1: #include <petscsys.h>
2: #include <petscdevice.h>
4: PETSC_EXTERN PetscErrorCode PetscHIPHostMalloc(size_t a,PetscBool clear,int lineno,const char function[],const char filename[],void **result)
5: {
6: hipError_t ierr;
7: hipHostMalloc(result,a);CHKERRHIP(ierr);
8: return 0;
9: }
11: PETSC_EXTERN PetscErrorCode PetscHIPHostFree(void *aa,int lineno,const char function[],const char filename[])
12: {
13: hipError_t ierr;
14: hipHostFree(aa);CHKERRHIP(ierr);
15: return 0;
16: }
18: PETSC_EXTERN PetscErrorCode PetscHIPHostRealloc(size_t a,int lineno,const char function[],const char filename[],void **result)
19: {
20: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"HIP has no Realloc()");
21: }
23: static PetscErrorCode (*PetscMallocOld)(size_t,PetscBool,int,const char[],const char[],void**);
24: static PetscErrorCode (*PetscReallocOld)(size_t,int,const char[],const char[],void**);
25: static PetscErrorCode (*PetscFreeOld)(void*,int,const char[],const char[]);
27: /*@C
28: PetscMallocSetHIPHost - Set PetscMalloc to use HIPHostMalloc
29: Switch the current malloc and free routines to the HIP malloc and free routines
31: Not Collective
33: Level: developer
35: Notes:
36: This provides a way to use the HIP malloc and free routines temporarily. One
37: can switch back to the previous choice by calling PetscMallocResetHIPHost().
39: .seealso: PetscMallocResetHIPHost()
40: @*/
41: PETSC_EXTERN PetscErrorCode PetscMallocSetHIPHost(void)
42: {
44: /* Save the previous choice */
45: PetscMallocOld = PetscTrMalloc;
46: PetscReallocOld = PetscTrRealloc;
47: PetscFreeOld = PetscTrFree;
48: PetscTrMalloc = PetscHIPHostMalloc;
49: PetscTrRealloc = PetscHIPHostRealloc;
50: PetscTrFree = PetscHIPHostFree;
51: return(0);
52: }
54: /*@C
55: PetscMallocResetHIPHost - Reset the changes made by PetscMallocSetHIPHost
57: Not Collective
59: Level: developer
61: .seealso: PetscMallocSetHIPHost()
62: @*/
63: PETSC_EXTERN PetscErrorCode PetscMallocResetHIPHost(void)
64: {
66: PetscTrMalloc = PetscMallocOld;
67: PetscTrRealloc = PetscReallocOld;
68: PetscTrFree = PetscFreeOld;
69: return(0);
70: }