osbert
osbert

Reputation: 55

How do I pass cl::Buffer object between host c++ functions?

I am using OpenCL with the C++ wrappers, using the GPU for heavy computations.

Data is in cl::Buffer objects, e.g.

std::vector<float> host_z_vec(100, 0.0f);
cl::Buffer device_z_vec;
device_z_vec = cl::Buffer(*context_ptr,
                          CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, 
                          sizeof(float)*host_z_vec.size(),   
                          &host_z_vec.at(0),
                          &status);

How should I pass the cl::Buffer object, device_z_vec, between host c++ functions? - I do not want to make copies of device_z_vec, merely pass it around.

void run_on_gpu(cl::Buffer device_z_vec){ ... }

or

void run_on_gpu(const cl::Buffer& device_z_vec){ ... }

?

And is this ok?

class StoreBuffer{
    cl::Buffer device_z_vec;
public:
    cl::Buffer provide_access_to_device_z_vec(){ return device_z_vec; };
}

or should I be passing a pointer to the cl::Buffer object?

Upvotes: 1

Views: 1015

Answers (1)

jprice
jprice

Reputation: 9925

Passing a cl::Buffer to a function by value or storing it as a class member are both fine. The copy constructor for the cl::Buffer class simply increments the reference count of the underlying OpenCL allocation (via clRetainMemObject). The same is true for all of the other OpenCL object classes in cl.hpp.

Upvotes: 3

Related Questions