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

5

17

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?

Foote answered 30/3, 2013 at 21:33 Comment(3)
How is this "off-topic"? You people are baffling sometimes.Phelps
Multithreading (or Open MP) could be useful. I don't know if you can utilize Open MP on .NET, but I'd recommend a native dll anyway if you can keep the total amount of p/invoke function calls low.Toastmaster
Late reply I know but you might want to look into the Yeppp! SIMD libraryEulogist
A
8

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.

Ayurveda answered 30/3, 2013 at 23:48 Comment(0)
D
4

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.

Devolve answered 4/4, 2014 at 15:0 Comment(0)
C
3

Latest developments in .NET include a SIMD dedicated vector/matrix library called System.Numerics.Vector:

Using System.Numerics.Vector for Graphics Programming

This will be enabled as soon as the new JIT compiler "RyuJIT" will be the default, the announcement is here:

RyuJIT: The next-generation JIT compiler for .NET

So very soon (hopefully 2015) we will have very fast SIMD vectors in .NET without any programming overhead.

Clingstone answered 20/3, 2015 at 13:36 Comment(0)
C
1

If you're in the mood to write assembly code in C#, another option is the NAsmJit project, which is a port of AsmJit to C#. I haven't updated it to reflect the latest changes in that project, but much of the support was quite usable at last check.

Coot answered 4/4, 2014 at 15:22 Comment(0)
E
0

Mixing .NET with native code. In this case you will need to have one release for x86 and another for x64. You can see Mixed (Native and Managed) Assemblies [MSDN]

Enviable answered 30/3, 2013 at 23:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.