0

On a Windows machine with Intel Core-i5, I want to write a c# program which sums an array of double at highest possible speed, which in fact means using the instruction set of the built-in FPU.

double[] arr = new double[] { 1.123, 2.234, 3.1234, .... };

The processor has a built-in command which can sum up a whole memory array ("vector") with one single command. Is there a way in C# to execute the summation with this built-in machine command? ( I mean, besides writing unmanaged assembly code)

EDIT: Or is there a library call which will do this ?

2 Answers2

0

No. You can't directly use SSE/AVX/... instructions in C#. You could write some C++ code and PInvoke it, but probably the PInvoke cost would remove all the benefits of using these instructions.

Technically you can do bad things and call these instructions from C# (see https://stackoverflow.com/a/29646856/613130), but they are bad slow things, so you wouldn't probably gain anything speedwise.

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • hm, I somehow cannot believe this. I mean: All around the world we have millions of of Intel CPUs, millions of programmers are writing C# programs, amd many programs are doing data digging with millions of numbers. So, doesn't Microsoft's compiler+library support that all these programs use the processor's higher instruction set ? That would be a total waste of machine power! –  Apr 30 '15 at 16:15
  • 2
    @StackWorm Sadly it is so. In the future (lets say 2 years), the new Ryujit compiler should solve this... See http://blogs.msdn.com/b/dotnet/archive/2014/04/07/the-jit-finally-proposed-jit-and-simd-are-getting-married.aspx – xanatos Apr 30 '15 at 16:17
0

Yes, there are several ways to accomplish this

double sum = arr.Sum();

which uses Linq to sum the array. This is the simplest way, but is not the highest possible speed way. You asked about a library call that can do this, HPCsharp is such a library: nuget package available on nuget.org. The fastest implementation there is

double sum = arr.SumSsePar();

which uses SIMD/SSE instructions to get the most performance out of each core, and uses multiple cores to get the highest performance out of the processor.

DragonSpit
  • 458
  • 4
  • 9