Call SSE code in managed code (alignment)

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

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

Any ideas, is this possible somehow? I could not find useful information.

Additional Information:

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

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

I tried this: I included all our math functions in cpp, instead of using them as built-in functions. Now they are exported from their own DLL. However, the vector class, of course, has a private member __m128 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 internal code.

Does this mean that I should not have this type in the class definition and completely hide it behind the DLL interface? Thanks.

+7
source share
4 answers

Sounds like you're trying to compile your math library for managed code? Instead, you should leave it in your own code and call it directly from managed code using P / Invoke .

Marching the required structures from C # to native code with proper alignment will still be difficult, but should be doable.

The work shown here may be helpful in understanding problems.

I'm embarking on an adventurous course to speed up a simulation application written entirely in C # .NET using SSE2. So far I spent several days considering using SSE2 in .NET.

+5
source

Can struct packaging help you?

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

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

+2
source

Well, just to wrap things up:

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

This way we can compile and associate everything with managed code, but we can still improve our performance using SSE code, where it really matters.

Be that as it may, to compile with managed code, if the compiler can "see" the SSE stuff (for example, you just have the __m128 member in the class), this is a nightmare. You have to put a lot of wrappers around everything.

I suppose it's just a use case that C # and managed code should never have been compatible.

In any case, thanks for all your suggestions, this helped me get a clearer picture of the problem.

+1
source

BTW, fftw wrapper I use fftw methods to allocate memory, then the data is populated with managed code, then fftw processing is called again. Can you use a similar paradigm?

0
source

All Articles