如何从OpenCL设备写入/读取单个浮点值(缓冲区) [英] How to write/read a single float value(buffer) from OpenCL device

查看:170
本文介绍了如何从OpenCL设备写入/读取单个浮点值(缓冲区)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何从设备读取数组有很多问题,但是我只想从设备读取单个浮点值.还是只能从设备读取数组?

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);

像这样设置arg.

clSetKernelArg(kernel, 0, sizeof(cl_men), &ocl.arr);
clSetKernelArg(kernel, 1, sizeof(cl_float), &ocl.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);

推荐答案

从设备读取数据(无论是单个值还是数组)都必须通过全局内存进行.因此,内核签名必须为kernel calculate(..., global float *sum).然后,通过将&result传递到clEnqueueReadBuffer,以发布方式从设备中读取它.

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.

这篇关于如何从OpenCL设备写入/读取单个浮点值(缓冲区)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆