Access vector type OpenCL

泪湿孤枕 提交于 2019-11-28 23:59:40

Well there is still dirtier way :), I hope OpenCL provides better way of traversing vector elements.

Here is my way of doing it.

union
    {
      int  elarray[16];
      int16 elvector;
     } element;

//traverse the elements
for ( i = 0; i < 16; i++)
 element.elarray[i] = temp[vector[i]]++;

Btw rand() function is not available in OpenCL kernel, how did you make it work ??

AMD recommends getting vector components this way:

Put the array of masks into an OpenCl constant buffer:

cl_uint const_masks[4][4] =
{
    {0xffffffff, 0, 0, 0},
    {0, 0xffffffff, 0, 0},
    {0, 0, 0xffffffff, 0},
    {0, 0, 0, 0xffffffff},
}

Inside the kernel write something like this:

uint getComponent(uint4 a, int index, __constant uint4 * const_masks)
{
    uint b;
    uint4 masked_a = a & const_masks[index];
    b = masked_a.s0 + masked_a.s1 + masked_a.s2 + masked_a.s3;
    return (b);
}

__kernel void foo(…, __constant uint4 * const_masks, …)
{
    uint4 a = ….;
    int index = …;
    uint b = getComponent(a, index, const_masks);
}

Using pointers is a very easy solution

float4 f4 = (float4)(1.0f, 2.0f, 3.0f, 4.0f);

int gid = get_global_id(0);


float *p = &f4;

result[gid]=p[3];

It is possible, but it not as efficient as direct array accessing.

float index(float4 v, int i) {
    if (i==0) return v.x;
    if (i==1) return v.y;
    if (i==2) return v.z;
    if (i==3) return v.w;
}

But of course, if you need component-wise access this way, then chances are that you're better off not using vectors.

I use this workaround, hoping that compilers are smart enough to see what I mean (I think that element access is a serious omission form the standard):

int16 vec;
// access i-th element:
((int*)vec)[i]=...;

No that's not possible. At least not dynamically at runtime. But you can use an "compile-time"-index to access a component:

float4 v;
v.s0 == v.x; // is true
v.s01 == v.xy // also true

See http://www.khronos.org/registry/cl/specs/opencl-1.1.pdf Section 6.1.7

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