Jan
Jan

Reputation: 113

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.

Upvotes: 7

Views: 2187

Answers (4)

Daniel Mošmondor
Daniel Mošmondor

Reputation: 19966

BTW, fftw wrapper I use calls fftw methods for memory allocation, then the data is filled in managed code, then again fftw is called for processing. Can you use similar paradigm?

Upvotes: 0

Jan
Jan

Reputation: 113

Well, just to wrap things up:

We decided that it is way too complicated to get our code working directly with the SSE math library. So we will have two math libraries. One that uses SSE for high performance code, which will be used deep inside our C++ code. And another one that is implemented without SSE, which will be used in all the interfaces and the not so much performance critical code.

This way we can compile and link everything with managed code, but can still boost our performance by using the SSE code where it really matters.

However getting anything to compile with managed code, if the compiler can "see" the SSE stuff (e.g. you simply have a __m128 member in a class), is a nightmare. You have to put lots of wrappers around everything.

I guess that's simply a use-case that C# and managed code were never really meant to be compatible with.

Anyway, thanks for all your suggestions, it helped me get a clearer picture of the problem.

Upvotes: 1

Steve Townsend
Steve Townsend

Reputation: 54178

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.

Upvotes: 5

Bryan B
Bryan B

Reputation: 4535

Can struct packing help you at all?

http://www.developerfusion.com/article/84519/mastering-structs-in-c/

You can use it to align fields to specific areas in memory.

Upvotes: 2

Related Questions