Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there anyway to "vectorize" the addition of elements across arrays in a SIMD fashion?

For example, I would like to turn:

var a = new[] { 1, 2, 3, 4 };
var b = new[] { 1, 2, 3, 4 };
var c = new[] { 1, 2, 3, 4 };
var d = new[] { 1, 2, 3, 4 };

var e = new int[4];

for (int i = 0; i < a.Length; i++)
{
    e[i] = a[i] + b[i] + c[i] + d[i];
}

// e should equal { 4, 8, 12, 16 }

Into something like:

var e = VectorAdd(a,b,c,d);

I know something may exist in the C++ / XNA libraries, but I didn't know if we have it in the standard .Net libraries.

Thanks!

share|improve this question
    
I think that simple loops like the one you wrote are vectorized by an optimizing compiler. –  Tudor Nov 21 '11 at 16:15

4 Answers 4

up vote 1 down vote accepted
int[] VectorAdd(params int[][] inputVectors)
{
    var e = new int[4];

    var a = inputVectors.First();
    for (int i = 0; i < a.Length; i++)
    {
        for (int j = 0; j < inputVectors.Length ; j++)
            e[i] += inputVectors[j][i];
    }
    return e;
}
share|improve this answer
    
He was referring to SIMD vectorization (eg. SSE/AVX) not mathematical vectors as such. –  Ron Warholic Nov 21 '11 at 16:22
    
Oddly enough, somehow this answer was still the most helpful to the OP. Maybe you could explain a bit what you ended up doing, @AlexMoore ? –  sehe Sep 22 '12 at 12:09

You will want to look at Mono.Simd:

http://tirania.org/blog/archive/2008/Nov-03.html

It supports SIMD in C#

using Mono.Simd;


//...
var a = new Vector4f( 1, 2, 3, 4 );
var b = new Vector4f( 1, 2, 3, 4 );
var c = new Vector4f( 1, 2, 3, 4 );
var d = new Vector4f( 1, 2, 3, 4 );

var e = a+b+c+d;
share|improve this answer
    
FYI you can use the assembly but it will only use SIMD instructions if it is running within the mono CLR where there is support for them. –  locster Sep 22 '12 at 7:51

I guess it all depends on what you are doing, but if you are worried about vectorizing vector sums, you might want to take a look at a library such as Math.NET which provide optimized numerical computations.

From their website:

It targets Microsoft .Net 4.0, Mono and Silverlight 4, and in addition to a purely managed implementation will also support native hardware optimization (MKL, ATLAS).

share|improve this answer

Mono provides a relatively decent SIMD API (as sehe mentions) but if Mono isn't an option I would probably write a C++/CLI interface library to do the heavy lifting. C# works pretty well for most problem sets but if you start getting into high performance code it's best to go to a language that gives you the control to really get dirty with performance.

Here at work we use P/Invoke to call image processing routines written in C++ from C#. P/Invoke has some overhead but if you make very few calls and do a lot of processing on the native side it can be worth it.

share|improve this answer
    
second best, +1 –  sehe Nov 21 '11 at 16:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.