xref: /petsc/src/sys/memory/cuda/mcudahost.cu (revision 811af0c4b09a35de4306c442f88bd09fdc09897d)
12a01fb1fSRichard Tran Mills #include <petscsys.h>    /*I   "petscsys.h"   I*/
29566063dSJacob Faibussowitsch #include <petscdevice.h> /* Needed to provide PetscCallCUDA() */
32a01fb1fSRichard Tran Mills 
49371c9d4SSatish 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 
99371c9d4SSatish 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 
149371c9d4SSatish 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
23*811af0c4SBarry Smith    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 
30*811af0c4SBarry Smith    Note:
312a01fb1fSRichard Tran Mills      This provides a way to use the CUDA malloc and free routines temporarily. One
32*811af0c4SBarry Smith      can switch back to the previous choice by calling `PetscMallocResetCUDAHost()`.
332a01fb1fSRichard Tran Mills 
34*811af0c4SBarry Smith .seealso: `PetscCUDAHostMalloc()`, `PetscMallocResetCUDAHost()`, `PetscMallocSetHIPHost()`
352a01fb1fSRichard Tran Mills @*/
369371c9d4SSatish 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
49*811af0c4SBarry Smith    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 
55*811af0c4SBarry Smith .seealso: `PetscCUDAHostMalloc()`, `PetscMallocSetCUDAHost()`
562a01fb1fSRichard Tran Mills @*/
579371c9d4SSatish 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