12a01fb1fSRichard Tran Mills #include <petscsys.h> /*I "petscsys.h" I*/ 29566063dSJacob Faibussowitsch #include <petscdevice.h> /* Needed to provide PetscCallCUDA() */ 32a01fb1fSRichard Tran Mills 4*9371c9d4SSatish Balay static PetscErrorCode PetscCUDAHostMalloc(size_t a, PetscBool clear, int lineno, const char function[], const char filename[], void **result) { 59566063dSJacob Faibussowitsch PetscCallCUDA(cudaMallocHost(result, a)); 62a01fb1fSRichard Tran Mills return 0; 72a01fb1fSRichard Tran Mills } 82a01fb1fSRichard Tran Mills 9*9371c9d4SSatish Balay static PetscErrorCode PetscCUDAHostFree(void *aa, int lineno, const char function[], const char filename[]) { 109566063dSJacob Faibussowitsch PetscCallCUDA(cudaFreeHost(aa)); 112a01fb1fSRichard Tran Mills return 0; 122a01fb1fSRichard Tran Mills } 132a01fb1fSRichard Tran Mills 14*9371c9d4SSatish Balay static PetscErrorCode PetscCUDAHostRealloc(size_t a, int lineno, const char function[], const char filename[], void **result) { 152a01fb1fSRichard Tran Mills SETERRQ(PETSC_COMM_SELF, PETSC_ERR_MEM, "CUDA has no Realloc()"); 162a01fb1fSRichard Tran Mills } 172a01fb1fSRichard Tran Mills 182a01fb1fSRichard Tran Mills static PetscErrorCode (*PetscMallocOld)(size_t, PetscBool, int, const char[], const char[], void **); 1942e673e7SRichard Tran Mills static PetscErrorCode (*PetscReallocOld)(size_t, int, const char[], const char[], void **); 202a01fb1fSRichard Tran Mills static PetscErrorCode (*PetscFreeOld)(void *, int, const char[], const char[]); 212a01fb1fSRichard Tran Mills 222a01fb1fSRichard Tran Mills /*@C 232a01fb1fSRichard Tran Mills PetscMallocSetCUDAHost - Set PetscMalloc to use CUDAHostMalloc 242a01fb1fSRichard Tran Mills Switch the current malloc and free routines to the CUDA malloc and free routines 252a01fb1fSRichard Tran Mills 262a01fb1fSRichard Tran Mills Not Collective 272a01fb1fSRichard Tran Mills 282a01fb1fSRichard Tran Mills Level: developer 292a01fb1fSRichard Tran Mills 302a01fb1fSRichard Tran Mills Notes: 312a01fb1fSRichard Tran Mills This provides a way to use the CUDA malloc and free routines temporarily. One 322a01fb1fSRichard Tran Mills can switch back to the previous choice by calling PetscMallocResetCUDAHost(). 332a01fb1fSRichard Tran Mills 34db781477SPatrick Sanan .seealso: `PetscMallocResetCUDAHost()` 352a01fb1fSRichard Tran Mills @*/ 36*9371c9d4SSatish Balay PetscErrorCode PetscMallocSetCUDAHost(void) { 372a01fb1fSRichard Tran Mills PetscFunctionBegin; 382a01fb1fSRichard Tran Mills /* Save the previous choice */ 392a01fb1fSRichard Tran Mills PetscMallocOld = PetscTrMalloc; 4042e673e7SRichard Tran Mills PetscReallocOld = PetscTrRealloc; 412a01fb1fSRichard Tran Mills PetscFreeOld = PetscTrFree; 4247e6383dSRichard Tran Mills PetscTrMalloc = PetscCUDAHostMalloc; 4347e6383dSRichard Tran Mills PetscTrRealloc = PetscCUDAHostRealloc; 4447e6383dSRichard Tran Mills PetscTrFree = PetscCUDAHostFree; 452a01fb1fSRichard Tran Mills PetscFunctionReturn(0); 462a01fb1fSRichard Tran Mills } 472a01fb1fSRichard Tran Mills 482a01fb1fSRichard Tran Mills /*@C 492a01fb1fSRichard Tran Mills PetscMallocResetCUDAHost - Reset the changes made by PetscMallocSetCUDAHost 502a01fb1fSRichard Tran Mills 512a01fb1fSRichard Tran Mills Not Collective 522a01fb1fSRichard Tran Mills 532a01fb1fSRichard Tran Mills Level: developer 542a01fb1fSRichard Tran Mills 55db781477SPatrick Sanan .seealso: `PetscMallocSetCUDAHost()` 562a01fb1fSRichard Tran Mills @*/ 57*9371c9d4SSatish Balay PetscErrorCode PetscMallocResetCUDAHost(void) { 582a01fb1fSRichard Tran Mills PetscFunctionBegin; 592a01fb1fSRichard Tran Mills PetscTrMalloc = PetscMallocOld; 6042e673e7SRichard Tran Mills PetscTrRealloc = PetscReallocOld; 612a01fb1fSRichard Tran Mills PetscTrFree = PetscFreeOld; 622a01fb1fSRichard Tran Mills PetscFunctionReturn(0); 632a01fb1fSRichard Tran Mills } 64