OpenCL and encapsulation

Hello

I was trying to make myself a C++ class containing (wrapping) a cl_mem buffer, a bit like what’s in the khronos C++ wrapper (but it contains also other things that i need in my software).

So here my problem is: when i want to create the buffer, i need a context, and when i want to upload/download data, i need a queue.
And both should be the same as in the rest of my software, to make everything work…

So my question: is it enough to pass to my class, in the constructor, a context and a command queue, and then call clCreateBuffer, or clEnqueueRead/Writebuffer from within my class?

and , if so, should everything work fine if i write a Get() method, returning the cl_mem item, and using it from somewhere else in my code?

i tried to do so, but everything crashes (program has stopped working).
What is the right way to do?

thanks

If you do everything by reference this should work regardless of where in memory the objects exist. If you are trying to pass by variable I am not sure. I don’t know if cl_mem is a class object with overloaded copy methods.

You also have the choice of allowing the class to own both the command queue and context, then invoke a class method to do the crunching of numbers desired.

I tried to edit that quickly, but it wouldn’t let me, sorry for the extra post. To amend what I said:

I meant, I do not know if opencl has overloaded copy methods for cl_command_queue and cl_context. (or cl_mem for that matter) To clarify the by reference memory model, check out the code:


main()
{
  cl_command_queue clCq;
  cl_context  clC;

  int parameter1;

  MyClass.MyMethod(clCq, clC, parameter1);
}

MyClass::MyMethod(cl_command_queue &cQ, cl_context &cC, int &parameter)
{
  // stuff
}


This code demonstrates pass-by-reference of the queue and context from the top level main() routine, to a call in your class. The class expects the pointers of the queue and context. If you’re writing this in C, you will need to change the code to use asterisk operators etc…

It’s not necessary to pass cl_command_queue by reference since cl_command_queue is already a pointer (see cl.h).

Yes, it should work. You should also remember to call clRetainContext() and clRetainCommandQueue() in the constructor of the class. In the same way, you should call clReleaseContext() and clReleaseCommandQueue() in the destructor. Finally, when the class hands down a memory object to somebody else, the Get() method should call clRetainMemObject().

It’s difficult to know what could be going wrong without some source code. Can you simplify the source you are using to 10 lines of code or so and post it here?

Yes, it should work. You should also remember to call clRetainContext() and clRetainCommandQueue() in the constructor of the class. In the same way, you should call clReleaseContext() and clReleaseCommandQueue() in the destructor. Finally, when the class hands down a memory object to somebody else, the Get() method should call clRetainMemObject().

It’s difficult to know what could be going wrong without some source code. Can you simplify the source you are using to 10 lines of code or so and post it here?

Thanks david, good to know this info.
(I’m still learning. I should probably research more before contributing to technical questions!)

Thank you all! i was probably doing something wrong in the constructor, or operator=

Now i modified it (and also pass everything by pointer, although context and command queues are already pointers, as david confirmed),
and things seem to work properly!