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:   hipHostMalloc(result,a);
      7:   return 0;
      8: }

     10: PETSC_EXTERN PetscErrorCode PetscHIPHostFree(void *aa,int lineno,const char function[],const char filename[])
     11: {
     12:   hipHostFree(aa);
     13:   return 0;
     14: }

     16: PETSC_EXTERN PetscErrorCode PetscHIPHostRealloc(size_t a,int lineno,const char function[],const char filename[],void **result)
     17: {
     18:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"HIP has no Realloc()");
     19: }

     21: static PetscErrorCode (*PetscMallocOld)(size_t,PetscBool,int,const char[],const char[],void**);
     22: static PetscErrorCode (*PetscReallocOld)(size_t,int,const char[],const char[],void**);
     23: static PetscErrorCode (*PetscFreeOld)(void*,int,const char[],const char[]);

     25: /*@C
     26:    PetscMallocSetHIPHost - Set PetscMalloc to use HIPHostMalloc
     27:      Switch the current malloc and free routines to the HIP malloc and free routines

     29:    Not Collective

     31:    Level: developer

     33:    Notes:
     34:      This provides a way to use the HIP malloc and free routines temporarily. One
     35:      can switch back to the previous choice by calling PetscMallocResetHIPHost().

     37: .seealso: PetscMallocResetHIPHost()
     38: @*/
     39: PETSC_EXTERN PetscErrorCode PetscMallocSetHIPHost(void)
     40: {
     41:   /* Save the previous choice */
     42:   PetscMallocOld  = PetscTrMalloc;
     43:   PetscReallocOld = PetscTrRealloc;
     44:   PetscFreeOld    = PetscTrFree;
     45:   PetscTrMalloc   = PetscHIPHostMalloc;
     46:   PetscTrRealloc  = PetscHIPHostRealloc;
     47:   PetscTrFree     = PetscHIPHostFree;
     48:   return 0;
     49: }

     51: /*@C
     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:   PetscTrMalloc  = PetscMallocOld;
     63:   PetscTrRealloc = PetscReallocOld;
     64:   PetscTrFree    = PetscFreeOld;
     65:   return 0;
     66: }