Very simple SIMD programming

F i L witte2008 at gmail.com
Wed Oct 24 10:23:46 PDT 2012


Manu wrote:
> One thing I can think of that would really improve simd (and 
> not only simd)
> would be a way to define compound operators.
> If the library could detect/hook sequences of operations and 
> implement them
> more efficiently as a compound, that would make some very 
> powerful
> optimisations available.
>
> Simple example:
>   T opCompound(string seq)(T a, T b, T c) if(seq == "* +") { 
> return
> _madd(a, b, c); }
>   T opCompound(string seq)(T a, T b, T c) if(seq == "+ *") { 
> return
> _madd(b, c, a); }

I thought about that before and it might be nice to have that 
level of control in the language, but ultimately, like jerro 
said, I think it would be better suited for the compiler's 
backend optimization. Unfortunately I don't think more complex 
patterns, such as Matrix multiplications, are found and optimized 
by GCC/LLVM... I could be wrong, but these are area where my 
hand-tuned code always outperforms basic math code.

I think having that in the back-end makes a lot of sense, because 
your code is easier to read and understand, without sacrificing 
performance. Plus, it would be difficult to map a sequence as 
complex as matrix multiplication to a single compound operator. 
That being said, I do think something similar would be useful in 
general:

   struct Vector
   {
       ...

       static float distance(Vector a, Vector b) {...}
       static float distanceSquared(Vector a, Vector b) {...}

       float opSequence(string funcs...)(Vector a, Vector b)
         if (funcs[0] == "Math.sqrt" &&
             funcs[1] == "Vector.distance")
       {
           return distanceSquared(a, b);
       }
   }

   void main()
   {
       auto a = Vector.random( ... );
       auto b = Vector.random( ... );

       // Below is turned into a 'distanceSquared()' call
       float dis = Math.sqrt(Vector.distance(a, b));
   }

Since distance requires a 'Math.sqrt()', this pseudo-code could 
avoid the operation entirely by calling 'distanceSquared()' even 
if the programmer is a noob and doesn't know to do it explicitly.


More information about the Digitalmars-d mailing list