forked from abduld/libwb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbCUDA.cpp
44 lines (36 loc) · 1.02 KB
/
wbCUDA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "wb.h"
#ifdef WB_USE_CUDA
typedef struct st_wbCUDAMemory_t {
void * mem;
size_t sz;
} wbCUDAMemory_t;
static wbCUDAMemory_t _cudaMemoryList[1024];
static int idx = 0;
size_t _cudaMallocSize = 0;
cudaError_t wbCUDAMalloc(void ** devPtr, size_t sz) {
cudaError_t err = cudaMalloc(devPtr, sz);
if (idx == 0) {
memset(_cudaMemoryList, 0, sizeof(wbCUDAMemory_t) * 1024);
}
_cudaMallocSize += sz;
_cudaMemoryList[idx].mem = *devPtr;
_cudaMemoryList[idx].sz = sz;
idx++;
return err;
}
cudaError_t wbCUDAFree(void * mem) {
if (idx == 0) {
memset(_cudaMemoryList, 0, sizeof(wbCUDAMemory_t) * 1024);
}
for (int ii = 0; ii < idx; ii++) {
if (_cudaMemoryList[ii].mem != NULL &&
_cudaMemoryList[ii].mem == mem) {
cudaError_t err = cudaFree(mem);
_cudaMallocSize -= _cudaMemoryList[ii].sz;
_cudaMemoryList[idx].mem = NULL;
return err;
}
}
return cudaSuccess;
}
#endif /* WB_USE_CUDA */