forked from hughperkins/EasyCL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLArray.h
More file actions
94 lines (87 loc) · 2.52 KB
/
CLArray.h
File metadata and controls
94 lines (87 loc) · 2.52 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#include "EasyCL_export.h"
class EasyCL_EXPORT CLArray {
public:
int N;
bool onHost;
bool onDevice;
cl_mem devicearray;
EasyCL *easycl; // NOT owned by this object, so dont free!
cl_int error;
CLArray(int N, EasyCL *easycl) {
this->N = N;
this->easycl = easycl;
error = CL_SUCCESS;
onDevice = false;
onHost = false;
}
virtual ~CLArray() {
if(onDevice) {
clReleaseMemObject(devicearray);
}
}
virtual void allocateHostArray(int N) = 0;
virtual void deleteHostArray() = 0;
virtual void *getHostArray() = 0;
void createOnDevice() {
assert(!onHost && !onDevice);
devicearray = clCreateBuffer(*(easycl->context), CL_MEM_READ_WRITE, getElementSize() * N, 0, &error);
assert(error == CL_SUCCESS);
onDevice = true;
}
void copyToDevice() {
assert(onHost && !onDevice);
devicearray = clCreateBuffer(*(easycl->context), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, getElementSize() * N, getHostArray(), &error);
easycl->checkError(error);
onDevice = true;
}
void moveToDevice() {
assert(onHost && !onDevice);
devicearray = clCreateBuffer(*(easycl->context), CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, getElementSize() * N, getHostArray(), &error);
assert(error == CL_SUCCESS);
deleteHostArray();
onDevice = true;
onHost = false;
}
void moveToHost() {
copyToHost();
deleteFromDevice();
}
void copyToHost() {
assert(onDevice);
if(!onHost) {
allocateHostArray(N);
onHost = true;
}
error = clEnqueueReadBuffer(*(easycl->queue), devicearray, CL_TRUE, 0, getElementSize() * N, getHostArray(), 0, NULL, NULL);
easycl->checkError(error);
}
void deleteFromHost(){
assert(onHost);
deleteHostArray();
onHost = false;
}
cl_mem *getDeviceArray() {
if(!onDevice) {
assert(onHost);
copyToDevice();
deleteFromHost();
}
return &devicearray;
}
void deleteFromDevice(){
assert(onDevice);
clReleaseMemObject(devicearray);
onDevice = false;
}
inline int size() {
return N;
}
inline bool isOnHost(){
return onHost;
}
inline bool isOnDevice(){
return onDevice;
}
virtual int getElementSize() = 0;
};