4

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.

me me
  • 395
  • 1
  • 3
  • 14
  • 1
    Create a class library in C++/CLI. You can use SSEx intrinsics in code that's compiled with #pragma unmanaged in effect. You will however never get a single SSE4 instruction efficient, there's overhead in switching from managed to unmanaged code execution. Takes about 5 nanoseconds, plus whatever overhead is involved in marshaling arguments and the inevitable copy you need to get variables aligned to 16. Your native code must be substantial enough to amortize that cost. – Hans Passant May 27 '13 at 13:04
  • 3
    "since SSE4 is available since 2006 one can safely assume that every modern computer has it by now" - Penryn (Intel) was January 2008 and Bulldozer (AMD) was October 2011. If you don't have a fallback on SSE2 your code will crash on any CPUs older than these dates for Intel and AMD respectively. – Asik May 27 '13 at 14:56
  • You can use opencl wrappers. – huseyin tugrul buyukisik Aug 11 '17 at 20:24

3 Answers3

12

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.

Marat Dukhan
  • 11,993
  • 4
  • 27
  • 41
  • This looks really promising, how would I use this in my code (I'm using c# in Mono, for a project in unity, so it would be nice if it could run on both Windows and Mac)? Is there a dll, or is it possible to make one that will be usable in MacOS and also uses all (or most of) the optimizations? – me me Jun 01 '13 at 19:27
  • It works under Mono in Linux (and obviously under .Net in Windows), but Mac is not currently supported. – Marat Dukhan Jun 02 '13 at 05:25
  • Download the latest official release, and you will find pre-built files in binaries directory – Marat Dukhan Jun 02 '13 at 05:34
  • In the next release (Beta 3) Mac OS X will be supported as well – Marat Dukhan Jun 08 '13 at 10:45
  • This library looks great, going to test it with an audio engine i am coding rigth now: http://www.kvraudio.com/forum/viewtopic.php?p=5526631 – thalm Oct 18 '13 at 18:39
  • @MaratDukhan Is Yeppp! still maintained and alive? The website looks down-ish. And how does it compare to RyuJIT's new SIMD instructions? – M. Mimpen Feb 21 '17 at 10:09
  • Yeppp 1.0 is unmaintained, but Robert Guthrie works on a new release – Marat Dukhan Feb 23 '17 at 03:00
2

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++.

Asik
  • 21,506
  • 6
  • 72
  • 131
1

C# supports quite a few SIMD/SSE instructions natively in System.Numerics which is cross-platform. Dot product is a supported instruction.

HPCsharp nuget package on nuget.org, which I've been actively developing for the last two years, uses this capability to accelerate many algorithms. Let me know if certain useful algorithms could use acceleration thru SIMD/SSE and multi-core.

DragonSpit
  • 458
  • 4
  • 9