Access vector type OpenCL

2019-02-05 01:04发布

问题:

I have a variable whithin a kernel like:

int16 element;

I would like to know if there is a way to adress the third int in element like

element[2] so that i would be as same as writing element.s2

So how can i do something like:

int16 element;
int vector[100] = rand() % 16;

for ( int i=0; i<100; i++ )
   element[ vector[i] ]++;

The way i did was:

int temp[16] = {0};
int16 element;
int vector[100] = rand() % 16;

for ( int i=0; i<100; i++ )
  temp[ vector[i] ]++;


element = (int16)(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8],temp[9],temp[10],temp[11],temp[12],temp[13],temp[14],temp[15]);

I know this is terrible, but it works, ;-)

回答1:

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 ??



回答2:

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


回答3:

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


回答4:

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.



回答5:

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]=...;


回答6:

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



标签: vector opencl