Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SSE in C#

Tags:

c#

simd

sse

I'm currently coding an application in C# which could benefit a great deal from using SSE, as a relative small piece of code causes 90-95% of the execution time. The code itself is also perfect for SSE (as it's matrix and vectorbased), so I went ahead and started to use Mono.Simd and even though this made a significant difference in execution time, this still isn't enough. The problem with Mono.Simd is that it only has very old SSE-instruction (mainly from SSE1 en SSE2, I believe), which causes the dotproduct (or scalar/inner product) for example to take up 3 instructions, while it can be implemented with SSE4 in only 1 instruction (and since SSE4 is available since 2006 one can safely assume that every modern computer has it by now). Also, a bunch of other functions aren't included at all (get the absolute value of every number for example, which will also need a clumsy workaround).

My question is, are there any other libraries I can call from within my C# code to make use of SSE/SIMD? It's also possible to use inline assembly in C#, so apparently I can also use C++-code, even though this causes a small performance hit, but if anyone would have a relatively easy-to-use C++ library with said functions this would be acceptable I guess.

Thanks in advance for any help.

like image 585
me me Avatar asked Dec 06 '22 07:12

me me


2 Answers

Open-source Yeppp! library (of which I am the author) provides SIMD-optimized data processing functions, and is usable from .Net languages via official bindings. It supports not only SSE, but also later SIMD extensions up to AVX2 from the upcoming Intel Haswell processors. The library automatically chooses the optimal version for the processor it runs on.

like image 192
Marat Dukhan Avatar answered Dec 09 '22 14:12

Marat Dukhan


As of April 2013, Steam Survey reports that only 64% of PCs have support for SSE4.1. In other words, if you assume SSE4.1 support, you'll crash on about a third of all consumer PCs.

I am not familiar with Mono.Simd, but a good alternative on Windows is DirectXMath, if you can be bothered to write a suitable C++/CLI wrapper. Neither will take advantage of all the latest instructions, but you can supplement these on a need-to basis relatively easily with intrinsics. I'm not sure you'll be able to do significantly better than Mono.Simd with it though.

There is no such thing as "inline assembly" in C#; if you want to use C++ or assembly code from C#, you'll have to call it via P/Invoke or a C++/CLI wrapper. Out of the two, C++/CLI has less overhead.

That said, if you need to optimize the hell out of a small piece of code, the best option might be to rewrite that piece of code entirely in native C++.

like image 38
Asik Avatar answered Dec 09 '22 16:12

Asik