Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling SSE code in managed code (alignment)

Here's my problem: We have a math library written in C++ that is heavily using SSE. We need to use that same math library in our the managed layer of our tools (which are written in C#).

The problem is, that the math library classes must be 16-byte aligned (for SSE to work). However when compiling the managed code, I get lots of errors, because the "__declspec (align(X))" is not supported.

Any ideas whether this is possible somehow? I wasn't able to find any useful information.

Some additional information:

The math library, written in C++, uses SSE for maximum performance. However our tool does not require maximum performance, we can even take a performance hit compared to general C# code. It is more about being able to actually execute all our code (it is a huge code base), without having people to convert back and forth between data types.

So this is really only about usability, not about performance.

I tried this: I put all our math functions into a cpp, instead of having them as inline functions. Now they are exported from their own DLL. However the vector-class of course still has a __m128 private member for its data.

As soon as I just put such a variable in managed code, the compiler tells me that my managed code is now native code.

Does that mean I must not have such a type in my class definition and hide it completely behind a DLL interface? Thanks.

like image 746
Jan Avatar asked Nov 14 '11 16:11

Jan


1 Answers

Sounds like you are trying to compile your math library to managed code? Instead, you should leave it in native code and call it direct from managed code using P/Invoke.

Marshalling of the required structs from C# into native code with correct alignment will still be complex, but should be doable.

The work shown here could be useful to you in understanding the issues.

I'm embarking on the adventurous course of trying to accelerate a simulation application written entirely in C#.NET using SSE2. So far I've spent a few days looking at the feasibility of using SSE2 in a .NET application.

like image 127
Steve Townsend Avatar answered Oct 14 '22 06:10

Steve Townsend