2009-01-12 22:41:12 +08:00
namespace Eigen {
Big changes in Eigen documentation:
- Organize the documentation into "chapters".
- Each chapter include many documentation pages, reference pages organized as modules, and a quick reference page.
- The "Chapters" tree is created using the defgroup/ingroup mechanism, even for the documentation pages (i.e., .dox files for which I added an \eigenManualPage macro that we can switch between \page or \defgroup ).
- Add a "General topics" entry for all pages that do not fit well in the previous "chapters".
- The highlevel struture is managed by a new eigendoxy_layout.xml file.
- remove the "index" and quite useless pages (namespace list, class hierarchy, member list, file list, etc.)
- add the javascript search-engine.
- add the "treeview" panel.
- remove \tableofcontents (replace them by a custom \eigenAutoToc macro to be able to easily re-enable if needed).
- add javascript to automatically generate a TOC from the h1/h2 tags of the current page, and put the TOC in the left side panel.
- overload various javascript function generated by doxygen to:
- remove the root of the treeview
- remove links to section/subsection from the treeview
- automatically expand the "Chapters" section
- automatically expand the current section
- adjust the height of the treeview to take into account the TOC
- always use the default .css file, eigendoxy.css now only includes our modifications
- use Doxyfile to specify our logo
- remove cross references to unsupported modules (temporarily)
2013-01-05 23:37:11 +08:00
/** \eigenManualPage TopicStructHavingEigenMembers Structures Having Eigen Members
2009-01-12 22:41:12 +08:00
Big changes in Eigen documentation:
- Organize the documentation into "chapters".
- Each chapter include many documentation pages, reference pages organized as modules, and a quick reference page.
- The "Chapters" tree is created using the defgroup/ingroup mechanism, even for the documentation pages (i.e., .dox files for which I added an \eigenManualPage macro that we can switch between \page or \defgroup ).
- Add a "General topics" entry for all pages that do not fit well in the previous "chapters".
- The highlevel struture is managed by a new eigendoxy_layout.xml file.
- remove the "index" and quite useless pages (namespace list, class hierarchy, member list, file list, etc.)
- add the javascript search-engine.
- add the "treeview" panel.
- remove \tableofcontents (replace them by a custom \eigenAutoToc macro to be able to easily re-enable if needed).
- add javascript to automatically generate a TOC from the h1/h2 tags of the current page, and put the TOC in the left side panel.
- overload various javascript function generated by doxygen to:
- remove the root of the treeview
- remove links to section/subsection from the treeview
- automatically expand the "Chapters" section
- automatically expand the current section
- adjust the height of the treeview to take into account the TOC
- always use the default .css file, eigendoxy.css now only includes our modifications
- use Doxyfile to specify our logo
- remove cross references to unsupported modules (temporarily)
2013-01-05 23:37:11 +08:00
\eigenAutoToc
2009-01-12 22:41:12 +08:00
2015-05-02 04:10:41 +08:00
\section StructHavingEigenMembers_summary Executive Summary
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
If you define a structure having members of \ref TopicFixedSizeVectorizable "fixed-size vectorizable Eigen types", you must ensure that calling operator new on it allocates properly aligned buffers.
If you're compiling in \cpp17 mode only with a sufficiently recent compiler (e.g., GCC>=7, clang>=5, MSVC>=19.12), then everything is taken care by the compiler and you can stop reading.
Otherwise, you have to overload its `operator new` so that it generates properly aligned pointers (e.g., 32-bytes-aligned for Vector4d and AVX).
Fortunately, %Eigen provides you with a macro `EIGEN_MAKE_ALIGNED_OPERATOR_NEW` that does that for you.
2009-01-12 22:41:12 +08:00
2015-05-02 04:10:41 +08:00
\section StructHavingEigenMembers_what What kind of code needs to be changed?
2009-01-12 22:41:12 +08:00
The kind of code that needs to be changed is this:
\code
class Foo
{
...
Eigen::Vector2d v;
...
};
...
Foo *foo = new Foo;
\endcode
2010-07-04 16:14:47 +08:00
In other words: you have a class that has as a member a \ref TopicFixedSizeVectorizable "fixed-size vectorizable Eigen object", and then you dynamically create an object of that class.
2009-01-12 22:41:12 +08:00
2015-05-02 04:10:41 +08:00
\section StructHavingEigenMembers_how How should such code be modified?
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
Very easy, you just need to put a `EIGEN_MAKE_ALIGNED_OPERATOR_NEW` macro in a public part of your class, like this:
2009-01-12 22:41:12 +08:00
\code
class Foo
{
...
2019-02-20 20:54:04 +08:00
Eigen::Vector4d v;
2009-01-12 22:41:12 +08:00
...
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
...
Foo *foo = new Foo;
\endcode
2019-02-20 20:54:04 +08:00
This macro makes `new Foo` always return an aligned pointer.
In \cpp17, this macro is empty.
2009-01-12 22:41:12 +08:00
2015-12-30 23:04:24 +08:00
If this approach is too intrusive, see also the \ref StructHavingEigenMembers_othersolutions "other solutions".
2011-11-25 20:46:48 +08:00
2015-05-02 04:10:41 +08:00
\section StructHavingEigenMembers_why Why is this needed?
2009-01-12 22:41:12 +08:00
OK let's say that your code looks like this:
\code
class Foo
{
...
2019-02-20 20:54:04 +08:00
Eigen::Vector4d v;
2009-01-12 22:41:12 +08:00
...
};
...
Foo *foo = new Foo;
\endcode
2019-02-20 20:54:04 +08:00
A Eigen::Vector4d consists of 4 doubles, which is 256 bits.
This is exactly the size of an AVX register, which makes it possible to use AVX for all sorts of operations on this vector.
But AVX instructions (at least the ones that %Eigen uses, which are the fast ones) require 256-bit alignment.
Otherwise you get a segmentation fault.
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
For this reason, %Eigen takes care by itself to require 256-bit alignment for Eigen::Vector4d, by doing two things:
\li %Eigen requires 256-bit alignment for the Eigen::Vector4d's array (of 4 doubles). With \cpp11 this is done with the <a href="https://en.cppreference.com/w/cpp/keyword/alignas">alignas</a> keyword, or compiler's extensions for c++98/03.
\li %Eigen overloads the `operator new` of Eigen::Vector4d so it will always return 256-bit aligned pointers. (removed in \cpp17)
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
Thus, normally, you don't have to worry about anything, %Eigen handles alignment of operator new for you...
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
... 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 256-bit aligned.
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
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!
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
The solution is to let `class Foo` have an aligned `operator new`, as we showed in the previous section.
This explanation also holds for SSE/NEON/MSA/Altivec/VSX targets, which require 16-bytes alignment, and AVX512 which requires 64-bytes alignment for fixed-size objects multiple of 64 bytes (e.g., Eigen::Matrix4d).
2009-01-12 22:41:12 +08:00
2015-05-02 04:10:41 +08:00
\section StructHavingEigenMembers_movetotop Should I then put all the members of Eigen types at the beginning of my class?
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
That's not required. Since %Eigen takes care of declaring adequate alignment, all members that need it are automatically aligned relatively to the class. So code like this works fine:
2009-01-12 22:41:12 +08:00
\code
class Foo
{
double x;
2019-02-20 20:54:04 +08:00
Eigen::Vector4d v;
2009-01-12 22:41:12 +08:00
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
\endcode
2019-02-20 20:54:04 +08:00
That said, as usual, it is recommended to sort the members so that alignment does not waste memory.
In the above example, with AVX, the compiler will have to reserve 24 empty bytes between `x` and `v`.
2015-05-02 04:10:41 +08:00
\section StructHavingEigenMembers_dynamicsize What about dynamic-size matrices and vectors?
2009-01-12 22:41:12 +08:00
2010-07-04 16:14:47 +08:00
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 \ref TopicFixedSizeVectorizable "fixed-size vectorizable matrices and vectors".
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
2015-05-02 04:10:41 +08:00
\section StructHavingEigenMembers_bugineigen So is this a bug in Eigen?
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
No, it's not our bug. It's more like an inherent problem of the c++ language specification that has been solved in c++17 through the feature known as <a href="http://wg21.link/p0035r4">dynamic memory allocation for over-aligned data</a>.
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
\section StructHavingEigenMembers_conditional What if I want to do this conditionally (depending on template parameters) ?
2009-01-12 22:41:12 +08:00
2019-02-20 20:54:04 +08:00
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.
In \cpp17, this macro is empty.
2009-01-12 22:41:12 +08:00
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
2011-11-25 20:46:48 +08:00
2015-05-02 04:10:41 +08:00
\section StructHavingEigenMembers_othersolutions Other solutions
2011-11-25 20:46:48 +08:00
2019-02-20 20:54:04 +08:00
In case putting the `EIGEN_MAKE_ALIGNED_OPERATOR_NEW` macro everywhere is too intrusive, there exists at least two other solutions.
2011-11-25 20:46:48 +08:00
\subsection othersolutions1 Disabling alignment
The first is to disable alignment requirement for the fixed size members:
\code
class Foo
{
...
2019-02-20 20:54:04 +08:00
Eigen::Matrix<double,4,1,Eigen::DontAlign> v;
2011-11-25 20:46:48 +08:00
...
};
\endcode
2019-02-20 20:54:04 +08:00
This `v` is fully compatible with aligned Eigen::Vector4d.
This has only for effect to make load/stores to `v` more expensive (usually slightly, but that's hardware dependent).
2011-11-25 20:46:48 +08:00
\subsection othersolutions2 Private structure
The second consist in storing the fixed-size objects into a private struct which will be dynamically allocated at the construction time of the main object:
\code
struct Foo_d
{
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
2019-02-20 20:54:04 +08:00
Vector4d v;
2011-11-25 20:46:48 +08:00
...
};
struct Foo {
Foo() { init_d(); }
~Foo() { delete d; }
void bar()
{
// use d->v instead of v
...
}
private:
void init_d() { d = new Foo_d; }
Foo_d* d;
};
\endcode
2019-02-20 20:54:04 +08:00
The clear advantage here is that the class `Foo` remains unchanged regarding alignment issues.
The drawback is that an additional heap allocation will be required whatsoever.
2011-11-25 20:46:48 +08:00
2009-01-12 22:41:12 +08:00
*/
}