Access vector type OpenCL

后端 未结 6 881
礼貌的吻别
礼貌的吻别 2020-12-14 11:53

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

相关标签:
6条回答
  • 2020-12-14 12:25

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

    0 讨论(0)
  • 2020-12-14 12:28

    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.

    0 讨论(0)
  • 2020-12-14 12:32

    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];
    
    0 讨论(0)
  • 2020-12-14 12:32

    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);
    }
    
    0 讨论(0)
  • 2020-12-14 12:44

    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

    0 讨论(0)
  • 2020-12-14 12:45

    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]=...;
    
    0 讨论(0)
提交回复
热议问题