Results 1 to 8 of 8

Thread: OpenCL and encapsulation

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    6

    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

  2. #2
    Junior Member
    Join Date
    Apr 2010
    Posts
    26

    Re: OpenCL and encapsulation

    Quote Originally Posted by paoletto
    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.

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    26

    Re: OpenCL and encapsulation

    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:

    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...

  4. #4
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: OpenCL and encapsulation

    It's not necessary to pass cl_command_queue by reference since cl_command_queue is already a pointer (see cl.h).
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  5. #5
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: OpenCL and encapsulation

    Quote Originally Posted by paoletto
    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?
    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?
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  6. #6
    Senior Member
    Join Date
    May 2010
    Location
    Toronto, Canada
    Posts
    845

    Re: OpenCL and encapsulation

    Quote Originally Posted by paoletto
    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?
    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?
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

  7. #7
    Junior Member
    Join Date
    Apr 2010
    Posts
    26

    Re: OpenCL and encapsulation

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

  8. #8
    Junior Member
    Join Date
    Jul 2010
    Posts
    6

    Re: OpenCL and encapsulation

    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!

Posting Permissions

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