How to write/read a single float value(buffer) from OpenCL device

此生再无相见时 提交于 2019-12-13 03:49:33

问题


There are lots of questions about how to read an array from the device, but I only wanna read a single float value from the device. Or it only can read an array from the device?

I create a buffer for (float) sum like below.

ocl.sum = clCreateBuffer(context, CL_MEM_READ_WRITE, 1, NULL, &err);

Set the arg like this.

clSetKernelArg(kernel, 0, sizeof(cl_men), &ocl.arr);
clSetKernelArg(kernel, 1, sizeof(cl_float), &ocl.sum);

In the kernel, I calculate the sum.

kernel calculate(global arr, float sum)
{
...
sum = 100.0f;
}

How can I get the sum from the device after all?

float result = 0.f;
err = clEnqueueReadBuffer(queue, ocl.sum, CL_TRUE, 0, 1, &result, 0, NULL, NULL);

print(result);

回答1:


Reading from the device, whether that is for a single value or an array have to go via global memory. So the kernel signature has to be of kernel calculate(..., global float *sum). Then you read it from the device the way you posted - by passing &result to clEnqueueReadBuffer.



来源:https://stackoverflow.com/questions/55409702/how-to-write-read-a-single-float-valuebuffer-from-opencl-device

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