简体   繁体   中英

Summation of double using the processor's floating-point instruction set

On a Windows machine with Intel Core-i5, I want to write ac# 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 ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM