LinNotFound
LinNotFound

Reputation: 541

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

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 can it only 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_mem), &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?

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

print(result);

Upvotes: 1

Views: 1464

Answers (1)

doqtor
doqtor

Reputation: 8484

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.

Upvotes: 2

Related Questions