Actual source code: mcudahost.cu

  1: #include <petscsys.h>
  2: #include <petscdevice.h>

  4: static PetscErrorCode PetscCUDAHostMalloc(size_t a,PetscBool clear,int lineno,const char function[],const char filename[],void **result)
  5: {
  6:   cudaMallocHost(result,a);
  7:   return 0;
  8: }

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

 16: static PetscErrorCode PetscCUDAHostRealloc(size_t a,int lineno,const char function[],const char filename[],void **result)
 17: {
 18:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"CUDA 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:    PetscMallocSetCUDAHost - Set PetscMalloc to use CUDAHostMalloc
 27:      Switch the current malloc and free routines to the CUDA malloc and free routines

 29:    Not Collective

 31:    Level: developer

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

 37: .seealso: PetscMallocResetCUDAHost()
 38: @*/
 39: PetscErrorCode PetscMallocSetCUDAHost(void)
 40: {
 41:   /* Save the previous choice */
 42:   PetscMallocOld  = PetscTrMalloc;
 43:   PetscReallocOld = PetscTrRealloc;
 44:   PetscFreeOld    = PetscTrFree;
 45:   PetscTrMalloc   = PetscCUDAHostMalloc;
 46:   PetscTrRealloc  = PetscCUDAHostRealloc;
 47:   PetscTrFree     = PetscCUDAHostFree;
 48:   return 0;
 49: }

 51: /*@C
 52:    PetscMallocResetCUDAHost - Reset the changes made by PetscMallocSetCUDAHost

 54:    Not Collective

 56:    Level: developer

 58: .seealso: PetscMallocSetCUDAHost()
 59: @*/
 60: PetscErrorCode PetscMallocResetCUDAHost(void)
 61: {
 62:   PetscTrMalloc  = PetscMallocOld;
 63:   PetscTrRealloc = PetscReallocOld;
 64:   PetscTrFree    = PetscFreeOld;
 65:   return 0;
 66: }