tags:

views:

2020

answers:

6

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?

A: 

Okay first of all SSE something thats "inside" the CPU, read here

and "dealing" with optimiziation for this is done by the compilera and in this case the JIT. Why? Simply because, when something on a computer needs to be executed, it needs to be compiled / interpreted into byte-sequences so that the computer can understand you. And SSE somehow magically enhances stuff. So you need to optimize your byte-code, but no real human can read that stuff so you write it all in assembler. But thats something that the interpreter or compiler does for you.

But sure, you can do SSE optimizations from c++, which probably means you can do it with unmanaged code, but i don't see why since the .NET JIT Compiler will handle all the optimization you need.

Filip Ekberg
+2  A: 

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

Paul Whitehurst
Sort of. It checks the rev of SSE to know if it has access to some nice general purpose instructions like LZCNT, POPCNT, and a few instructions that will use the extended registers to move larger block of memory. It won't auto-parallelize anything.
Joe
+2  A: 

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.

JaredPar
Can you give a reference and/or example of where the JIT will employ SSE? Is there a way to write code in an SSE-friendly manner?
Konrad Rudolph
@Konrad, check out David Notario's blog. He has some details about where the CLR uses SSE2. http://blogs.msdn.com/davidnotario/archive/2005/08/15/451845.aspx
JaredPar
+1  A: 

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.

Filip
The runtime may or may not use some SSE instructions but it's not going to vectorize your code. This means you're missing significant performance speedups which may be important to you.
Ade Miller
+7  A: 

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

Amir
+1  A: 

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.

Joe