1 #include <petscsys.h> /*I "petscsys.h" I*/ 2 #include <petsccublas.h> /* Needed to provide CHKERRCUDA() */ 3 4 static PetscErrorCode PetscCudaHostMalloc(size_t a,PetscBool clear,int lineno,const char function[],const char filename[],void **result) 5 { 6 cudaError_t ierr; 7 ierr = cudaMallocHost(result,a);CHKERRCUDA(ierr); 8 return 0; 9 } 10 11 static PetscErrorCode PetscCudaHostFree(void *aa,int lineno,const char function[],const char filename[]) 12 { 13 cudaError_t ierr; 14 ierr = cudaFreeHost(aa);CHKERRCUDA(ierr); 15 return 0; 16 } 17 18 static PetscErrorCode PetscCudaHostRealloc(size_t a,int lineno,const char function[],const char filename[],void **result) 19 { 20 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"CUDA has no Realloc()"); 21 return 0; 22 } 23 24 static PetscErrorCode (*PetscMallocOld)(size_t,PetscBool,int,const char[],const char[],void**); 25 static PetscErrorCode (*PetscFreeOld)(void*,int,const char[],const char[]); 26 27 /*@C 28 PetscMallocSetCUDAHost - Set PetscMalloc to use CUDAHostMalloc 29 Switch the current malloc and free routines to the CUDA malloc and free routines 30 31 Not Collective 32 33 Level: developer 34 35 Notes: 36 This provides a way to use the CUDA malloc and free routines temporarily. One 37 can switch back to the previous choice by calling PetscMallocResetCUDAHost(). 38 39 .seealso: PetscMallocResetCUDAHost() 40 @*/ 41 PetscErrorCode PetscMallocSetCUDAHost(void) 42 { 43 PetscFunctionBegin; 44 /* Save the previous choice */ 45 PetscMallocOld = PetscTrMalloc; 46 PetscFreeOld = PetscTrFree; 47 PetscTrMalloc = PetscCudaHostMalloc; 48 PetscTrFree = PetscCudaHostFree; 49 PetscFunctionReturn(0); 50 } 51 52 /*@C 53 PetscMallocResetCUDAHost - Reset the changes made by PetscMallocSetCUDAHost 54 55 Not Collective 56 57 Level: developer 58 59 .seealso: PetscMallocSetCUDAHost() 60 @*/ 61 PetscErrorCode PetscMallocResetCUDAHost(void) 62 { 63 PetscFunctionBegin; 64 PetscTrMalloc = PetscMallocOld; 65 PetscTrFree = PetscFreeOld; 66 PetscFunctionReturn(0); 67 } 68