Re: [eigen] How do you link multiple versions (e.g. AVX vs SSE) of the same Eigen code?

[ Thread Index | Date Index | More Archives ]

Create interfaces to the high level functions (e.g. foo) and create a dll compiling with each option, then load the correct one at run-time?


From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
Sent: Tuesday, January 27, 2015 4:29 PM
To: eigen
Subject: [eigen] How do you link multiple versions (e.g. AVX vs SSE) of the same Eigen code?
Hi List,

The question has been around for a long time: one often needs to compile Eigen code with multiple sets of compiler flags, and select at runtime between them, to take advantage of possibly-present CPU features such as AVX.

I always thought that I could simply compile the same Eigen-using code multiple times, with just a different namespace around my Eigen-using function each time, like

#include <Eigen/Core>
namespace avx_code_path {
void foo(Eigen::Vector4f& v)
    v += v;

However, there is a problem with this: each of these code paths compiles Eigen code under the same, non-prefixed, Eigen namespace, each time with different resulting binary code due to different compiler flags. We are thus in a case of undefined behavior due to multiple definitions of inline functions (Eigen's funtions) actually being different.

I was hoping that this wasn't a big deal, that in practice the linker would do the right thing and let the code in each TU use the Eigen symbols defined in the same TU, so there would be no mixup. I was wrong... I'm seeing mixups on a basic testcase here, where my AVX codepath uses Eigen code from the non-AVX codepath.

So this approach doesn't work.

An approach that works is to #define Eigen EigenAVX, but this is very annoying because you can't anymore pass around Eigen objects, you have to somehow reinterpret_cast them to EigenAVX objects.

Any better approach?


Mail converted by MHonArc 2.6.19+