Question about cl_mem in OpenCL

smuggledPancakes picture smuggledPancakes · Oct 11, 2010 · Viewed 11.3k times · Source

I have been using cl_mem in some of my OpenCL boilerplate code, but I have been using it through context and not a sharp understanding of what exactly it is. I have been using it as a type for the memory I push on and off the board, which has so far been floats. I tried looking at the OpenCL docs, but cl_mem doesn't show up (does it?). Is there any documentation on it, or is it simple and can someone explain.

Answer

grrussel picture grrussel · Oct 13, 2010

The cl_mem type is a handle to a "Memory Object" (as described in Section 3.5 of the OpenCL 1.1 Spec). These essentially are inputs and outputs for OpenCL kernels, and are returned from OpenCL API calls in host code such as clCreateBuffer

cl_mem  clCreateBuffer (cl_context context, cl_mem_flags flags,
                            size_t size, void *host_ptr, cl_int *errcode_ret) 

The memory areas represented can be permitted different access patterns e.g. Read Only, or be allocated in different memory regions, depending on the flags set in the create buffer calls.

The handle is typically stored to allow a later call to release the memory, e.g:

cl_int  clReleaseMemObject (cl_mem memobj)  

In short, it provides an abstraction over where the memory actually is: you can copy data into the associated memory or back out via the OpenCL APIs clEnqueueWriteBuffer and clEnqueueReadBuffer, but the OpenCL implementation can allocate the space where it wants.