gpt4 book ai didi

OpenCL:通过 CL_MEM_USE_HOST_PTR 传递给内核的变量的设备/主机内存一致性

转载 作者:行者123 更新时间:2023-12-02 06:39:32 29 4
gpt4 key购买 nike

如果使用 CL_MEM_USE_HOST_PTR 将变量传递给内核,是否意味着设备中变量的任何更改也会显示在主机内存中?

我所处的场景是使用 CPU 作为设备而不是 GPU,因此传递给内核的所有内容都将标记为 CL_MEM_USE_HOST_PTR。

如果这是真的,那么我不再需要将所有内容读回主机,这非常方便。

最佳答案

您的理解是正确的,除了一个可能的陷阱:documentation指出

OpenCL implementations are allowed to cache the buffer contents pointed to by host_ptr in device memory. This cached copy can be used when kernels are executed on a device.

这意味着内核执行的数据更改可能不会立即反射(reflect)在 host_ptr 中。事实上,不能保证 host_ptr 在用于缓冲区时包含有效数据。

为了获得有效且最新的数据,您必须强制同步。官方文档对这一刻的描述有点模糊,但是buffer mapping/unmapping绝对有效:

If the buffer object is created with CL_MEM_USE_HOST_PTR set in mem_flags, the host_ptr specified in clCreateBuffer is guaranteed to contain the latest bits in the region being mapped when the clEnqueueMapBuffer command has completed; and the pointer value returned by clEnqueueMapBuffer will be derived from the host_ptr specified when the buffer object is created.

这是改编自 Khronos group forum post 的示例:

cl_mem device_output = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_USE_HOST_PTR, size, original_output, NULL);
// run the kernel
void* pointer = clEnqueueMapBuffer(queue, device_output, CL_TRUE, CL_MAP_READ, size, 0, 0, NULL, NULL, NULL);
// work with 'original_output'
clEnqueueUnmapMemObject(queue, device_output, pointer, 0, NULL, NULL);
clReleaseMemObject(device_output);

关于OpenCL:通过 CL_MEM_USE_HOST_PTR 传递给内核的变量的设备/主机内存一致性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12283537/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com