Fast Vector Math in .NET - What are the options?

LTR picture LTR · Mar 30, 2013 · Viewed 9.1k times · Source

My 3D graphics software, written in C# using SlimDX, does a lot of vector operations on the CPU. (In this specific situation, it is not possible to offload the work to the GPU).

How can I make my vector math faster? So far, I have found these approaches:

  • Run on Mono instead of Microsoft .NET, because they have SIMD support. Not an option for this project.
  • SlimGen, a project that injects high-performance maths code at runtime. Unfortunately, the project is not in a usable state yet.
  • Write a DLL in C++ using a compiler that utilizes SSE instructions. Interop with that DLL from C#.

Are there any other options to accomplish faster vector math in .NET?

Answer

Ben Voigt picture Ben Voigt · Mar 31, 2013

Write a DLL using Microsoft Visual C++'s compiler. Use standard C++ with SSE intrinsics and/or OpenMP for the heavy numeric code, with #pragma unmanaged. Use #pragma managed to define a clean C++/CLI API which C# can use.

C++ interop is quite a bit faster than p/invoke. And C++/CLI is the only elegant way to deal with both garbage collected memory and the assumptions of native functions (that memory blocks won't move).

You might find that moving some of the OpenGL calls to C++, and using the C++-allocated memory buffers directly for loading VBOs, etc. also gives a big performance win.