-
Notifications
You must be signed in to change notification settings - Fork 2
/
cl.hpp
3814 lines (3374 loc) · 106 KB
/
cl.hpp
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2008-2010 The Khronos Group Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and/or associated documentation files (the
* "Materials"), to deal in the Materials without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Materials, and to
* permit persons to whom the Materials are furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Materials.
*
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
******************************************************************************/
/*! \file
*
* \brief C++ bindings for OpenCL 1.0 (rev 48) and OpenCL 1.1 (rev 33)
* \author Benedict R. Gaster and Laurent Morichetti
*
* Additions and fixes from Brian Cole, March 3rd 2010.
*
* \version 1.1
* \date June 2010
*
* Optional extension support
*
* cl
* cl_ext_device_fission
* #define USE_CL_DEVICE_FISSION
*/
/*! \mainpage
* \section intro Introduction
* For many large applications C++ is the language of choice and so it seems
* reasonable to define C++ bindings for OpenCL.
*
*
* The interface is contained with a single C++ header file \em cl.hpp and all
* definitions are contained within the namespace \em cl. There is no additional
* requirement to include \em cl.h and to use either the C++ or original C
* bindings it is enough to simply include \em cl.hpp.
*
* The bindings themselves are lightweight and correspond closely to the
* underlying C API. Using the C++ bindings introduces no additional execution
* overhead.
*
* For detail documentation on the bindings see:
*
* The OpenCL C++ Wrapper API 1.1 (revision 04)
* http://www.khronos.org/registry/cl/specs/opencl-cplusplus-1.1.pdf
*
* \section example Example
*
* The following example shows a general use case for the C++
* bindings, including support for the optional exception feature and
* also the supplied vector and string classes, see following sections for
* decriptions of these features.
*
* \code
* #define __CL_ENABLE_EXCEPTIONS
*
* #if defined(__APPLE__) || defined(__MACOSX)
* #include <OpenCL/cl.hpp>
* #else
* #include <CL/cl.hpp>
* #endif
* #include <cstdio>
* #include <cstdlib>
* #include <iostream>
*
* const char * helloStr = "__kernel void "
* "hello(void) "
* "{ "
* " "
* "} ";
*
* int
* main(void)
* {
* cl_int err = CL_SUCCESS;
* try {
*
* std::vector<cl::Platform> platforms;
* cl::Platform::get(&platforms);
* if (platforms.size() == 0) {
* std::cout << "Platform size 0\n";
* return -1;
* }
*
* cl_context_properties properties[] =
* { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0};
* cl::Context context(CL_DEVICE_TYPE_CPU, properties);
*
* std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
*
* cl::Program::Sources source(1,
* std::make_pair(helloStr,strlen(helloStr)));
* cl::Program program_ = cl::Program(context, source);
* program_.build(devices);
*
* cl::Kernel kernel(program_, "hello", &err);
*
* cl::Event event;
* cl::CommandQueue queue(context, devices[0], 0, &err);
* queue.enqueueNDRangeKernel(
* kernel,
* cl::NullRange,
* cl::NDRange(4,4),
* cl::NullRange,
* NULL,
* &event);
*
* event.wait();
* }
* catch (cl::Error err) {
* std::cerr
* << "ERROR: "
* << err.what()
* << "("
* << err.err()
* << ")"
* << std::endl;
* }
*
* return EXIT_SUCCESS;
* }
*
* \endcode
*
*/
#ifndef CL_HPP_
#define CL_HPP_
#ifdef _WIN32
#include <windows.h>
#include <malloc.h>
#if defined(USE_DX_INTEROP)
#include <CL/cl_d3d10.h>
#endif
#endif // _WIN32
//
#if defined(USE_CL_DEVICE_FISSION)
#include <CL/cl_ext.h>
#endif
#if defined(__APPLE__) || defined(__MACOSX)
#include <OpenGL/OpenGL.h>
#include <OpenCL/opencl.h>
#else
#include <GL/gl.h>
#include <CL/opencl.h>
#endif // !__APPLE__
#if !defined(CL_CALLBACK)
#define CL_CALLBACK
#endif //CL_CALLBACK
#include <utility>
#if !defined(__NO_STD_VECTOR)
#include <vector>
#endif
#if !defined(__NO_STD_STRING)
#include <string>
#endif
#if defined(linux) || defined(__APPLE__) || defined(__MACOSX)
# include <alloca.h>
#endif // linux
#include <cstring>
/*! \namespace cl
*
* \brief The OpenCL C++ bindings are defined within this namespace.
*
*/
namespace cl {
#define __INIT_CL_EXT_FCN_PTR(name) \
if(!pfn_##name) { \
pfn_##name = (PFN_##name) \
clGetExtensionFunctionAddress(#name); \
if(!pfn_##name) { \
} \
}
class Program;
class Device;
class Context;
class CommandQueue;
class Memory;
#if defined(__CL_ENABLE_EXCEPTIONS)
#include <exception>
/*! \class Error
* \brief Exception class
*/
class Error : public std::exception
{
private:
cl_int err_;
const char * errStr_;
public:
/*! Create a new CL error exception for a given error code
* and corresponding message.
*/
Error(cl_int err, const char * errStr = NULL) : err_(err), errStr_(errStr)
{}
~Error() throw() {}
/*! \brief Get error string associated with exception
*
* \return A memory pointer to the error message string.
*/
virtual const char * what() const throw ()
{
if (errStr_ == NULL) {
return "empty";
}
else {
return errStr_;
}
}
/*! \brief Get error code associated with exception
*
* \return The error code.
*/
const cl_int err(void) const { return err_; }
};
#define __ERR_STR(x) #x
#else
#define __ERR_STR(x) NULL
#endif // __CL_ENABLE_EXCEPTIONS
//! \cond DOXYGEN_DETAIL
#if !defined(__CL_USER_OVERRIDE_ERROR_STRINGS)
#define __GET_DEVICE_INFO_ERR __ERR_STR(clgetDeviceInfo)
#define __GET_PLATFORM_INFO_ERR __ERR_STR(clGetPlatformInfo)
#define __GET_DEVICE_IDS_ERR __ERR_STR(clGetDeviceIDs)
#define __GET_PLATFORM_IDS_ERR __ERR_STR(clGetPlatformIDs)
#define __GET_CONTEXT_INFO_ERR __ERR_STR(clGetContextInfo)
#define __GET_EVENT_INFO_ERR __ERR_STR(clGetEventInfo)
#define __GET_EVENT_PROFILE_INFO_ERR __ERR_STR(clGetEventProfileInfo)
#define __GET_MEM_OBJECT_INFO_ERR __ERR_STR(clGetMemObjectInfo)
#define __GET_IMAGE_INFO_ERR __ERR_STR(clGetImageInfo)
#define __GET_SAMPLER_INFO_ERR __ERR_STR(clGetSamplerInfo)
#define __GET_KERNEL_INFO_ERR __ERR_STR(clGetKernelInfo)
#define __GET_KERNEL_WORK_GROUP_INFO_ERR __ERR_STR(clGetKernelWorkGroupInfo)
#define __GET_PROGRAM_INFO_ERR __ERR_STR(clGetProgramInfo)
#define __GET_PROGRAM_BUILD_INFO_ERR __ERR_STR(clGetProgramBuildInfo)
#define __GET_COMMAND_QUEUE_INFO_ERR __ERR_STR(clGetCommandQueueInfo)
#define __CREATE_CONTEXT_FROM_TYPE_ERR __ERR_STR(clCreateContextFromType)
#define __GET_SUPPORTED_IMAGE_FORMATS_ERR __ERR_STR(clGetSupportedImageFormats)
#define __CREATE_BUFFER_ERR __ERR_STR(clCreateBuffer)
#define __CREATE_SUBBUFFER_ERR __ERR_STR(clCreateSubBuffer)
#define __CREATE_GL_BUFFER_ERR __ERR_STR(clCreateFromGLBuffer)
#define __GET_GL_OBJECT_INFO_ERR __ERR_STR(clGetGLObjectInfo)
#define __CREATE_IMAGE2D_ERR __ERR_STR(clCreateImage2D)
#define __CREATE_IMAGE3D_ERR __ERR_STR(clCreateImage3D)
#define __CREATE_SAMPLER_ERR __ERR_STR(clCreateSampler)
#define __SET_MEM_OBJECT_DESTRUCTOR_CALLBACK_ERR __ERR_STR(clSetMemObjectDestructorCallback)
#define __CREATE_USER_EVENT_ERR __ERR_STR(clCreateUserEvent)
#define __SET_USER_EVENT_STATUS_ERR __ERR_STR(clSetUserEventStatus)
#define __SET_EVENT_CALLBACK_ERR __ERR_STR(clSetEventCallback)
#define __WAIT_FOR_EVENTS_ERR __ERR_STR(clWaitForEvents)
#define __CREATE_KERNEL_ERR __ERR_STR(clCreateKernel)
#define __SET_KERNEL_ARGS_ERR __ERR_STR(clSetKernelArg)
#define __CREATE_PROGRAM_WITH_SOURCE_ERR __ERR_STR(clCreateProgramWithSource)
#define __CREATE_PROGRAM_WITH_BINARY_ERR __ERR_STR(clCreateProgramWithBinary)
#define __BUILD_PROGRAM_ERR __ERR_STR(clBuildProgram)
#define __CREATE_KERNELS_IN_PROGRAM_ERR __ERR_STR(clCreateKernelsInProgram)
#define __CREATE_COMMAND_QUEUE_ERR __ERR_STR(clCreateCommandQueue)
#define __SET_COMMAND_QUEUE_PROPERTY_ERR __ERR_STR(clSetCommandQueueProperty)
#define __ENQUEUE_READ_BUFFER_ERR __ERR_STR(clEnqueueReadBuffer)
#define __ENQUEUE_READ_BUFFER_RECT_ERR __ERR_STR(clEnqueueReadBufferRect)
#define __ENQUEUE_WRITE_BUFFER_ERR __ERR_STR(clEnqueueWriteBuffer)
#define __ENQUEUE_WRITE_BUFFER_RECT_ERR __ERR_STR(clEnqueueWriteBufferRect)
#define __ENQEUE_COPY_BUFFER_ERR __ERR_STR(clEnqueueCopyBuffer)
#define __ENQEUE_COPY_BUFFER_RECT_ERR __ERR_STR(clEnqueueCopyBufferRect)
#define __ENQUEUE_READ_IMAGE_ERR __ERR_STR(clEnqueueReadImage)
#define __ENQUEUE_WRITE_IMAGE_ERR __ERR_STR(clEnqueueWriteImage)
#define __ENQUEUE_COPY_IMAGE_ERR __ERR_STR(clEnqueueCopyImage)
#define __ENQUEUE_COPY_IMAGE_TO_BUFFER_ERR __ERR_STR(clEnqueueCopyImageToBuffer)
#define __ENQUEUE_COPY_BUFFER_TO_IMAGE_ERR __ERR_STR(clEnqueueCopyBufferToImage)
#define __ENQUEUE_MAP_BUFFER_ERR __ERR_STR(clEnqueueMapBuffer)
#define __ENQUEUE_MAP_IMAGE_ERR __ERR_STR(clEnqueueMapImage)
#define __ENQUEUE_UNMAP_MEM_OBJECT_ERR __ERR_STR(clEnqueueUnMapMemObject)
#define __ENQUEUE_NDRANGE_KERNEL_ERR __ERR_STR(clEnqueueNDRangeKernel)
#define __ENQUEUE_TASK_ERR __ERR_STR(clEnqueueTask)
#define __ENQUEUE_NATIVE_KERNEL __ERR_STR(clEnqueueNativeKernel)
#define __ENQUEUE_MARKER_ERR __ERR_STR(clEnqueueMarker)
#define __ENQUEUE_WAIT_FOR_EVENTS_ERR __ERR_STR(clEnqueueWaitForEvents)
#define __ENQUEUE_BARRIER_ERR __ERR_STR(clEnqueueBarrier)
#define __ENQUEUE_ACQUIRE_GL_ERR __ERR_STR(clEnqueueAcquireGLObjects)
#define __ENQUEUE_RELEASE_GL_ERR __ERR_STR(clEnqueueReleaseGLObjects)
#define __FLUSH_ERR __ERR_STR(clFlush)
#define __FINISH_ERR __ERR_STR(clFinish)
#define __CREATE_SUB_DEVICES __ERR_STR(clCreateSubDevicesEXT)
#endif // __CL_USER_OVERRIDE_ERROR_STRINGS
//! \endcond
/*! \class string
* \brief Simple string class, that provides a limited subset of std::string
* functionality but avoids many of the issues that come with that class.
*/
class string
{
private:
::size_t size_;
char * str_;
public:
string(void) : size_(0), str_(NULL)
{
}
string(char * str, ::size_t size) :
size_(size),
str_(NULL)
{
str_ = new char[size_+1];
if (str_ != NULL) {
memcpy(str_, str, size_ * sizeof(char));
str_[size_] = '\0';
}
else {
size_ = 0;
}
}
string(char * str) :
str_(NULL)
{
size_= ::strlen(str);
str_ = new char[size_ + 1];
if (str_ != NULL) {
memcpy(str_, str, (size_ + 1) * sizeof(char));
}
else {
size_ = 0;
}
}
string& operator=(const string& rhs)
{
if (this == &rhs) {
return *this;
}
if (rhs.size_ == 0 || rhs.str_ == NULL) {
size_ = 0;
str_ = NULL;
}
else {
size_ = rhs.size_;
str_ = new char[size_ + 1];
if (str_ != NULL) {
memcpy(str_, rhs.str_, (size_ + 1) * sizeof(char));
}
else {
size_ = 0;
}
}
return *this;
}
string(const string& rhs)
{
*this = rhs;
}
~string()
{
if (str_ != NULL) {
delete[] str_;
}
}
::size_t size(void) const { return size_; }
::size_t length(void) const { return size(); }
const char * c_str(void) const { return (str_) ? str_ : "";}
};
#if !defined(__USE_DEV_STRING) && !defined(__NO_STD_STRING)
#include <string>
typedef std::string STRING_CLASS;
#elif !defined(__USE_DEV_STRING)
typedef cl::string STRING_CLASS;
#endif
#if !defined(__USE_DEV_VECTOR) && !defined(__NO_STD_VECTOR)
#include <vector>
#define VECTOR_CLASS std::vector
#elif !defined(__USE_DEV_VECTOR)
#define VECTOR_CLASS cl::vector
#endif
#if !defined(__MAX_DEFAULT_VECTOR_SIZE)
#define __MAX_DEFAULT_VECTOR_SIZE 10
#endif
/*! \class vector
* \brief Fixed sized vector implementation that mirroring
* std::vector functionality.
*/
template <typename T, unsigned int N = __MAX_DEFAULT_VECTOR_SIZE>
class vector
{
private:
T data_[N];
unsigned int size_;
bool empty_;
public:
vector() :
size_(-1),
empty_(true)
{}
~vector() {}
unsigned int size(void) const
{
return size_ + 1;
}
void clear()
{
size_ = -1;
empty_ = true;
}
void push_back (const T& x)
{
if (size() < N) {
size_++;
data_[size_] = x;
empty_ = false;
}
}
void pop_back(void)
{
if (!empty_) {
data_[size_].~T();
size_--;
if (size_ == -1) {
empty_ = true;
}
}
}
vector(const vector<T, N>& vec) :
size_(vec.size_),
empty_(vec.empty_)
{
if (!empty_) {
memcpy(&data_[0], &vec.data_[0], size() * sizeof(T));
}
}
vector(unsigned int size, const T& val = T()) :
size_(-1),
empty_(true)
{
for (unsigned int i = 0; i < size; i++) {
push_back(val);
}
}
vector<T, N>& operator=(const vector<T, N>& rhs)
{
if (this == &rhs) {
return *this;
}
size_ = rhs.size_;
empty_ = rhs.empty_;
if (!empty_) {
memcpy(&data_[0], &rhs.data_[0], size() * sizeof(T));
}
return *this;
}
bool operator==(vector<T,N> &vec)
{
if (empty_ && vec.empty_) {
return true;
}
if (size() != vec.size()) {
return false;
}
return memcmp(&data_[0], &vec.data_[0], size() * sizeof(T)) == 0 ? true : false;
}
operator T* () { return data_; }
operator const T* () const { return data_; }
bool empty (void) const
{
return empty_;
}
unsigned int max_size (void) const
{
return N;
}
unsigned int capacity () const
{
return sizeof(T) * N;
}
T& operator[](int index)
{
return data_[index];
}
T operator[](int index) const
{
return data_[index];
}
template<class I>
void assign(I start, I end)
{
clear();
while(start < end) {
push_back(*start);
start++;
}
}
/*! \class iterator
* \brief Iterator class for vectors
*/
class iterator
{
private:
vector<T,N> vec_;
int index_;
bool initialized_;
public:
iterator(void) :
index_(-1),
initialized_(false)
{
index_ = -1;
initialized_ = false;
}
~iterator(void) {}
static iterator begin(vector<T,N> &vec)
{
iterator i;
if (!vec.empty()) {
i.index_ = 0;
}
i.vec_ = vec;
i.initialized_ = true;
return i;
}
static iterator end(vector<T,N> &vec)
{
iterator i;
if (!vec.empty()) {
i.index_ = vec.size();
}
i.vec_ = vec;
i.initialized_ = true;
return i;
}
bool operator==(iterator i)
{
return ((vec_ == i.vec_) &&
(index_ == i.index_) &&
(initialized_ == i.initialized_));
}
bool operator!=(iterator i)
{
return (!(*this==i));
}
void operator++()
{
index_++;
}
void operator++(int x)
{
index_ += x;
}
void operator--()
{
index_--;
}
void operator--(int x)
{
index_ -= x;
}
T operator *()
{
return vec_[index_];
}
};
iterator begin(void)
{
return iterator::begin(*this);
}
iterator end(void)
{
return iterator::end(*this);
}
T& front(void)
{
return data_[0];
}
T& back(void)
{
return data_[size_];
}
const T& front(void) const
{
return data_[0];
}
const T& back(void) const
{
return data_[size_];
}
};
/*!
* \brief size_t class used to interface between C++ and
* OpenCL C calls that require arrays of size_t values, who's
* size is known statically.
*/
template <int N>
struct size_t : public cl::vector< ::size_t, N> { };
namespace detail {
// GetInfo help struct
template <typename Functor, typename T>
struct GetInfoHelper
{
static cl_int
get(Functor f, cl_uint name, T* param)
{
return f(name, sizeof(T), param, NULL);
}
};
// Specialized GetInfoHelper for VECTOR_CLASS params
template <typename Func, typename T>
struct GetInfoHelper<Func, VECTOR_CLASS<T> >
{
static cl_int get(Func f, cl_uint name, VECTOR_CLASS<T>* param)
{
::size_t required;
cl_int err = f(name, 0, NULL, &required);
if (err != CL_SUCCESS) {
return err;
}
T* value = (T*) alloca(required);
err = f(name, required, value, NULL);
if (err != CL_SUCCESS) {
return err;
}
param->assign(&value[0], &value[required/sizeof(T)]);
return CL_SUCCESS;
}
};
// Specialized for getInfo<CL_PROGRAM_BINARIES>
template <typename Func>
struct GetInfoHelper<Func, VECTOR_CLASS<char *> >
{
static cl_int
get(Func f, cl_uint name, VECTOR_CLASS<char *>* param)
{
cl_uint err = f(name, param->size() * sizeof(char *), &(*param)[0], NULL);
if (err != CL_SUCCESS) {
return err;
}
return CL_SUCCESS;
}
};
// Specialized GetInfoHelper for STRING_CLASS params
template <typename Func>
struct GetInfoHelper<Func, STRING_CLASS>
{
static cl_int get(Func f, cl_uint name, STRING_CLASS* param)
{
::size_t required;
cl_int err = f(name, 0, NULL, &required);
if (err != CL_SUCCESS) {
return err;
}
char* value = (char*) alloca(required);
err = f(name, required, value, NULL);
if (err != CL_SUCCESS) {
return err;
}
*param = value;
return CL_SUCCESS;
}
};
#define __GET_INFO_HELPER_WITH_RETAIN(CPP_TYPE) \
namespace detail { \
template <typename Func> \
struct GetInfoHelper<Func, CPP_TYPE> \
{ \
static cl_int get(Func f, cl_uint name, CPP_TYPE* param) \
{ \
cl_uint err = f(name, sizeof(CPP_TYPE), param, NULL); \
if (err != CL_SUCCESS) { \
return err; \
} \
\
return ReferenceHandler<CPP_TYPE::cl_type>::retain((*param)()); \
} \
}; \
}
#define __PARAM_NAME_INFO_1_0(F) \
F(cl_platform_info, CL_PLATFORM_PROFILE, STRING_CLASS) \
F(cl_platform_info, CL_PLATFORM_VERSION, STRING_CLASS) \
F(cl_platform_info, CL_PLATFORM_NAME, STRING_CLASS) \
F(cl_platform_info, CL_PLATFORM_VENDOR, STRING_CLASS) \
F(cl_platform_info, CL_PLATFORM_EXTENSIONS, STRING_CLASS) \
\
F(cl_device_info, CL_DEVICE_TYPE, cl_device_type) \
F(cl_device_info, CL_DEVICE_VENDOR_ID, cl_uint) \
F(cl_device_info, CL_DEVICE_MAX_COMPUTE_UNITS, cl_uint) \
F(cl_device_info, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, cl_uint) \
F(cl_device_info, CL_DEVICE_MAX_WORK_GROUP_SIZE, ::size_t) \
F(cl_device_info, CL_DEVICE_MAX_WORK_ITEM_SIZES, VECTOR_CLASS< ::size_t>) \
F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR, cl_uint) \
F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT, cl_uint) \
F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT, cl_uint) \
F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG, cl_uint) \
F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT, cl_uint) \
F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE, cl_uint) \
F(cl_device_info, CL_DEVICE_MAX_CLOCK_FREQUENCY, cl_uint) \
F(cl_device_info, CL_DEVICE_ADDRESS_BITS, cl_bitfield) \
F(cl_device_info, CL_DEVICE_MAX_READ_IMAGE_ARGS, cl_uint) \
F(cl_device_info, CL_DEVICE_MAX_WRITE_IMAGE_ARGS, cl_uint) \
F(cl_device_info, CL_DEVICE_MAX_MEM_ALLOC_SIZE, cl_ulong) \
F(cl_device_info, CL_DEVICE_IMAGE2D_MAX_WIDTH, ::size_t) \
F(cl_device_info, CL_DEVICE_IMAGE2D_MAX_HEIGHT, ::size_t) \
F(cl_device_info, CL_DEVICE_IMAGE3D_MAX_WIDTH, ::size_t) \
F(cl_device_info, CL_DEVICE_IMAGE3D_MAX_HEIGHT, ::size_t) \
F(cl_device_info, CL_DEVICE_IMAGE3D_MAX_DEPTH, ::size_t) \
F(cl_device_info, CL_DEVICE_IMAGE_SUPPORT, cl_uint) \
F(cl_device_info, CL_DEVICE_MAX_PARAMETER_SIZE, ::size_t) \
F(cl_device_info, CL_DEVICE_MAX_SAMPLERS, cl_uint) \
F(cl_device_info, CL_DEVICE_MEM_BASE_ADDR_ALIGN, cl_uint) \
F(cl_device_info, CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE, cl_uint) \
F(cl_device_info, CL_DEVICE_SINGLE_FP_CONFIG, cl_device_fp_config) \
F(cl_device_info, CL_DEVICE_GLOBAL_MEM_CACHE_TYPE, cl_device_mem_cache_type) \
F(cl_device_info, CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE, cl_uint)\
F(cl_device_info, CL_DEVICE_GLOBAL_MEM_CACHE_SIZE, cl_ulong) \
F(cl_device_info, CL_DEVICE_GLOBAL_MEM_SIZE, cl_ulong) \
F(cl_device_info, CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE, cl_ulong) \
F(cl_device_info, CL_DEVICE_MAX_CONSTANT_ARGS, cl_uint) \
F(cl_device_info, CL_DEVICE_LOCAL_MEM_TYPE, cl_device_local_mem_type) \
F(cl_device_info, CL_DEVICE_LOCAL_MEM_SIZE, cl_ulong) \
F(cl_device_info, CL_DEVICE_ERROR_CORRECTION_SUPPORT, cl_bool) \
F(cl_device_info, CL_DEVICE_PROFILING_TIMER_RESOLUTION, ::size_t) \
F(cl_device_info, CL_DEVICE_ENDIAN_LITTLE, cl_bool) \
F(cl_device_info, CL_DEVICE_AVAILABLE, cl_bool) \
F(cl_device_info, CL_DEVICE_COMPILER_AVAILABLE, cl_bool) \
F(cl_device_info, CL_DEVICE_EXECUTION_CAPABILITIES, cl_device_exec_capabilities) \
F(cl_device_info, CL_DEVICE_QUEUE_PROPERTIES, cl_command_queue_properties) \
F(cl_device_info, CL_DEVICE_PLATFORM, cl_platform_id) \
F(cl_device_info, CL_DEVICE_NAME, STRING_CLASS) \
F(cl_device_info, CL_DEVICE_VENDOR, STRING_CLASS) \
F(cl_device_info, CL_DRIVER_VERSION, STRING_CLASS) \
F(cl_device_info, CL_DEVICE_PROFILE, STRING_CLASS) \
F(cl_device_info, CL_DEVICE_VERSION, STRING_CLASS) \
F(cl_device_info, CL_DEVICE_EXTENSIONS, STRING_CLASS) \
\
F(cl_context_info, CL_CONTEXT_REFERENCE_COUNT, cl_uint) \
F(cl_context_info, CL_CONTEXT_DEVICES, VECTOR_CLASS<Device>) \
F(cl_context_info, CL_CONTEXT_PROPERTIES, VECTOR_CLASS<cl_context_properties>) \
\
F(cl_event_info, CL_EVENT_COMMAND_QUEUE, cl::CommandQueue) \
F(cl_event_info, CL_EVENT_COMMAND_TYPE, cl_command_type) \
F(cl_event_info, CL_EVENT_REFERENCE_COUNT, cl_uint) \
F(cl_event_info, CL_EVENT_COMMAND_EXECUTION_STATUS, cl_uint) \
\
F(cl_profiling_info, CL_PROFILING_COMMAND_QUEUED, cl_ulong) \
F(cl_profiling_info, CL_PROFILING_COMMAND_SUBMIT, cl_ulong) \
F(cl_profiling_info, CL_PROFILING_COMMAND_START, cl_ulong) \
F(cl_profiling_info, CL_PROFILING_COMMAND_END, cl_ulong) \
\
F(cl_mem_info, CL_MEM_TYPE, cl_mem_object_type) \
F(cl_mem_info, CL_MEM_FLAGS, cl_mem_flags) \
F(cl_mem_info, CL_MEM_SIZE, ::size_t) \
F(cl_mem_info, CL_MEM_HOST_PTR, void*) \
F(cl_mem_info, CL_MEM_MAP_COUNT, cl_uint) \
F(cl_mem_info, CL_MEM_REFERENCE_COUNT, cl_uint) \
F(cl_mem_info, CL_MEM_CONTEXT, cl::Context) \
\
F(cl_image_info, CL_IMAGE_FORMAT, cl_image_format) \
F(cl_image_info, CL_IMAGE_ELEMENT_SIZE, ::size_t) \
F(cl_image_info, CL_IMAGE_ROW_PITCH, ::size_t) \
F(cl_image_info, CL_IMAGE_SLICE_PITCH, ::size_t) \
F(cl_image_info, CL_IMAGE_WIDTH, ::size_t) \
F(cl_image_info, CL_IMAGE_HEIGHT, ::size_t) \
F(cl_image_info, CL_IMAGE_DEPTH, ::size_t) \
\
F(cl_sampler_info, CL_SAMPLER_REFERENCE_COUNT, cl_uint) \
F(cl_sampler_info, CL_SAMPLER_CONTEXT, cl::Context) \
F(cl_sampler_info, CL_SAMPLER_NORMALIZED_COORDS, cl_addressing_mode) \
F(cl_sampler_info, CL_SAMPLER_ADDRESSING_MODE, cl_filter_mode) \
F(cl_sampler_info, CL_SAMPLER_FILTER_MODE, cl_bool) \
\
F(cl_program_info, CL_PROGRAM_REFERENCE_COUNT, cl_uint) \
F(cl_program_info, CL_PROGRAM_CONTEXT, cl::Context) \
F(cl_program_info, CL_PROGRAM_NUM_DEVICES, cl_uint) \
F(cl_program_info, CL_PROGRAM_DEVICES, VECTOR_CLASS<cl_device_id>) \
F(cl_program_info, CL_PROGRAM_SOURCE, STRING_CLASS) \
F(cl_program_info, CL_PROGRAM_BINARY_SIZES, VECTOR_CLASS< ::size_t>) \
F(cl_program_info, CL_PROGRAM_BINARIES, VECTOR_CLASS<char *>) \
\
F(cl_program_build_info, CL_PROGRAM_BUILD_STATUS, cl_build_status) \
F(cl_program_build_info, CL_PROGRAM_BUILD_OPTIONS, STRING_CLASS) \
F(cl_program_build_info, CL_PROGRAM_BUILD_LOG, STRING_CLASS) \
\
F(cl_kernel_info, CL_KERNEL_FUNCTION_NAME, STRING_CLASS) \
F(cl_kernel_info, CL_KERNEL_NUM_ARGS, cl_uint) \
F(cl_kernel_info, CL_KERNEL_REFERENCE_COUNT, cl_uint) \
F(cl_kernel_info, CL_KERNEL_CONTEXT, cl::Context) \
F(cl_kernel_info, CL_KERNEL_PROGRAM, cl::Program) \
\
F(cl_kernel_work_group_info, CL_KERNEL_WORK_GROUP_SIZE, ::size_t) \
F(cl_kernel_work_group_info, CL_KERNEL_COMPILE_WORK_GROUP_SIZE, cl::size_t<3>) \
F(cl_kernel_work_group_info, CL_KERNEL_LOCAL_MEM_SIZE, cl_ulong) \
\
F(cl_command_queue_info, CL_QUEUE_CONTEXT, cl::Context) \
F(cl_command_queue_info, CL_QUEUE_DEVICE, cl::Device) \
F(cl_command_queue_info, CL_QUEUE_REFERENCE_COUNT, cl_uint) \
F(cl_command_queue_info, CL_QUEUE_PROPERTIES, cl_command_queue_properties)
#if defined(CL_VERSION_1_1)
#define __PARAM_NAME_INFO_1_1(F) \
F(cl_context_info, CL_CONTEXT_NUM_DEVICES, cl_uint)\
F(cl_device_info, CL_DEVICE_PREFERRED_VECTOR_WIDTH_HALF, cl_uint) \
F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_CHAR, cl_uint) \
F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_SHORT, cl_uint) \
F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_INT, cl_uint) \
F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_LONG, cl_uint) \
F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_FLOAT, cl_uint) \
F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_DOUBLE, cl_uint) \
F(cl_device_info, CL_DEVICE_NATIVE_VECTOR_WIDTH_HALF, cl_uint) \
F(cl_device_info, CL_DEVICE_DOUBLE_FP_CONFIG, cl_device_fp_config) \
F(cl_device_info, CL_DEVICE_HALF_FP_CONFIG, cl_device_fp_config) \
F(cl_device_info, CL_DEVICE_HOST_UNIFIED_MEMORY, cl_bool) \
\
F(cl_mem_info, CL_MEM_ASSOCIATED_MEMOBJECT, cl::Memory) \
F(cl_mem_info, CL_MEM_OFFSET, ::size_t) \
\
F(cl_kernel_work_group_info, CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE, ::size_t) \
F(cl_kernel_work_group_info, CL_KERNEL_PRIVATE_MEM_SIZE, cl_ulong) \
\
F(cl_event_info, CL_EVENT_CONTEXT, cl::Context)
#endif // CL_VERSION_1_1
#if defined(USE_CL_DEVICE_FISSION)
#define __PARAM_NAME_DEVICE_FISSION(F) \
F(cl_device_info, CL_DEVICE_PARENT_DEVICE_EXT, cl_device_id) \
F(cl_device_info, CL_DEVICE_PARTITION_TYPES_EXT, VECTOR_CLASS<cl_device_partition_property_ext>) \
F(cl_device_info, CL_DEVICE_AFFINITY_DOMAINS_EXT, VECTOR_CLASS<cl_device_partition_property_ext>) \
F(cl_device_info, CL_DEVICE_REFERENCE_COUNT_EXT , cl_uint) \
F(cl_device_info, CL_DEVICE_PARTITION_STYLE_EXT, VECTOR_CLASS<cl_device_partition_property_ext>)
#endif // USE_CL_DEVICE_FISSION
template <typename enum_type, cl_int Name>
struct param_traits {};
#define __DECLARE_PARAM_TRAITS(token, param_name, T) \
struct token; \
template<> \
struct param_traits<detail:: token,param_name> \
{ \
enum { value = param_name }; \
typedef T param_type; \
};
__PARAM_NAME_INFO_1_0(__DECLARE_PARAM_TRAITS);
#if defined(CL_VERSION_1_1)
__PARAM_NAME_INFO_1_1(__DECLARE_PARAM_TRAITS);
#endif // CL_VERSION_1_1
#if defined(USE_CL_DEVICE_FISSION)
__PARAM_NAME_DEVICE_FISSION(__DECLARE_PARAM_TRAITS);
#endif // USE_CL_DEVICE_FISSION
#undef __DECLARE_PARAM_TRAITS
// Convenience functions
template <typename Func, typename T>
inline cl_int
getInfo(Func f, cl_uint name, T* param)
{
return GetInfoHelper<Func, T>::get(f, name, param);
}
template <typename Func, typename Arg0>
struct GetInfoFunctor0
{
Func f_; const Arg0& arg0_;
cl_int operator ()(
cl_uint param, ::size_t size, void* value, ::size_t* size_ret)
{ return f_(arg0_, param, size, value, size_ret); }
};
template <typename Func, typename Arg0, typename Arg1>
struct GetInfoFunctor1
{
Func f_; const Arg0& arg0_; const Arg1& arg1_;
cl_int operator ()(
cl_uint param, ::size_t size, void* value, ::size_t* size_ret)
{ return f_(arg0_, arg1_, param, size, value, size_ret); }
};
template <typename Func, typename Arg0, typename T>
inline cl_int
getInfo(Func f, const Arg0& arg0, cl_uint name, T* param)
{
GetInfoFunctor0<Func, Arg0> f0 = { f, arg0 };
return GetInfoHelper<GetInfoFunctor0<Func, Arg0>, T>
::get(f0, name, param);
}
template <typename Func, typename Arg0, typename Arg1, typename T>
inline cl_int
getInfo(Func f, const Arg0& arg0, const Arg1& arg1, cl_uint name, T* param)
{
GetInfoFunctor1<Func, Arg0, Arg1> f0 = { f, arg0, arg1 };
return GetInfoHelper<GetInfoFunctor1<Func, Arg0, Arg1>, T>
::get(f0, name, param);
}
template<typename T>
struct ReferenceHandler
{ };