clCreateBuffer and cl_mem_flags

I am doing an N Body particle project and have some questions about cl_mem_flags.

I assume cl_mem_flags are only used for optimisation?
Otherwise why would I not use CL_MEM_READ_WRITE all the time.

I assume this flag is not relevant to clEnqueueWriteBuffer, clEnqueueReadBuffer and clEnqueueCopyBuffer
i.e. you can copy to/write to a CL_MEM_READ_ONLY buffer and copy from/read from a CL_MEM_WRITE_ONLY buffer.

Is it possible to change the flags between kernel calls.
For example a kernel might take data from current Positions (CL_MEM_READ_ONLY) and write out new values to new Positions (CL_MEM_WRITE_ONLY).
Rather than clEnqueueCopyBuffer to copy new Positions to current Positions is it possible to switch the cl_mem_flags on these two and then switch the arguments around using clSetKernelArg

As it happens… I’m using a GL Vertex Buffer Object so the Positions can be directly rendered from graphics card memory.

Could I create two cl_mem objects for two vbo’s each with different cl_mem_flags?
i.e.
PositionsARead = clCreateFromGLBuffer(this->context, CL_MEM_READ_ONLY, vbo[0], &status);
PositionsAWrite = clCreateFromGLBuffer(this->context, CL_MEM_WRITE_ONLY, vbo[0], &status);

PositionsBRead = clCreateFromGLBuffer(this->context, CL_MEM_READ_ONLY, vbo[1], &status);
PositionsBWrite = clCreateFromGLBuffer(this->context, CL_MEM_WRITE_ONLY, vbo[1], &status);

And then alternate between

  1. kernel to read from PositionsARead and write to PositionsBWrite
  2. kernel to read from PositionsBRead and write to PositionsAWrite

My end goal is to have a one thread iterating OpenCL kernel calls (current to new,new to current) and have a timer event in another thread request that the OpenCL kernel also write a copy to a vbo buffer for display.

One last question is their any restrictions between the glBufferData GL flags and the cm_mem_flags
i.e. specifiying GL_STATIC_DRAW on a vbo and then creating a CL_MEM_WRITE_ONLY cl buffer to it doesn’t seem wise.

I assume cl_mem_flags are only used for optimisation?
Otherwise why would I not use CL_MEM_READ_WRITE all the time.

Correct. cl_mem_flags can be used to avoid some data copies in some cases. However, this doesn’t mean that CL_MEM_READ will always be faster than CL_MEM_READ_WRITE; sometimes it will do nothing.

I assume this flag is not relevant to clEnqueueWriteBuffer, clEnqueueReadBuffer and clEnqueueCopyBuffer

That’s right. These memory flags only affect kernels.

Is it possible to change the flags between kernel calls.

No, that’s not allowed.

Could I create two cl_mem objects for two vbo’s each with different cl_mem_flags?

You could, but it will be more difficult to use and it may not improve performance at all. I would say that if you are sometimes reading and sometimes writing to the same buffer, simply create it as READ_WRITE and leave it at that.

My end goal is to have a one thread iterating OpenCL kernel calls (current to new,new to current) and have a timer event in another thread request that the OpenCL kernel also write a copy to a vbo buffer for display.

I see. Make sure to set up event dependencies correctly so that you don’t read a buffer while it’s being written to. If you are using a single in-order queue then this serialization will be done for you automatically.

One last question is their any restrictions between the glBufferData GL flags and the cm_mem_flags
i.e. specifiying GL_STATIC_DRAW on a vbo and then creating a CL_MEM_WRITE_ONLY cl buffer to it doesn’t seem wise.

Indeed :slight_smile: