在C / openCL中修补向量元素 [英] Adressing vector elements in C / openCL

查看:124
本文介绍了在C / openCL中修补向量元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pyopenCL中编写一个openCL内核,其中我想要处理向量元素。

I'm writing an openCL Kernel in pyopenCL, where I want to address vector elements.

在plain C中,我想要的结果是: p>

In plain C, the result I want to have is:

int i = 0;
float *vec = (float*)maalloc(sizeof(float)*4);
for (i=0;i<4;i++)
{
    vec[i]=2*i;
}

在openCL中,向量的元素在pythonic - 合成风格。

In openCL, the elements of a vector are accessed in a "pythonic" point-syntax style.

float4 vec = (float4)(0);
for (i=0;i<4,i++)
{
    vec.si = 2*i;
/*obviously doesn't work*/
}

cc.c> vec [2] 在openCL中变为 vec.s2 ,因此不再直接使用变量访问元素。但是如何使用变量访问向量元素?

So vec[2] becomes vec.s2 in openCL, so it is no longer straightforward to access the element with a variable. How can I access the vector element using a variable nevertheless?

推荐答案

OpenCL支持主机端组件和设备端组件(内核)C,所以你可以编写一个内核与你的第一个例子几乎完全相同,使用一个 float 数组。内核可能如下所示:

OpenCL supports C for both the host side components and the device side components (kernels), so you can write a kernel which is almost exactly the same as your first example, using a float array. The kernel might look like the following:

__kernel void vectorAddition(__global float* vec) {
  // Get the global thread id in x dimension(eliminates loop)
  size_t index = get_global_id(0);

  vec[index] = 2.0f * index;
}

然后您可以指定要使用的线程数,

Then you can specify the number of threads to use so that this is done to each element of the array (make the same number of threads as elements in the array).

OpenCL允许使用点表示法访问,但这是访问元素的元素向量数据类型。向量数据类型可以提供改进的性能,因为可以同时对向量数据类型中的所有元素执行相同的操作。

OpenCL does allow access using the dot notation, but this is to access the elements of vector data types. Vector data types can provide improved performance because the same operation can be done to all the elements in the vector data type at the same time.

例如, float4 是一个向量数据类型,它存储四个32位浮点,彼此相邻,形成一个128位结构。

For example, float4 is a vector data type which stores four 32 bit floats next to each other to make a 128 bit struct. You can then perform an operation on all 4 of the floats at one time.

例如:

float4 v = (float4)(1.0f, 2.0f, 3.0f, 4.0f);
float4 mult_result = v * 2;

这需要一条指令同时进行四次乘法。 mult_result 则具有值 {2.0f,4.0f,6.0f,8.0f}

Which takes a single instruction to do the four multiplications simultaneously. mult_result then has the value {2.0f, 4.0f, 6.0f, 8.0f}.

点符号可用于访问 float4 变量的组件,例如:

The dot notation can then be used to access the components of the float4 variables, for example:

float a = v.x;           // a = 1.0f
float b = mult_result.y; // b = 4.0f

这是向量数据类型的摘要:矢量数据类型概述

Here is a summary of the vector data types: Vector Data Type Overview.

这篇关于在C / openCL中修补向量元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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