Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 230
LTR Avatar asked Mar 30 '13 21:03

LTR


2 Answers

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.

like image 122
Ben Voigt Avatar answered Oct 04 '22 16:10

Ben Voigt


Microsoft just announced support for generating vectorized instructions in their .NET Native compiler thanks to back-end C++ compiler optimizations, and more importantly native support for SIMD vector types in the most recent version of their JIT ("RyuJIT"). See some samples here.

like image 31
Sebastian Good Avatar answered Oct 04 '22 14:10

Sebastian Good