mirror of
https://gitlab.com/libeigen/eigen.git
synced 2024-12-15 07:10:37 +08:00
44a527dfa5
by tweaking the filename and adding 2 categories: Troubleshooting and Advanced * use the EXCLUDE_SYMBOLS to clean the class list (insteaded of a homemade bash script) * remove the broken "exemple list" * re-structure the unsupported directory as mentionned in the ML and integrate the doc as follow: - snippets of the unsupported directory are directly imported from the main snippets/CMakefile.txt (no need to duplicate code) - add a top level "Unsupported modules" group - unsupported modules have to defined their own sub group and nest it using \ingroup Unsupported_modules - then a pair of //@{ //@} will put everything in the submodule - this is just a proposal !
150 lines
4.7 KiB
Plaintext
150 lines
4.7 KiB
Plaintext
namespace Eigen {
|
|
|
|
/** \page StructHavingEigenMembers Troubleshooting - Structures Having Eigen Members
|
|
|
|
\b Table \b of \b contents
|
|
- \ref summary
|
|
- \ref what
|
|
- \ref how
|
|
- \ref why
|
|
- \ref movetotop
|
|
- \ref bugineigen
|
|
- \ref conditional
|
|
|
|
\section summary Executive Summary
|
|
|
|
If you define a structure having members of \ref FixedSizeVectorizable "fixed-size vectorizable Eigen types", you must overload its "operator new" so that it generates 16-bytes-aligned pointers. Fortunately, Eigen provides you with a macro EIGEN_MAKE_ALIGNED_OPERATOR_NEW that does that for you.
|
|
|
|
\section what What kind of code needs to be changed?
|
|
|
|
The kind of code that needs to be changed is this:
|
|
|
|
\code
|
|
class Foo
|
|
{
|
|
...
|
|
Eigen::Vector2d v;
|
|
...
|
|
};
|
|
|
|
...
|
|
|
|
Foo *foo = new Foo;
|
|
\endcode
|
|
|
|
In other words: you have a class that has as a member a \ref FixedSizeVectorizable "fixed-size vectorizable Eigen object", and then you dynamically create an object of that class.
|
|
|
|
\section how How should such code be modified?
|
|
|
|
Very easy, you just need to put a EIGEN_MAKE_ALIGNED_OPERATOR_NEW macro in a public part of your class, like this:
|
|
|
|
\code
|
|
class Foo
|
|
{
|
|
...
|
|
Eigen::Vector2d v;
|
|
...
|
|
public:
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
|
|
};
|
|
|
|
...
|
|
|
|
Foo *foo = new Foo;
|
|
\endcode
|
|
|
|
This macro makes "new Foo" always return an aligned pointer.
|
|
|
|
\section why Why is this needed?
|
|
|
|
OK let's say that your code looks like this:
|
|
|
|
\code
|
|
class Foo
|
|
{
|
|
...
|
|
Eigen::Vector2d v;
|
|
...
|
|
};
|
|
|
|
...
|
|
|
|
Foo *foo = new Foo;
|
|
\endcode
|
|
|
|
A Eigen::Vector2d consists of 2 doubles, which is 128 bits. Which is exactly the size of a SSE packet, which makes it possible to use SSE for all sorts of operations on this vector. But SSE instructions (at least the ones that Eigen uses, which are the fast ones) require 128-bit alignment. Otherwise you get a segmentation fault.
|
|
|
|
For this reason, Eigen takes care by itself to require 128-bit alignment for Eigen::Vector2d, by doing two things:
|
|
\li Eigen requires 128-bit alignment for the Eigen::Vector2d's array (of 2 doubles). With GCC, this is done with a __attribute__ ((aligned(16))).
|
|
\li Eigen overloads the "operator new" of Eigen::Vector2d so it will always return 128-bit aligned pointers.
|
|
|
|
Thus, normally, you don't have to worry about anything, Eigen handles alignment for you...
|
|
|
|
... except in one case. When you have a class Foo like above, and you dynamically allocate a new Foo as above, then, since Foo doesn't have aligned "operator new", the returned pointer foo is not necessarily 128-bit aligned.
|
|
|
|
The alignment attribute of the member v is then relative to the start of the class, foo. If the foo pointer wasn't aligned, then foo->v won't be aligned either!
|
|
|
|
The solution is to let class Foo have an aligned "operator new", as we showed in the previous section.
|
|
|
|
\section movetotop Should I then put all the members of Eigen types at the beginning of my class?
|
|
|
|
No, that's not needed. Since Eigen takes care of declaring 128-bit alignment, all members that need it are automatically 128-bit aligned relatively to the class. So when you have code like
|
|
|
|
\code
|
|
class Foo
|
|
{
|
|
double x;
|
|
Eigen::Vector2d v;
|
|
public:
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
|
|
};
|
|
\endcode
|
|
|
|
it will work just fine. You do \b not need to rewrite it as
|
|
|
|
\code
|
|
class Foo
|
|
{
|
|
Eigen::Vector2d v;
|
|
double x;
|
|
public:
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
|
|
};
|
|
\endcode
|
|
|
|
\section dynamicsize What about dynamic-size matrices and vectors?
|
|
|
|
Dynamic-size matrices and vectors, such as Eigen::VectorXd, allocate dynamically their own array of coefficients, so they take care of requiring absolute alignment automatically. So they don't cause this issue. The issue discussed here is only with fixed-size matrices and vectors.
|
|
|
|
\section bugineigen So is this a bug in Eigen?
|
|
|
|
No, it's not our bug. It's more like an inherent problem of the C++ language -- though it must be said that any other existing language probably has the same problem. The problem is that there is no way that you can specify an aligned "operator new" that would propagate to classes having you as member data.
|
|
|
|
\section conditional What if I want to do this conditionnally (depending on template parameters) ?
|
|
|
|
For this situation, we offer the macro EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign). It will generate aligned operators like EIGEN_MAKE_ALIGNED_OPERATOR_NEW if NeedsToAlign is true. It will generate operators with the default alignment if NeedsToAlign is false.
|
|
|
|
Example:
|
|
|
|
\code
|
|
template<int n> class Foo
|
|
{
|
|
typedef Eigen::Matrix<float,n,1> Vector;
|
|
enum { NeedsToAlign = (sizeof(Vector)%16)==0 };
|
|
...
|
|
Vector v;
|
|
...
|
|
public:
|
|
EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
|
|
};
|
|
|
|
...
|
|
|
|
Foo<4> *foo4 = new Foo<4>; // foo4 is guaranteed to be 128bit-aligned
|
|
Foo<3> *foo3 = new Foo<3>; // foo3 has only the system default alignment guarantee
|
|
\endcode
|
|
|
|
*/
|
|
|
|
}
|