forked from CRVI/OpenCLIPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Readme - About this library.txt
452 lines (296 loc) · 16.8 KB
/
Readme - About this library.txt
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
This library is designed to do accelerated image processing using OpenCL
It is designed to be simple to use and to have a low overhead.
How to build the library :
Windows : Use the .sln file with Visual Studio 2012
Other platforms : use make with the provided makefiles
user@machine ~/OpenCLIPP# make
How to use the library :
#include <OpenCLIPP.hpp>
using namespace OpenCLIPP;
COpenCL CL; // Initialize OpenCL
// Fill a SImage structure with image information
SImage SomeImage = {width, height, step, depth, channels};
// Create an image in the device
// Image data is not sent to the device
Image Img1(CL, SomeImage, Img1Data);
// Create a second image in the device
Image Img2(CL, SomeImage, Img2Data);
// Create an OpenCL program to do arithmetic operations
Arithmetic arithmetic(CL);
// Will add 10 to each pixel value and put the result in Img2 in the device
// Img1 is automatically sent to the device if it was not sent previously
// The memory transfer and calculation are done asynchronously
arithmetic.Add(Img1, Img2, 10);
// Ask to read Img2 back into our image, after previous operations are done
// true as argument means we want to wait for the transfer to be done
// because the transfer will happen only after previous operations are done
Img2.Read(true);
// The image now contains the result of the addition
- Error handling
Any error or out of the ordinary event will be signaled by throwing a cl::Error object
The cl::Error contains the OpenCL status code ( .err() ) and a string representing the function that did the thow or an explanation of why it was thrown ( .what() ).
Using try & catch, either for the whole application or for each interaction with the library is recommended.
Try catch example :
try
{
COpenCL CL;
// More code
}
catch (cl::Error e)
{
std::string Msg = "Eror : ";
Msg += COpenCL::ErrorName(e.Err());
Msg += " - ";
Msg += e.what();
// Use Msg
}
- List of Structures
SImage - List necessary information about an image
It is used to initialize Image objects
struct SImage
{
uint Width;
uint Height;
uint Step; ///< Nb of bytes between each row
uint Channels; ///< Allowed values : 1, 2, 3 or 4
enum EDataType
{
U8,
S8,
U16,
S16,
U32,
S32,
F32,
} Type;
};
- List of classes
COpenCL - Takes care of initializing OpenCL
members :
COpenCL(const char * PreferredPlatform = "", cl_device_type deviceType = CL_DEVICE_TYPE_ALL)
Initializes OpenCL
PreferredPlatform is the name of the OpenCL platform to use
cl_device_type is the type of device to use, can be :
CL_DEVICE_TYPE_CPU, CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_ACCELERATOR,
CL_DEVICE_TYPE_ALL or CL_DEVICE_TYPE_DEFAULT
cl_device_type will be used only if PreferredPlatform is specified
The default OpenCL platform will be used if no PreferredPlatform is given
Currently, Only a single device is used and with an in-order command queue
cl::CommandQueue& GetQueue()
Retreives the command queue
static const char * COpenCL::ErrorName(cl_int status)
Returns a short textual description of the error code
Memory - base class for Buffers and Images - not useable directly
members :
bool IsInDevice() const
Returns true if the memory object contains useful data
Will return true if data has been sent to this object or if a kernel has written to this object
void SetInDevice(bool inDevice = true)
Sets or clears the InDevice state of the object
virtual void SendIfNeeded()
Overridable method that sends the assotiated host data to the device memory
(does nothing by default as there is no assotiated data)
IBuffer : public Memory - base class for Buffers - not useable directly
members :
size_t Size() const
returns the number of bytes in the buffer
TempBuffer : public IBuffer - Represents a buffer that is only in the device
members :
TempBuffer(COpenCL& CL, size_t size, cl_mem_flags flags = CL_MEM_READ_WRITE)
Allocates memory on the device
CL : The device to use
size : Number of bytes to allocate
flags : Type of device memory to allocate
Allowed values :
CL_MEM_READ_WRITE - kernels can read & write values
CL_MEM_WRITE_ONLY - kernels can only write values
CL_MEM_READ_ONLY - kernels can only read values
ReadBuffer : public IBuffer - Represents a read only buffer
members :
template<class T>
ReadBuffer(COpenCL& CL, T * data, size_t length)
Creates a copy of the contents of data then allocates memory on the device
Useful when using a small temporary buffer as parameter to a kernel that is launched asyncrhonously
CL : The device to use
length : Number of elements to allocate
Buffer : public IBuffer - Represents a buffer that exist in both the host and the device
members :
template<class T>
Buffer(COpenCL& CL, T * data, size_t length, cl_mem_flags flags = CL_MEM_READ_WRITE);
Allocates memory on the device, stores the address of the data
Upon creation, the content of data is not copied and it is not transfered to the device
CL : The device to use
data : The host buffer - pointer must remain valid as long as Read() and Send() may be done on this object
length : Number of elements to allocate
flags : Type of device memory to allocate
Allowed values :
CL_MEM_READ_WRITE - kernels can read & write values
CL_MEM_WRITE_ONLY - kernels can only write values
CL_MEM_READ_ONLY - kernels can only read values
void Read(bool blocking = false)
Reads the data from buffer in the device to the host buffer given to the constructor
blocking : if true, the method will wait for all device operations to be done, including the reading the data
void Send(bool blocking = false)
Sends the data from the host buffer to the buffer in the device
blocking : if true, the method will wait for all device operations to be done
virtual void SendIfNeeded()
Calls Send() if IsInDevice() is false
ImageBase - base class for Images - not useable directly
members:
cl::NDRange FullRange()
returns a global range that will start 1 work-item per pixel of the image
cl::NDRange VectorRange(int NbElementsPerWorker)
returns a global range that will start multiple work-items per pixel channel of the image
size_t Width() const;
size_t Height() const;
size_t Step() const;
size_t Depth() const;
size_t NbChannels() const;
size_t NbBytes() const;
bool IsFloat() const;
bool IsUnsigned() const;
return information about the image
SImage ToSImage() const;
Creates a SImage that represents the image
Only the size and type are used, the Data field of SImage will be null
ImageBuffer : public Buffer, public ImageBase - Represents a buffer in the device that contains an image, the buffer exists both
members:
ImageBuffer(COpenCL& CL, const SImage& image, cl_mem_flags flags = CL_MEM_READ_WRITE)
Allocates memory in the device, with enough space to contain the image
Upon creation, the content of image is not copied and it is not transfered to the device
CL : The device to use
image : Information about an image - pointer image.Data must remain valid as long as Read() and Send() may be done on this object
image can have 1, 3 or 4 channels
flags : Type of device memory to allocate
Allowed values :
CL_MEM_READ_WRITE - kernels can read & write values
CL_MEM_WRITE_ONLY - kernels can only write values
CL_MEM_READ_ONLY - kernels can only read values
important inherited members:
void Read(bool blocking = false)
Reads the data from buffer in the device to the host buffer given to the constructor
blocking : if true, the method will wait for all device operations to be done, including the reading the data
void Send(bool blocking = false)
Sends the data from the host buffer to the buffer in the device
blocking : if true, the method will wait for all device operations to be done
virtual void SendIfNeeded()
Calls Send() if IsInDevice() is false
TempImageBuffer : public ImageBuffer - Represents a device-only buffer that contains an image
members:
TempImageBuffer(COpenCL& CL, const SImage& image, cl_mem_flags flags = CL_MEM_READ_WRITE)
Allocates memory in the device, with enough space to contain the image
CL : The device to use
image : Information about an image
flags : Type of device memory to allocate
Allowed values :
CL_MEM_READ_WRITE - kernels can read & write values
CL_MEM_WRITE_ONLY - kernels can only write values
CL_MEM_READ_ONLY - kernels can only read values
TempImageBuffer(COpenCL& CL, const SImage& image, size_t Depth, size_t Channels, bool isFloat = false)
Allocates memory in the device, with enough space to contain an image that has the same dimentions as image but with a different data type
CL : The device to use
image : Information about an image, image can have 1, 3 or 4 channels
Depth : number of bits per channel
Channels : number of channels per pixel
isFloat : true if we desire a float image
virtual void SendIfNeeded()
Does nothing (resides only in the device and can't be sent)
IImage : public ImageBase, public Memory - base class for Images containing a cl::Image2D - not useable directly
TempImage : public IImage - Represents an image that is only in the device
members:
TempImage(COpenCL& CL, const SImage& image, cl_mem_flags flags = CL_MEM_READ_WRITE)
Allocates an image in the device with the same dimention and type as the given image
CL : The device to use
image : Information about an image, image must have 1 or 4 channels
flags : Type of device memory to allocate
Allowed values :
CL_MEM_READ_WRITE - kernels can read & write values
CL_MEM_WRITE_ONLY - kernels can only write values
CL_MEM_READ_ONLY - kernels can only read values
TempImage(COpenCL& CL, const SImage& image, size_t Depth, size_t Channels, bool isFloat = false)
Allocates memory in the device, with enough space to contain an image that has the same dimentions as image but with a different data type
CL : The device to use
image : Information about an image
Depth : number of bits per channel
Channels : number of channels per pixel
isFloat : true if we desire a float image
Image : public IImage - Represents an image that is both in the host and in the device
members:
Image(COpenCL& CL, SImage& image, cl_mem_flags flags = CL_MEM_READ_WRITE)
Allocates an image in the device with the same dimention and type as the given image
Upon creation, the content of image is not copied and it is not transfered to the device
CL : The device to use
image : Information about an image - pointer image.Data must remain valid as long as Read() and Send() may be done on this object
image must have 1 or 4 channels
flags : Type of device memory to allocate
Allowed values :
CL_MEM_READ_WRITE - kernels can read & write values
CL_MEM_WRITE_ONLY - kernels can only write values
CL_MEM_READ_ONLY - kernels can only read values
void Read(bool blocking = false)
Reads the data from the image in the device to the pointer inside the Image structure given to the constructor
blocking : if true, the method will wait for all device operations to be done, including the reading the data
void Send(bool blocking = false)
Sends the data from the host image to the image in the device
blocking : if true, the method will wait for all device operations to be done
virtual void SendIfNeeded()
Calls Send() if IsInDevice() is false
ColorImage : public TempImage - Represents a 3 channel image on the host and a 4 channel image on the device
members:
ColorImage(COpenCL& CL, SImage& image)
Allocates an image in the device with the same dimention the given image and the same type except that it will have 4 channels
Also allocate a buffer in the device able to contain the 3 channel image
Upon creation, the content of image is not copied and it is not transfered to the device
CL : The device to use
image : Information about an image - pointer image.Data must remain valid as long as Read() and Send() may be done on this object
image must have 3 channels
flags : Type of device memory to allocate
Allowed values :
CL_MEM_READ_WRITE - kernels can read & write values
CL_MEM_WRITE_ONLY - kernels can only write values
CL_MEM_READ_ONLY - kernels can only read values
void Read(bool blocking = false)
Converts the 4 Channel image into a 3 channel buffer and reads the buffer into the host image
blocking : if true, the method will wait for all device operations to be done
void Send()
Sends the 3 channel data to a buffer in the device and converts the buffer in a 4 channel image
blocking : if true, the method will wait for all device operations to be done
virtual void SendIfNeeded()
Calls Send() if IsInDevice() is false
Program - Represents an OpenCL C program
members:
Program(COpenCL& CL, const char * Source, const char * options = "",
const char * Path = "", bool build = false)
Saves the given values for later use
CL : The device to use
Source : Source code for the program, can be ""
options : A string listing compiler options
Path : Complete path to the .cl file to use
NOTE : At least one of these two parameters must not be empty : Source and Path
bool Build()
Builds the program if it has not yet been built
If Source was "", the content of the file given with Path is loaded and used to build the program
If Source contains a program, Path is ignored and the program in Source is built
If the build fails, build information is written to std::cerr and a cl::Error is thrown
Returns true if the build is successful or if the program has been built already
MultiProgram - Holder of multiple version of an OpenCL program with different compiler options - not useable directly
ImageProgram : public MultiProgram - Contains three versions of one program, for each three versions of image types :
Signed integer, Unsigned integer and Floating point
members:
ImageProgram(COpenCL& CL, const char * Path)
Prepares a program with the .cl file at the given path
ImageProgram(COpenCL& CL, bool fromSource, const char * Source)
Prepares a program with the given Source
void PrepareFor(ImageBase& Source)
Builds the proper version of the program for the given image
Program& SelectProgram(ImageBase& Source)
Builds and returns the proper version of the program for the given image
ImageBufferProgram : public MultiProgram - Contains multiple versions of one program, a version for each supported data type :
S8, U8, S16, U16, S32, U32, F32
members:
ImageBufferProgram(COpenCL& CL, const char * Path)
Prepares a program with the .cl file at the given path
void PrepareFor(ImageBase& Source)
Builds the proper version of the program for the given image
Program& SelectProgram(ImageBase& Source)
Builds and returns the proper version of the program for the given image