-
Notifications
You must be signed in to change notification settings - Fork 25
/
cuda_pointer.h
52 lines (51 loc) · 1.11 KB
/
cuda_pointer.h
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
44
45
46
47
48
49
50
51
52
#include <assert.h>
// #include <cutil.h>
template <typename T>
struct cudaPointer{
T *dev_pointer;
T *host_pointer;
int size;
cudaPointer(){
dev_pointer = NULL;
host_pointer = NULL;
size = 0;
}
// ~cudaPointer(){
// free();
// }
void allocate(int _size){
size = _size;
void *p;
CUDA_SAFE_CALL(cudaMalloc(&p, size * sizeof(T)));
assert(p);
dev_pointer = (T*)p;
CUDA_SAFE_CALL(cudaMallocHost(&p, size * sizeof(T)));
assert(p);
host_pointer = (T*)p;
}
void free(){
CUDA_SAFE_CALL(cudaFree(dev_pointer));
CUDA_SAFE_CALL(cudaFreeHost(host_pointer));
dev_pointer = NULL;
host_pointer = NULL;
size = 0;
}
void htod(int count){
CUDA_SAFE_CALL(cudaMemcpy(dev_pointer, host_pointer, count * sizeof(T), cudaMemcpyHostToDevice));
}
void htod(){
this->htod(size);
}
void dtoh(int count){
CUDA_SAFE_CALL(cudaMemcpy(host_pointer, dev_pointer, count * sizeof(T), cudaMemcpyDeviceToHost));
}
void dtoh(){
this->dtoh(size);
}
T &operator [] (int i){
return host_pointer[i];
}
operator T* (){
return dev_pointer;
}
};