Where is the buffer allocated in opencl?

痴心易碎 提交于 2019-12-08 09:31:42

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/27140457/where-is-the-buffer-allocated-in-opencl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!