-
Notifications
You must be signed in to change notification settings - Fork 41
/
opencl.h
120 lines (71 loc) · 1.87 KB
/
opencl.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* opencl.h
*
* Created on: 01.05.2014
* Author: mad
*/
#ifndef OPENCL_H_
#define OPENCL_H_
#include <CL/cl.h>
extern cl_context gContext;
#define OCL(error) \
if(cl_int err = error){ \
printf("OpenCL error: %d at %s:%d\n", err, __FILE__, __LINE__); \
return; \
}
#define OCLR(error, ret) \
if(cl_int err = error){ \
printf("OpenCL error: %d at %s:%d\n", err, __FILE__, __LINE__); \
return ret; \
}
#define OCLE(error) \
if(cl_int err = error){ \
printf("OpenCL error: %d at %s:%d\n", err, __FILE__, __LINE__); \
exit(err); \
}
template<typename T>
class clBuffer {
public:
clBuffer() {
Size = 0;
HostData = 0;
DeviceData = 0;
}
~clBuffer() {
if(HostData)
delete [] HostData;
if(DeviceData)
clReleaseMemObject(DeviceData);
}
void init(int size, cl_mem_flags flags = 0) {
Size = size;
if(!(flags & CL_MEM_HOST_NO_ACCESS)){
HostData = new T[Size];
memset(HostData, 0, Size*sizeof(T));
}else
HostData = 0;
//printf("clCreateBuffer: size = %d, %d bytes\n", Size, Size*sizeof(T));
cl_int error;
DeviceData = clCreateBuffer(gContext, flags, Size*sizeof(T), 0, &error);
OCL(error);
}
void copyToDevice(cl_command_queue cq, bool blocking = true) {
OCL(clEnqueueWriteBuffer(cq, DeviceData, blocking, 0, Size*sizeof(T), HostData, 0, 0, 0));
}
void copyToHost(cl_command_queue cq, bool blocking = true, unsigned size = 0) {
if(size == 0)
size = Size;
OCL(clEnqueueReadBuffer(cq, DeviceData, blocking, 0, size*sizeof(T), HostData, 0, 0, 0));
}
T& get(int index) {
return HostData[index];
}
T& operator[](int index) {
return HostData[index];
}
public:
int Size;
T* HostData;
cl_mem DeviceData;
};
#endif /* OPENCL_H_ */