openclstandards

Do I need to set a CL_MEM_READ_WRITE when creating a buffer?


When creating a buffer in OpenCL, one passes a flags bitfield. One of these possible flags is CL_MEM_READ_WRITE, being the lowest bit in the field (value 1 << 0). Its documentation says that "it is the default".

That means I could pass either 0 or 1 in the lowest bit, with 0 in the next two bits (i.e. not set CL_MEM_WRITE_ONLY nor CL_MEM_READ_ONLY) - and supposedly, the result in both cases is the same - a read-and-write buffer.

Is that really how clCreateBuffer() is supposed to behave? And if it is - why does this flag exist?


Solution

  • The wording for CL_MEM_READ_WRITE being "the default" in the memory flags table is unclear. I opened an issue on the Github repository for OpenCL specification. According to the reply by Ben Ashbaugh, the intended meaning is that CL_MEM_READ_WRITE is the default behaviour among the group of non-host CL_MEM_* flags:

    ...

    Here's my mental model:

    If none of CL_MEM_READ_WRITE, CL_MEM_READ_ONLY, or CL_MEM_WRITE_ONLY are included in the flags, then the memory object is both readable and writeable in a kernel, so it is essentially the same as it would have been if the flags included CL_MEM_READ_WRITE. This is true both when flags is zero and when some other bits are set. ...

    Note that while clCreateBuffer and most other functions refer to one table of memory flags (contained within the description of clCreateBuffer and the one you referred to), clSVMAlloc refers to a separate table, which has the same problem.