Page 6 of 10 FirstFirst ... 2345678910 LastLast
Results 51 to 60 of 100

Thread: OpenCL C++ Bindings

  1. #51
    Junior Member
    Join Date
    Jan 2010
    Posts
    1

    Re: OpenCL C++ Bindings

    I'm having issues compiling the C++ OpenCL bindings.

    I'm running on a MacBook Pro 2009 under WinXP with nVidia driver version 190.89.

    Compiling the following code
    Code :
    #include <cstdio>
    #include <cstdlib>
    #include "c:/dev/3rdparty/opencl/cl.hpp"
     
    int main(void)
    {
    	cl_int err = CL_SUCCESS;
    	cl::Context context(CL_DEVICE_TYPE_GPU, 0, NULL, NULL, &err);
    	cl::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();    // this line gives the error
    	return 0;
    }

    Gives me this compile error under Visual Studio 2005:
    Code :
    1>------ Build started: Project: opencltest, Configuration: Debug Win32 ------
    1>Compiling...
    1>main.cpp
    1>c:\dev\test\opencl\opencltest\opencltest\main.cpp(9) : error C2440: 'initializing' : cannot convert from 'cl::detail::param_traits<cl::detail::cl_context_info,4225>::param_type' to 'cl::vector<T>'
    1>        with
    1>        [
    1>            T=cl::Device
    1>        ]
    1>        No constructor could take the source type, or constructor overload resolution was ambiguous
    1>Build log was saved at "file://c:\dev\test\opencl\opencltest\opencltest\Debug\BuildLog.htm"
    1>opencltest - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #52
    Junior Member
    Join Date
    Nov 2009
    Posts
    12

    Re: OpenCL C++ Bindings

    Hi there,

    I'm trying to work out how to use the sampler from the c++ bindings however there is no constructor given for the class where I was expecting something similar to the current openCL implementation which is like:

    clCreateSampler(cxGPUContext, true, CL_ADDRESS_REPEAT, CL_FILTER_LINEAR, &ciErrNum);

    Am I missing something in how to properly construct the cl::Sampler object? Thanks in advance.

  3. #53
    Junior Member
    Join Date
    Oct 2009
    Posts
    15

    Re: OpenCL C++ Bindings

    Hey Ricka,

    I'm not sure, but I think you have to define
    Code :
    #define __NO_STD_VECTOR
    before including cl.hpp to use cl::vector. But I can't see, the reason, why the compiler doesn't confirm the missing of cl::vector

  4. #54
    Junior Member
    Join Date
    Mar 2010
    Posts
    3

    Re: OpenCL C++ Bindings

    Quote Originally Posted by louiswu
    clCreateSampler(cxGPUContext, true, CL_ADDRESS_REPEAT, CL_FILTER_LINEAR, &ciErrNum);

    Am I missing something in how to properly construct the cl::Sampler object? Thanks in advance.
    Yesterday I started to learn OpenCL and stumbled on this C++ binding which seems to be the easy way of doing OpenCL programming.

    It seems to me that the Sampler class is incomplete there should be a constructor with cl_addressing_mode and cl_filter_mode parameters or maybe even a Context member function:
    Code :
    Sampler Context::getSampler(cl_addressing_mode a, cl_filter_mode m);

    It is possible to use the Sampler by initializing the Wrapper with a C API call like this
    Code :
    cl::Sampler linear;
    linear() = clCreateSampler(context(), CL_TRUE, CL_ADDRESS_REPEAT, CL_FILTER_LINEAR, &err);

  5. #55
    Junior Member
    Join Date
    Mar 2010
    Posts
    3

    Re: OpenCL C++ Bindings

    Hi,

    Should it be possible to convert from cl::NDRange to cl::size_t<3> ?

    I tried
    cl::size_t<3> size = cl::NDRange(64,64,64);

    Compiling this code with MSVC2008 produced this error
    error C2440: 'initializing' : cannot convert from 'cl::NDRange' to 'cl::size_t<N>'
    with
    [
    N=3
    ]
    No constructor could take the source type, or constructor overload resolution was ambiguous

    I am using std::vectors so __NO_STD_VECTOR is NOT defined.

    it would just have been so convenient to use
    enqueueMapImage(image, CL_TRUE, CL_MAP_WRITE, NDRange(0,0,0), NDRange(64,64,64), 0, 0);

    wouldn't it even be appropriate for enqueueMapImage function (and others) to use the NDRange for offset and size parameter?

  6. #56

    Re: OpenCL C++ Bindings

    Quote Originally Posted by henkkas
    Hi,
    Should it be possible to convert from cl::NDRange to cl::size_t<3> ?
    You can convert from cl::NDRange to size_t *. However, I would disagree that there should be an implicit conversion operator to go to cl::size_t<3>. This allows breaking some of the type safety afforded by using a cl::size_t<3>. For example, if we were to allow the conversion and if the 3rd dimension was accidentally left out like the following:

    Code :
    enqueueMapImage(image, CL_TRUE, CL_MAP_WRITE, NDRange(0,0), NDRange(64,64,64), 0, 0);

    The compiler would allow this, and due to how memory allocators usually allocate memory, the program would probably run fine. Worse yet, memory is usually zero, so the program would crash only sometimes when the 3rd spot in memory occasionally wasn't zero.

    A better option may be specializing the cl::size_t<3> class constructor to allow the syntax you want
    Code :
    template <int N>
    struct size_t : public cl::vector< ::size_t, N> { };
     
    template <>
    struct size_t<3> : public cl::vector< ::size_t, 3> 
    { 
      size_t(size_t x, size_t y, size_t z)
      {
        push_back(x);
        push_back(y);
        push_back(z);
      }
    };

    Then the same ease of use can be achieved.

    Code :
    enqueueMapImage(image, CL_TRUE, CL_MAP_WRITE, size_t<3>(0,0,0), size_t<3>(64,64,64), 0, 0);

    And the compiler would quickly throw an error if this occurred:

    Code :
    enqueueMapImage(image, CL_TRUE, CL_MAP_WRITE, size_t<3>(0,0), size_t<3>(64,64,64), 0, 0);

  7. #57
    Junior Member
    Join Date
    Apr 2009
    Posts
    27

    Re: OpenCL C++ Bindings

    I have uploaded a new version of the cl.hpp which addresses many of the issues discussed here. Please try them out and let me know how they go.

  8. #58
    Junior Member
    Join Date
    Aug 2009
    Posts
    9

    Re: OpenCL C++ Bindings

    cl::CommandQueue::getInfo<CL_QUEUE_CONTEXT>() doesn't increase the reference count of the cl::Context object returned by this method. This results in a program crash when the destructur is called for the unreferenced instance. See http://www.khronos.org/bugzilla/show_bug.cgi?id=296 for details and a piece of code that demonstrates this problem.

    Since I learned that this is a known (and non-trivial) issue, I would like to discuss how this could be addressed. Are there any proposals for a fix?

    Thanks & kind regards,
    Markus

  9. #59

    Re: OpenCL C++ Bindings

    Quote Originally Posted by grabner
    cl::CommandQueue::getInfo<CL_QUEUE_CONTEXT>() doesn't increase the reference count of the cl::Context object returned by this method. This results in a program crash when the destructur is called for the unreferenced instance. See http://www.khronos.org/bugzilla/show_bug.cgi?id=296 for details and a piece of code that demonstrates this problem.

    Since I learned that this is a known (and non-trivial) issue, I would like to discuss how this could be addressed. Are there any proposals for a fix?

    Thanks & kind regards,
    Markus
    I've done the following in my local copy:
    Code :
    namespace detail {
    // Specialized for cl_context
    template <typename Func>
    struct GetInfoHelper<Func, cl::Context>
    {
        static cl_int
        get(Func f, cl_uint name, cl::Context* param)
        {
          cl_uint err = f(name, sizeof(cl::Context), param, NULL);
          if (err != CL_SUCCESS) {
            return err;
          }
     
          return ReferenceHandler<cl::Context::cl_type>::retain(param->object_);
        }
    };
    }

    The same needs to be done for all the types derived from cl::detail::Wrapper. Unfortunately, C++ isn't clever enough to specialize on a generic base class to effect all derived class (like Java generics do). So the above code needs to be duplicated for every OpenCL type. Not too bad since it can be done pseudo-manually with preprocessor macros.

    The other tricky part is it needs cl::detail::Wrapper<T>:bject_ to be a public object. This is a hack. In reality what is needed is getters for the cl_* type inside the object. Something like the following:

    Code :
    T cl::detail::Wrapper<T>::getCLObject() { return object_; }

    I'm planning on submitting the above patches once my company is accepted as a Khronos member and I'm granted SVN access. So what do you think of the fix? Can you think of improvements?

  10. #60
    Senior Member
    Join Date
    Nov 2009
    Posts
    118

    Re: OpenCL C++ Bindings

    In my opencl c++ layer there is a generic retain/release system.

    I have these base classes :
    Code :
    namespace cl
    {
    	namespace base
    	{
    		/** Object wich contain a cl type instance. */
    		template<typename CLType_>
    		class CLObject
    		{
    		public:
    			typedef CLType_ CLType;
     
    			static CLType const & GetObjectCLInstance(CLObject<CLType_> const & object);
    			CLObject & operator = (CLObject const & copy);
     
    	//		bool operator == (CLObject<CLType_> const & comp) const;
    			bool CLEquals(CLObject<CLType_> const & comp) const;
     
    		protected:
    			typedef CLObject<CLType_> CLObjectBase;
     
    			CLObject();
    			CLObject(CLObject const & copy);
    			explicit CLObject(CLType const & copy);
     
    			template<typename OtherCLType>
    			static OtherCLType const & GetCLInstance(CLObject<OtherCLType> const & object);
     
    			CLType clInstance_;
    		};
     
    		/** Object wich can be retained and released from opencl. */
    		template<typename ChildType, typename CLType>
    		class CLRefObject : public CLObject<CLType>
    		{
    		public:
    			CLRefObject();
    			~CLRefObject();
    			CLRefObject(CLRefObject const & copy);
    			explicit CLRefObject(CLType const & copy);
    			CLRefObject & operator = (CLRefObject const & copy);
     
    		protected:
    			typedef CLRefObject<ChildType, CLType> CLRefObjectBase;
    		};
     
    		/** Object wich can be instanciated from a cl type instance. */
    		template<typename ChildType, typename CLType>
    		class InstanciableFromCLObject
    		{
    		public:
    			typedef ChildType APIType;
     
    			static APIType CreateObjectFromCLInstance(CLType const & clInstance);
     
    		protected:
    			typedef InstanciableFromCLObject<ChildType, CLType> InstanciableFromCLObjectBase;
     
    			InstanciableFromCLObject();
    			InstanciableFromCLObject(InstanciableFromCLObject const &);
    		};
     
    		/** Compound object from CLObject and InstanciableFromCLObject. */
    		template<typename ChildType, typename CLType>
    		class CLObjectInstanciableFromCLObject : public CLObject<CLType>, public base::InstanciableFromCLObject<ChildType, CLType>
    		{
    		protected:
    			typedef CLObjectInstanciableFromCLObject<ChildType, CLType> CLObjectInstanciableFromCLObjectBase;
     
    			CLObjectInstanciableFromCLObject();
    			CLObjectInstanciableFromCLObject(CLObjectInstanciableFromCLObject const & copy);
    			explicit CLObjectInstanciableFromCLObject(CLType const & copy);
    		};
     
    		/** Compound object from CLRefObject and InstanciableFromCLObject. */
    		template<typename ChildType, typename CLType>
    		class CLRefObjectInstanciableFromCLObject : public base::CLRefObject<ChildType, CLType>, public base::InstanciableFromCLObject<ChildType, CLType>
    		{
    		protected:
    			typedef CLRefObjectInstanciableFromCLObject<ChildType, CLType> CLRefObjectInstanciableFromCLObjectBase;
     
    			CLRefObjectInstanciableFromCLObject();
    			CLRefObjectInstanciableFromCLObject(CLRefObjectInstanciableFromCLObject const & copy);
    			explicit CLRefObjectInstanciableFromCLObject(CLType const & copy);
    		};
     
    	} // namespace base
     
    } //namespace cl

    Where CLRefObject is responsible for retain/release system :
    Code :
    //CLRefObject
    		template<typename ChildType, typename CLType>
    		CLRefObject<ChildType, CLType>::~CLRefObject()
    		{
    			if(this->clInstance_ != NULL)
    			{
    				detail::error::manage(
    					ChildType::ReleaseCLObject(this->clInstance_),
    					"clRelease*"
    				);
    			}
    		}
     
    		template<typename ChildType, typename CLType>
    		CLRefObject<ChildType, CLType>::CLRefObject(CLRefObject const & copy)
    		: CLObject<CLType>(copy)
    		{
    			detail::error::manage(
    				ChildType::RetainCLObject(this->clInstance_),
    				"clRetain*"
    			);
    		}
     
    		template<typename ChildType, typename CLType>
    		CLRefObject<ChildType, CLType>::CLRefObject(CLType const & copy)
    		: CLObject<CLType>(copy)
    		{
    			detail::error::manage(
    				ChildType::RetainCLObject(this->clInstance_),
    				"clRetain*"
    			);
    		}
     
    		template<typename ChildType, typename CLType>
    		CLRefObject<ChildType, CLType> & CLRefObject<ChildType, CLType>::operator = (CLRefObject const & copy)
    		{
    			detail::error::manage(
    				ChildType::ReleaseCLObject(this->clInstance_),
    				"clRelease*"
    			);
     
    			CLObject<CLType>::operator = (copy);
     
    			detail::error::manage(
    				ChildType::RetainCLObject(this->clInstance_),
    				"clRetain*"
    			);
     
    			return *this;
    		}

    Each CLRefObject child have to define RetainCLObject and ReleaseCLObject function (example with context):
    Code :
    cl_int Context::RetainCLObject(cl_context const & clInstance)
    	{
    		return clRetainContext(clInstance);
    	}
     
    	cl_int Context::ReleaseCLObject(cl_context const & clInstance)
    	{
    		return clReleaseContext(clInstance);
    	}

    And InstanciableFromCLObject provide API to create a "c++ opencl object" from a "cl_object" :
    Code :
    	//InstanciableFromCLObject
    		template<typename ChildType, typename CLType>
    		ChildType InstanciableFromCLObject<ChildType, CLType>::CreateObjectFromCLInstance(CLType const & clInstance)
    		{
    			return APIType(clInstance);
    		}

    And for example Context class inherit from CLRefObjectInstanciableFromCLObject (wich have the api of CLRefObject and InstanciableFromCLObject) :
    Code :
    class OPENCL_CPP_WINDLL Context : public base::CLRefObjectInstanciableFromCLObject<Context, cl_context>
    {

    And so when we call GetContext on a commandqueue :
    Code :
    	Context CommandQueue::GetContext() const
    	{
    		cl_context clContext;
    		GetInfo<CONTEXT>(clContext);
    		return Context::CreateObjectFromCLInstance( clContext );
    	}

    It automatically create a Context from a cl_context, with CreateObjectFromCLInstance wich call the CLRefObject constructor and so retain object.

Page 6 of 10 FirstFirst ... 2345678910 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
  •