Page 5 of 10 FirstFirst 123456789 ... LastLast
Results 41 to 50 of 100

Thread: OpenCL C++ Bindings

  1. #41
    Junior Member
    Join Date
    Apr 2009
    Posts
    27

    Re: OpenCL C++ Bindings

    I have not tested this yet, getting late in Austin, but would something like this work instead:

    template <typename T, int N>
    struct KernelArgumentHandler<T[N]>
    {
    static ::size_t size(const T value[N]) { return sizeof(T[N]); }
    static T* ptr(T value[N]) { return &value; }
    };

    Note that the references have to be removed as C++ does not allow arrays to be passed by reference but this should not be an issue.

    You can now do:

    cl_int2 v = {0,1};
    setArg(n, v);

  2. #42
    Junior Member
    Join Date
    Dec 2009
    Posts
    5

    Re: OpenCL C++ Bindings

    You are right this is a much shorter way to approach it. Here is a new diff:
    Code :
    --- cl_orig.hpp	Thu Dec 17 16:21:25 2009
    +++ cl.hpp	Thu Dec 17 17:59:12 2009
    @@ -2990,10 +2991,17 @@
     template <typename T>
     struct KernelArgumentHandler
     {
    -    static ::size_t size(const T& value) { return sizeof(T); }
    -    static T* ptr(T& value) { return &value; }
    +    static ::size_t size(const T& value)   { return sizeof(T); }
    +    static void const* ptr(T const& value) { return &value;    }
     };
     
    +template <typename T, int N>
    +struct KernelArgumentHandler< T[N] >
    +{
    +    static ::size_t size(const T value[N])   { return sizeof(T[N]); }
    +    static void const* ptr(const T value[N]) { return value;        }
    +};
    +
     template <>
     struct KernelArgumentHandler<LocalSpaceArg>
     {
    @@ -3225,7 +3233,7 @@
          * generated.
          */
         template <typename T>
    -    cl_int setArg(cl_uint index, T value)
    +    cl_int setArg(cl_uint index, T const & value)
         {
             return detail::errHandler(
                 ::clSetKernelArg(

    I still think KernelArgumentHandler:tr should take a cont reference and return a const void* . And setArg should take const reference also, just like KernelFunctor:perator().

    I am sure it is just typo, but just in case: ptr() in your example should return value and not its address.

  3. #43
    Junior Member
    Join Date
    Apr 2009
    Posts
    27

    Re: OpenCL C++ Bindings

    Thinking about this some more I'm not sure this makes sense.

    I agree that it will work with

    template <typename T, int N>
    struct KernelArgumentHandler<T[N]>
    {
    static ::size_t size(const T value[N]) { return sizeof(T[N]); }
    static T* ptr(T value[N]) { return value; }
    };

    But my thoughts are that this is not a bug in the C++ bindings but in the particular implementation of cl_int2. It is not valid to pass an array as an argument to an OpenCL kernel, it must be a base type or a struct/union. This leads me to wonder how the cl_int2 is defined, can you post it's definition?

    I think you are correct about the const I'll check tomorrow.

  4. #44

    Re: OpenCL C++ Bindings

    This seems related to an annoyance with OpenCL vector types in CPU code I've always had. The following code won't even compile:

    Code :
    #include <vector>
     
    #include "cl.hpp"
     
    int main(int, char *[])
    {
      std::vector<cl_int2> vector;
     
      cl_int2 vtype = {1, 2};
      vector.push_back(vtype);
     
      return 0;
    }

    I get the following error message with gcc 4.2.1 on Snow Leopard:

    coleb@shark~/oecl$ make CLVectorTypesAnnoyance
    /usr/bin/c++ -g -W -Wall -pedantic -O3 -Wno-long-long -ffast-math -arch x86_64 -msse3 -I./openeye/toolkits/include -I. -I./include_private -c CLVectorTypesAnnoyance.cpp
    In file included from CLVectorTypesAnnoyance.cpp:3:
    cl.hpp:1165: warning: unused parameter ‘errStr’
    /usr/include/c++/4.2.1/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = int32_t [2]]’:
    /usr/include/c++/4.2.1/bits/stl_vector.h:604: instantiated from ‘void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp = int32_t [2], _Alloc = std::allocator<int32_t [2]>]’
    CLVectorTypesAnnoyance.cpp:10: instantiated from here
    /usr/include/c++/4.2.1/ext/new_allocator.h:107: error: ISO C++ forbids initialization in array new
    /usr/include/c++/4.2.1/bits/vector.tcc: In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type:ointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = int32_t [2], _Alloc = std::allocator<int32_t [2]>]’:
    /usr/include/c++/4.2.1/bits/stl_vector.h:608: instantiated from ‘void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp = int32_t [2], _Alloc = std::allocator<int32_t [2]>]’
    CLVectorTypesAnnoyance.cpp:10: instantiated from here
    /usr/include/c++/4.2.1/bits/vector.tcc:252: error: array must be initialized with a brace-enclosed initializer
    /usr/include/c++/4.2.1/bits/stl_vector.h:608: instantiated from ‘void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp = int32_t [2], _Alloc = std::allocator<int32_t [2]>]’
    CLVectorTypesAnnoyance.cpp:10: instantiated from here
    /usr/include/c++/4.2.1/bits/vector.tcc:256: error: invalid array assignment
    /usr/include/c++/4.2.1/bits/stl_algobase.h: In static member function ‘static _BI2 std::__copy_backward<_BoolType, std::random_access_iterator_tag>::__copy_b(_BI1, _BI1, _BI2) [with _BI1 = int32_t (*)[2], _BI2 = int32_t (*)[2], bool _BoolType = false]’:
    /usr/include/c++/4.2.1/bits/stl_algobase.h:465: instantiated from ‘_BI2 std::__copy_backward_aux(_BI1, _BI1, _BI2) [with _BI1 = int32_t (*)[2], _BI2 = int32_t (*)[2]]’
    /usr/include/c++/4.2.1/bits/stl_algobase.h:474: instantiated from ‘static _BI2 std::__copy_backward_normal<<anonymous>, <anonymous> >::__copy_b_n(_BI1, _BI1, _BI2) [with _BI1 = int32_t (*)[2], _BI2 = int32_t (*)[2], bool <anonymous> = false, bool <anonymous> = false]’
    /usr/include/c++/4.2.1/bits/stl_algobase.h:540: instantiated from ‘_BI2 std::copy_backward(_BI1, _BI1, _BI2) [with _BI1 = int32_t (*)[2], _BI2 = int32_t (*)[2]]’
    /usr/include/c++/4.2.1/bits/vector.tcc:253: instantiated from ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type:ointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = int32_t [2], _Alloc = std::allocator<int32_t [2]>]’
    /usr/include/c++/4.2.1/bits/stl_vector.h:608: instantiated from ‘void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp = int32_t [2], _Alloc = std::allocator<int32_t [2]>]’
    CLVectorTypesAnnoyance.cpp:10: instantiated from here
    /usr/include/c++/4.2.1/bits/stl_algobase.h:433: error: invalid array assignment
    /usr/include/c++/4.2.1/bits/stl_construct.h: In function ‘void std::_Construct(_T1*, const _T2&) [with _T1 = int32_t [2], _T2 = int32_t [2]]’:
    /usr/include/c++/4.2.1/bits/stl_uninitialized.h:87: instantiated from ‘_ForwardIterator std::__uninitialized_copy_aux(_InputIterator, _InputIterator, _ForwardIterator, std::__false_type) [with _InputIterator = int32_t (*)[2], _ForwardIterator = int32_t (*)[2]]’
    /usr/include/c++/4.2.1/bits/stl_uninitialized.h:114: instantiated from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = int32_t (*)[2], _ForwardIterator = int32_t (*)[2]]’
    /usr/include/c++/4.2.1/bits/stl_uninitialized.h:254: instantiated from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>) [with _InputIterator = int32_t (*)[2], _ForwardIterator = int32_t (*)[2], _Tp = int32_t [2]]’
    /usr/include/c++/4.2.1/bits/vector.tcc:275: instantiated from ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type:ointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = int32_t [2], _Alloc = std::allocator<int32_t [2]>]’
    /usr/include/c++/4.2.1/bits/stl_vector.h:608: instantiated from ‘void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp = int32_t [2], _Alloc = std::allocator<int32_t [2]>]’
    CLVectorTypesAnnoyance.cpp:10: instantiated from here
    /usr/include/c++/4.2.1/bits/stl_construct.h:81: error: ISO C++ forbids initialization in array new
    make: *** [CLVectorTypesAnnoyance.o] Error 1
    Basically, the vector types are interpreted as arrays, which can only be used in certain very restricted ways. If I had my way I would have defined them as fields in structs:
    Code :
    typedef struct _cl_int2 {
      cl_int x;
      cl_int y;
    } cl_int2;

    Unfortunately, this would break backwards compatibility. It could be done in C++ without breaking source level compatibility since we could define operator[] on the struct.

  5. #45
    Junior Member
    Join Date
    Apr 2009
    Posts
    27

    Re: OpenCL C++ Bindings

    This is actually being worked on at Khronos and there is a new cl_platform.h that resolves both issues discussed here, i.e. vector types will not be defined as simple arrays and this will fix the issue with copy constructors and also the problem here with array types in setArg.

    So I propose we hold on this until this appears publicly, which will be very soon, and I can tell you that both of these examples work without change on this new cl_platform.h. Of course, when this will be released as part of a particular vendors implementation is up to them and you will need to take this up there.

  6. #46
    Junior Member
    Join Date
    Dec 2009
    Posts
    5

    Re: OpenCL C++ Bindings

    It is true that "typedef T TN[N];" are a bit of Frankentypes: not really a pointer not really a value, and they could be annoying. But they are also very handy in some circumstances. I am not sure what changes are planned for cl_platform.h, but I am certain that won't be without a compromise.

    As far as vendors are concerned they only see "const void*" and "size_t". All they should care about is that pointer is of appropriate alignment and the size matches that of the kernel argument. I guess I will have to use my own type in the meantime. This will do the trick for cl_float2 for example
    Code :
    namespace cl{ typedef struct _float2{ cl_float2 v;} float2;}

  7. #47
    Junior Member
    Join Date
    Dec 2009
    Posts
    5

    Re: OpenCL C++ Bindings

    Hello again. I am writing some convenience methods that wrap my data structures to be used with opencl via C++ bindings and I am experiencing some const correctness issues. I believe methods

    cl::CommandQueue::enqueueWriteBuffer
    cl::CommandQueue::enqueueWriteImage

    should have
    void const * ptr, not void * ptr. The underlying methods they use have const qualifiers for that variable.

    Sorry if it has been raised already.

  8. #48

    Re: OpenCL C++ Bindings

    You are correct, I noticed that the other day as well.

    Thanks,
    Brian

  9. #49
    Junior Member
    Join Date
    Oct 2009
    Posts
    15

    Re: OpenCL C++ Bindings

    Are there some news according to the GL-Context-Sharing extension?

  10. #50
    Junior Member
    Join Date
    Jan 2010
    Posts
    1

    Re: OpenCL C++ Bindings

    Hi.

    I have just downloaded rev 45 of the c++ bindings.
    I'm compiling on mac os x 10.6.2 with gcc 4.2 from xcode.

    I was just trying to find my way around the openCL so I stared with some basic basic programs but when I tried
    Code :
    #include <utility>
    #define __CL_ENABLE_EXCEPTIONS
    #define __NO_STD_VECTOR // Use cl::vector and cl::string and 
    #define __NO_STD_STRING  // not STL versions, more on this later
    #include "cl.hpp"
     
    int main(int argc, char** argv) {
    const char * helloStr  = "__kernel void "
                                 "hello(void) "
                                 "{ "
                                 "  "
                                 "} ";
     
     
      cl_int err = CL_SUCCESS;
      try {
        cl::Context context(CL_DEVICE_TYPE_CPU, 0, NULL, NULL, &err); 
     
        cl::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::CommandQueue queue(context, devices[0], 0, &err);
        cl::KernelFunctor func = kernel.bind(queue, cl::NDRange(4, 4),cl::NDRange(2, 2));
     
        func().wait();
      }
      catch (cl::Error& err) {
        std::cerr 
        << "ERROR: "
        << err.what()
        << "("
        << err.err()
        << ")"
        << std::endl;
      }
     
      return EXIT_SUCCESS;
    }

    first I hade to fix:
    Code :
    typedef VECTOR_CLASS< std::pair<const void*, ::size_t> > Binaries;
      typedef VECTOR_CLASS< std::pair<const char*, ::size_t> > Sources;
    to
    Code :
    typedef VECTOR_CLASS< ::std::pair<const void*, ::size_t> > Binaries;
      typedef VECTOR_CLASS< ::std::pair<const char*, ::size_t> > Sources;

    but now I get:
    Build xCodeOpenGl of project xCodeOpenGl with configuration Debug

    CompileC build/xCodeOpenGl.build/Debug/xCodeOpenGl.build/Objects-normal/x86_64/main.o main.cpp normal x86_64 c++ com.apple.compilers.gcc.4_2
    cd /Users/eske/Documents/xCodeOpenGl
    setenv LANG en_US.US-ASCII
    /Developer/usr/bin/gcc-4.2 -x c++ -arch x86_64 -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -D_GLIBCXX_DEBUG=1 -D_GLIBCXX_DEBUG_PEDANTIC=1 -isysroot /Developer/SDKs/MacOSX10.6.sdk -mfix-and-continue -fvisibility-inlines-hidden -mmacosx-version-min=10.6 -gdwarf-2 -iquote /Users/eske/Documents/xCodeOpenGl/build/xCodeOpenGl.build/Debug/xCodeOpenGl.build/xCodeOpenGl-generated-files.hmap -I/Users/eske/Documents/xCodeOpenGl/build/xCodeOpenGl.build/Debug/xCodeOpenGl.build/xCodeOpenGl-own-target-headers.hmap -I/Users/eske/Documents/xCodeOpenGl/build/xCodeOpenGl.build/Debug/xCodeOpenGl.build/xCodeOpenGl-all-target-headers.hmap -iquote /Users/eske/Documents/xCodeOpenGl/build/xCodeOpenGl.build/Debug/xCodeOpenGl.build/xCodeOpenGl-project-headers.hmap -F/Users/eske/Documents/xCodeOpenGl/build/Debug -I/Users/eske/Documents/xCodeOpenGl/build/Debug/include -I/Users/eske/Documents/xCodeOpenGl/build/xCodeOpenGl.build/Debug/xCodeOpenGl.build/DerivedSources/x86_64 -I/Users/eske/Documents/xCodeOpenGl/build/xCodeOpenGl.build/Debug/xCodeOpenGl.build/DerivedSources -c /Users/eske/Documents/xCodeOpenGl/main.cpp -o /Users/eske/Documents/xCodeOpenGl/build/xCodeOpenGl.build/Debug/xCodeOpenGl.build/Objects-normal/x86_64/main.o

    In file included from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/debug/formatter.h:37,
    from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/debug/debug.h:141,
    from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_algobase.h:78,
    from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/vector:66,
    from /Users/eske/Documents/xCodeOpenGl/main.cpp:7:
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/typeinfo:144: error: expected class-name before '{' token
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/typeinfo:158: error: expected class-name before '{' token
    In file included from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/new_allocator.h:37,
    from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/i686-apple-darwin10/x86_64/bits/c++allocator.h:39,
    from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/allocator.h:53,
    from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/vector:67,
    from /Users/eske/Documents/xCodeOpenGl/main.cpp:7:
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/new:59: error: expected class-name before '{' token
    In file included from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/debug/safe_base.h:38,
    from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/debug/safe_sequence.h:41,
    from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/debug/vector:40,
    from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/vector:78,
    from /Users/eske/Documents/xCodeOpenGl/main.cpp:7:
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/concurrence.h:71: error: expected class-name before '{' token
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/concurrence.h:79: error: expected class-name before '{' token
    In file included from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ios:48,
    from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ostream:45,
    from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/iostream:45,
    from /Users/eske/Documents/xCodeOpenGl/main.cpp:9:
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/ios_base.h:208: error: expected class-name before '{' token
    In file included from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/iostream:45,
    from /Users/eske/Documents/xCodeOpenGl/main.cpp:9:
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ostream: In destructor 'std::basic_ostream<_CharT, _Traits>::sentry::~sentry()':
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ostream:415: error: there are no arguments to 'uncaught_exception' that depend on a template parameter, so a declaration of 'uncaught_exception' must be available
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ostream:415: error: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ostream: In destructor 'std::basic_ostream<_CharT, _Traits>::sentry::~sentry() [with _CharT = char, _Traits = std::char_traits<char>]':
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/ostream_insert.h:84: instantiated from 'std::basic_ostream<_CharT, _Traits>& std::__ostream_insert(std::basic_ostream<_CharT, _Traits>&, const _CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ostream:517: instantiated from 'std::basic_ostream<char, _Traits>& std:perator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>]'
    /Users/eske/Documents/xCodeOpenGl/main.cpp:34: instantiated from here
    /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ostream:415: error: 'uncaught_exception' was not declared in this scope

    is this a know bug? I tried to search the Bugzilla but found nothing.

    thanks,
    eske

Page 5 of 10 FirstFirst 123456789 ... LastLast

Similar Threads

  1. PyOpenCL: OpenCL Python Bindings
    By inducer77 in forum OpenCL
    Replies: 2
    Last Post: 11-03-2011, 05:46 AM
  2. OpenCL C# bindings
    By The Fiddler in forum OpenCL
    Replies: 1
    Last Post: 08-11-2009, 03:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •