0

I was trying to create a memory buffer in OpenCL with C++ binding. The sentence looks like

cl::Buffer buffer(context,CL_MEM_READ_ONLY,sizeof(float)*(100));

This sentence confuses me because it doesn't specify which device the memory is allocated on. In principle context contains all devices, including cpu and gpu, on the chosen platform. Is it true that the buffer is put in a common region shared by all the devices?

andy90
  • 525
  • 5
  • 19

1 Answers1

6

The spec does not define where the memory is. For the API user, it is "in the context".

If you have one device only, probably (99.99%) is going to be in the device. (In rare cases it may be in the host if the device does not have enough memory for the time being)

In case of many different devices, it will be in one of them at the creation. But it may move transparently to another device depending on the kernel launches.

This is the reason why the call clEnqueueMIgrateMemObjects (OpenCL 1.2 only) exists. It allows the user to tell some hints to the API about where the memory will be needed, and prepare the copy in advance.

Here is the definition of what it does:

clEnqueueMIgrateMemObjects provides a mechanism for assigning which device an OpenCL memory object resides. A user may wish to have more explicit control over the location of their memory objects on creation. This could be used to:

  • Ensure that an object is allocated on a specific device prior to usage.
  • Preemptively migrate an object from one device to another.

Typically, memory objects are implicitly migrated to a device for which enqueued commands, using the memory object, are targeted

Community
  • 1
  • 1
DarkZeros
  • 8,235
  • 1
  • 26
  • 36