当使用的OpenCL API标量数据类型? [英] When to use the OpenCL API scalar data types?

查看:162
本文介绍了当使用的OpenCL API标量数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有麻烦的理解时使用OpenCL的API数据类型,如cl_float,cl_uchar,等等,这些都可以在这里找到:

I have been having trouble understanding when to use the OpenCL API data types like cl_float, cl_uchar, etc., which can be found here:

http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/scalarDataTypes.html

我所看到的例子,涉及复制缓冲装置是这样的:

The examples I have seen that involve copying a buffer to the device look like this:

float data[DATA_SIZE];              // original data set given to device

//Create the input and output arrays in device memory for our calculation
input = clCreateBuffer(context,  CL_MEM_READ_ONLY,  sizeof(float) * count, NULL,

// Write our data set into the input array in device memory
err = clEnqueueWriteBuffer(commands, input, CL_TRUE, 0, sizeof(float) * count, data, 0, NULL, NULL);
if (err != CL_SUCCESS)
{
    printf("Error: Failed to write to source array!\n");
    exit(1);
}

这code是直接取自苹果code可以在这里找到示例:
<一href=\"https://developer.apple.com/library/mac/sample$c$c/OpenCL_Hello_World_Example/Listings/ReadMe_txt.html\" rel=\"nofollow\">https://developer.apple.com/library/mac/sample$c$c/OpenCL_Hello_World_Example/Listings/ReadMe_txt.html

This code was taken directly from an Apple code sample available here: https://developer.apple.com/library/mac/samplecode/OpenCL_Hello_World_Example/Listings/ReadMe_txt.html

您将在彩车的数组复制到设备上面code注意到。为什么这并不需要cl_floats数组?内存直接复制,对不对?如果您的主机浮子和设备浮动是不一样的大小会发生什么?

You will notice in the above code that an array of floats is copied to the device. Why does this not need to be an array of cl_floats? The memory is directly copied, right? What happens if your host float and the device float are not the same size?

你能解释一下为什么它是没有必要使用cl_float?如果没有必要在这种情况下,则当<青霉>应的的OpenCL类型一起使用

Could you explain why it is not necessary to use cl_float? If it is not necessary in this case, then when should the opencl types be used?

感谢您的帮助!

推荐答案

这些整点CL _ - prefixed类型是它们被定义为相同的大小由OpenCL的运行时主机和设备。在实践中,对于简单的类型,如浮动你经常可以脱身,而无需使用 cl_float (为浮动 cl_float 往往是相同的大小),但它始终是推荐使用 CL _ - prefixed类型最大的可移植性。作为<一指出, href=\"http://stackoverflow.com/questions/25398480/when-to-use-the-opencl-api-scalar-data-types/25398700?noredirect=1#comment39616918_25398700\">the评论(感谢@DarkZeros),这可能会导致问题的类型的一个很好的例子是 INT ,因为这会导致大小取决于主机平台而有所不同。使用 cl_int 表示,这是没有问题的。

The whole point of these cl_-prefixed types are they are defined as the same size on the host and device by the OpenCL runtime. In practice, for simple types such as a float you can often get away without using cl_float (as a float and cl_float are often the same size) but it is always recommended to use the cl_-prefixed types for maximum portability. As pointed out in the comments (thanks @DarkZeros), a good example of a type that may cause problems is an int, as this can vary in size depending on the host platform. Using cl_int means that this is not a problem.

正如您所链接到的文件中指出,OpenCL的C基于C99用的特定的扩展和限制的(我听说它描述为一个supersubset:-)。除了确保您的类型的大小匹配,这样做也会限制自己使用的OpenCL C(为size_t 中定义的类型不具有匹配 cl_size_t ,例如)。

As pointed out in the documentation that you have linked to, OpenCL C is based on C99 with specific extensions and restrictions (I've heard it described as a "supersubset" :-). As well as ensuring that the sizes of your types match up, you are also limiting yourself to using types that are defined in OpenCL C (size_t doesn't have a matching cl_size_t, for example).

在你的例子而言,缓冲器的大小相同的读取和正在进行,但潜在的问题是,该装置可具有不同尺寸的类型,所以你的内核可能会被垃圾值工作。我会改变浮动的所有实例 cl_float 来防范这种可能性。

In terms of your example, the buffers are the same size as the reads and writes that are being carried out but the potential problem is that the device may have a differently sized float type, so your kernels may potentially be working with garbage values. I would change all instances of float to cl_float to guard against this possibility.

这篇关于当使用的OpenCL API标量数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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