Question

I was reading a question about c# code optimization and one solution was to use c++ with SSE. Is it possible to do SSE directly from a c# program?

Was it helpful?

Solution

The upcoming Mono 2.2 release will have SIMD support. Miguel de Icaza blogged about the upcoming feature here, and the API is here.

Although there will be a library that will support development under Microsoft's .NET Windows runtime, it will not have the performance benefits that you are looking for unless you run the code under the Mono runtime. Which might be doable depending on your circumstances.

Update: Mono 2.2 is released

OTHER TIPS

Can C# explicitly make an SSE call?

No. C# cannot produce inline IL much less inline x86/amd64 assembly.

The CLR, and more specifically the JIT, will use SSE if it's available removing the need to force it in most circumstances. I say most because I'm not an SSE expert and I'm sure that there are cases where it could be beneficial and the JIT does not make the optimization.

SIMD for .NET will be available in the near future. RyuJIT (the next-generation JIT compiler for .NET) required for this feature ATM.

You should use Microsoft.Numerics.Vectors.Vector<T> class from Microsoft.Bcl.Simd package to take advantage of this feature. Sample code here.

Based on this forum posting, the MS JIT compiler automatically uses SSE if SSE is available on the target machine.

If you have a 'chunk' of work you want to do, the best bet is to write it in C++ using the MMX/SSE intrinsics and then make a very simple /clr managed C++ class that wraps your functionality and exposes it out as a .net class. Then your code can just use that assembly as if it were a normal class.

For more about the VC intrinsics you can look at this little ditty I wrote many years ago.

http://msdn.microsoft.com/en-us/library/0aws1s9k.aspx

Oh - I'm assuming you are actually wanting to use the parallel functions to speed something up. As others have pointed out - if you just want to move data in larger chunks and the like, the JIT already knows how to use SSE for those basics.

Filip is correct. I have another, older post showing a similar, but more detailed example. I have actually run this code, and modified it myself to prove to myself that it works. I am contemplating using this technique in a project I am working and is why I am out looking to see what may be new since this is a bit old. As the author implies, you can write any function you wish in C++, compile it, then copy the bytes into your C#.

http://blogs.msdn.com/b/devinj/archive/2005/07/12/438323.aspx

I would add that Joe's CLI C++ class is a good idea as well, however, I don't think the sse compiler flag and the /clr flag are compatible on the same project. I just verified that: have to write your high perf code in a separate project to use the SSE (/arch:sse or /arch:sse2) compiler flag as /clr is incomatible. To do anything much more complex than do simple arithmetic on a few inputs, I think this is the best approach.

Recently Microsoft has released a beta SIMD vector library (Microsoft.Bcl.Simd) for C# which requires installation of the RyuJIT CTP and works only Windows 8.

You can also just used a native SSE library and invoke it from C#. For example the Yeppp library, see this StackOverflow answer.

Sure you can (the more important question is - why would you? Just leave it to the runtime; that's its job).

C# lets you map a delegate to a memory address. That memory address can contain raw assembly codes. You can read more on Michael Giagnocavo's blog.

Although I have not tried myself, it may be possible to use Marshal.GetDelegateForFunctionPointer as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top