我试图在 OpenCL 中使用 C++ 绑定(bind)创建一个内存缓冲区。句子看起来像
cl::Buffer buffer(context,CL_MEM_READ_ONLY,sizeof(float)*(100));
这句话让我很困惑,因为它没有指定内存分配在哪个设备上。原则上 context
包含所选平台上的所有设备,包括 cpu 和 gpu。缓冲区是否放在所有设备共享的公共(public)区域中?
最佳答案
规范没有定义内存的位置。对于 API 用户,它是“在上下文中”。
如果您只有一台设备,很可能 (99.99%) 会在设备中。 (在极少数情况下,如果设备暂时没有足够的内存,它可能在主机中)
如果有许多不同的设备,它将在创建时位于其中一个中。但它可能会透明地移动到另一个设备,具体取决于内核启动。
这就是为什么调用clEnqueueMIgrateMemObjects的原因(仅限 OpenCL 1.2)存在。 它允许用户告诉 API 一些提示,告诉 API 哪里需要内存,并提前准备拷贝。
这是它的作用的定义:
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
关于c++ - opencl中分配的缓冲区在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27140457/