-
Notifications
You must be signed in to change notification settings - Fork 3
/
ClCompute.h
237 lines (214 loc) · 5.39 KB
/
ClCompute.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* ClCompute.h
*
* Created on: Feb 14, 2021
* Author: tugrul
*/
#ifndef CLCOMPUTE_H_
#define CLCOMPUTE_H_
#include<memory>
#include<map>
#include<string>
#include <CL/cl.h>
#include "ClDevice.h"
#include "ClContext.h"
#include "ClCommandQueue.h"
#include <stdexcept>
class ClComputeParameter
{
public:
ClComputeParameter(){mem=nullptr; built=false; name="", argIndex=-1, sizeBytes=0;}
ClComputeParameter(ClContext ctx,std::string nameP, size_t sizeBytesP, int argIndexP, cl_mem outerGpuBuf=nullptr)
{
name = nameP;
sizeBytes=sizeBytesP;
argIndex=argIndexP;
cl_int err;
mem = std::unique_ptr<cl_mem>(new cl_mem());
if(outerGpuBuf==nullptr)
{
*mem = clCreateBuffer( *ctx.ctxPtr(), CL_MEM_READ_WRITE,sizeBytes,nullptr,&err);
if(CL_SUCCESS != err)
{
built = false;
throw std::invalid_argument("Error: compute parameter create failed: ");
}
else
{
built = true;
}
}
else
{
*mem = outerGpuBuf;
built=false;
}
}
ClComputeParameter (const ClComputeParameter &) = delete;
ClComputeParameter & operator = (const ClComputeParameter &) = delete;
int getIdx(){ return argIndex;}
int getSize(){ return sizeBytes; }
std::string getName(){ return name; }
cl_mem getMem(){ return *mem; }
cl_mem * getMemPtr(){ return mem.get(); }
~ClComputeParameter(){ if(built){ if(CL_SUCCESS!=clReleaseMemObject(*mem)){ std::cout<<"Error: release kernel parameter mem object"<<std::endl;} }}
private:
std::unique_ptr<cl_mem> mem;
int argIndex;
int sizeBytes;
std::string name;
bool built;
};
class ClCompute
{
public:
ClCompute(){program=nullptr; kernel=nullptr; programBuilt=false; kernelBuilt = false; }
ClCompute(ClContext ctx, ClDevice dv, std::string clKernelCodes, std::string clKernelName)
{
const char * c_str = clKernelCodes.c_str();
cl_int err;
program = clCreateProgramWithSource(
*ctx.ctxPtr(),
1,
&c_str,
NULL,
&err );
if(CL_SUCCESS!=err)
{
throw std::invalid_argument("Error: program creation failure");
programBuilt=false;
kernelBuilt=false;
}
else
{
err=clBuildProgram( program, 1, dv.devPtr(), nullptr, nullptr, nullptr );
if(CL_SUCCESS != err)
{
throw std::invalid_argument("Error: program compilation failure");
programBuilt=false;
kernelBuilt=false;
}
else
{
programBuilt=true;
const char * name = clKernelName.c_str();
kernel = clCreateKernel( program, name, &err );
if(CL_SUCCESS != err)
{
throw std::invalid_argument("Error: kernel creation failure");
kernelBuilt = false;
}
else
{
kernelBuilt=true;
}
}
}
}
void addParameter(ClContext ctx, std::string name, int lengthByte, int parameterIndex, cl_mem outerGpuBuf=nullptr)
{
parameters[name]=std::unique_ptr<ClComputeParameter>(new ClComputeParameter(ctx, name,lengthByte,parameterIndex,outerGpuBuf));
}
void setKernelArgs(int arg = -1)
{
cl_int err;
for(auto const & e:parameters)
{
if((arg == -1) || (arg == e.second->getIdx()))
{
err=clSetKernelArg( kernel, e.second->getIdx(),
sizeof(e.second->getMem()),
(void*)(e.second->getMemPtr() )
);
if(CL_SUCCESS != err)
{
std::string errStr = ((ClComputeParameter*)(e.second.get()))->getName();
throw std::invalid_argument(std::string("Error: kernel arg set: ")+errStr);
}
}
}
}
// reads "size" bytes from value
template<typename D>
void setArgValueAsync(std::string name, ClCommandQueue q, const D * valuePtr)
{
auto it = parameters.find(name);
if(it!=parameters.end())
{
cl_int err;
err=clEnqueueWriteBuffer( q.getQueue(),
it->second->getMem(),
CL_FALSE,0,
it->second->getSize(),
valuePtr,0,nullptr,nullptr);
if(CL_SUCCESS != err)
{
throw std::invalid_argument(std::string("error: (find)write buffer: setArgValueAsync: ") + name);
}
}
else
{
throw std::invalid_argument(std::string("Error: setArgValueAsync: ") + name);
}
}
void runAsync(ClCommandQueue q, size_t numThreads, size_t numLocalThreads)
{
size_t numThr=numThreads;
size_t numThrLoc=numLocalThreads;
size_t ofs = 0;
cl_int err;
if(CL_SUCCESS!=(err=clEnqueueNDRangeKernel(q.getQueue(),kernel,1,&ofs,&numThr,&numThrLoc,0,nullptr,nullptr)))
{
throw std::invalid_argument(std::string("error: kernel run: err code=")+std::to_string(err));
}
}
// writes "size" bytes to value
template<typename D>
void getArgValueAsync(std::string name, ClCommandQueue q, D & value)
{
auto it = parameters.find(name);
if(it!=parameters.end())
{
cl_int err;
err=clEnqueueReadBuffer(q.getQueue(),
it->second->getMem(),
CL_FALSE,0,
it->second->getSize(),
&value,0,nullptr,nullptr);
if(CL_SUCCESS != err)
{
throw std::invalid_argument("error: (find)write buffer: setArgValueAsync ");
}
}
else
{
throw std::invalid_argument("Error: setArgValueAsync");
}
}
void sync(ClCommandQueue q){clFinish(q.getQueue());}
size_t getArgSizeBytes(std::string name)
{
auto it = parameters.find(name);
if(it!=parameters.end())
{
return parameters[name]->getSize();
}
else
{
return 0;
}
}
~ClCompute(){
if(kernelBuilt)
clReleaseKernel(kernel);
if(programBuilt)
clReleaseProgram(program);
}
private:
bool programBuilt;
bool kernelBuilt;
cl_program program;
cl_kernel kernel;
std::map<std::string,std::unique_ptr<ClComputeParameter>> parameters;
};
#endif /* CLCOMPUTE_H_ */