Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access vector type OpenCL

Tags:

vector

opencl

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

like image 636
Caslu Avatar asked Mar 20 '12 14:03

Caslu


2 Answers

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];
like image 183
MindX Avatar answered Oct 21 '22 09:10

MindX


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

like image 21
kiranputtur Avatar answered Oct 21 '22 09:10

kiranputtur