l'arbre
l'arbre

Reputation: 729

Difference between cl_context & cl::Context

I was trying to create a Buffer for my OpenCL program. However, the function

clCreateBuffer

expects a cl_context instead of the one I am using, which is cl::Context. What ways are there around this, and or what am I doing wrong here?

Upvotes: 0

Views: 2239

Answers (2)

Martin Zabel
Martin Zabel

Reputation: 3659

You are mixing the OpenCL C API (clCreateBuffer) with the C++ API (cl::Context). Don't do this and stick to either C or C++.

If you already have a cl::Context, then you should stick with the C++ API. The corresponding call to create a buffer, e.g., for 100 floats would be:

cl::Context context(...); // your context creation
cl_int err;
cl::Buffer my_buffer(context, CL_MEM_READ_WRITE, sizeof(cl_float)*100, NULL, &err); 
if (err != CL_SUCCESS) {
    std::cerr << "ERROR: create buffer (" << err << ")" << std::endl;
    exit(1);
}

Upvotes: 3

Dithermaster
Dithermaster

Reputation: 6343

cl_context is the low-level type of an OpenCL context (from cl.h), whereas cl::Context is from the OpenCL C++ wrapper (cl.hpp). To get the cl_context from an object of type cl::Context use operator(). For example if your context variable was "foo", to pass it to clCreateBuffer use clCreateBuffer(foo(), flags, ...).

I have found you are better off using either the C API or the C++ wrappers but not both together because it gets confusing and/or tedious to convert between them. Also beware the reference counting when constructing C++ wrappers from the low-level types.

Upvotes: 3

Related Questions