5 Replies Latest reply on Mar 3, 2012 8:14 PM by notzed

    int4 : access through index

    spectral

      Hi,

       

      I would like to know if there is a way to acces the elements of a int4, float8 etc... by index :

      int4 val;

      int a = val;

      where 'i' is dynamic at run time ? It will be useful to access a 'random' item !

      BTW : Intel SDK support it !

        • int4 : access through index
          genaganna

           

          Originally posted by: viewon01 Hi,

           

          I would like to know if there is a way to acces the elements of a int4, float8 etc... by index :

           

          int4 val;

           

          int a = val;

           

          where 'i' is dynamic at run time ? It will be useful to access a 'random' item !

           

          BTW : Intel SDK support it !

           

          As per OpenCL it is not supported. But we can do with existing methods and switch/if conditions

           

          if(i == 0)

                a = val.x;

          else if(i == 1)

                a = val.y;

          else if(i == 2)

                a = val.z;

          else

                 a = val.w;

          • int4 : access through index
            jasno

            you might try

            int4 val;

            int *valPtr = (int *)val;

            then use it like an array.

            valPtr[0] =

            This does compile and work but it might be bad practice

            • int4 : access through index
              MicahVillmow
              viewon01,
              You cannot legally index into a vector in OpenCL as a vector is not an array and is not indexable. Of course once you write the vector to memory, you can index memory and re-interpret the data however you want.
              • Re: int4 : access through index
                notzed

                I know this is an old post, but this type of question comes up fairly often.

                 

                So ... I didn't notice that the OpenCL 1.1 spec had added a shuffle instruction until I came across this unrelated post: http://www.openclblog.com/2011/10/shuffling-and-sorting-part-2.html

                 

                shuffle is awesome for simd code, it was one of the first things I looked up in the original spec and was dissapointed not to find it it.  Amongst other handy uses - particularly for processing non-element-aligned vector data - it can be used to dynamically index a vector.

                 

                e.g.

                 

                uint index;

                float4 vec;

                float value = shuffle(vec, (uint4)index).s0;

                 

                Unfortunately however, at least on Cayman, this looks pretty inefficient from the isa, and using an explicit bit of code is half the number of instructions (each of only 1 vliw slot each).

                 

                e.g.

                uint index;

                float4 vec;

                float value = vec.s0;

                value = index == 1 ? vec.s1 : value;

                value = index == 2 ? vec.s2 : value;

                value = index == 3 ? vec.s3 : value;

                is only 4 instructions with no branching.

                 

                On a CELL SPU, shuffle2(any-4-element-32bit-type) is 1 instruction, so it might have been added to the spec for SPU performance.  There it is quite critical to many algorithms as it is a true simd unit.