You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.1 KiB

  1. namespace StormEigen {
  2. /** \eigenManualPage TopicStructHavingEigenMembers Structures Having Eigen Members
  3. \eigenAutoToc
  4. \section StructHavingEigenMembers_summary Executive Summary
  5. If you define a structure having members of \ref TopicFixedSizeVectorizable "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 STORMEIGEN_MAKE_ALIGNED_OPERATOR_NEW that does that for you.
  6. \section StructHavingEigenMembers_what What kind of code needs to be changed?
  7. The kind of code that needs to be changed is this:
  8. \code
  9. class Foo
  10. {
  11. ...
  12. StormEigen::Vector2d v;
  13. ...
  14. };
  15. ...
  16. Foo *foo = new Foo;
  17. \endcode
  18. 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.
  19. \section StructHavingEigenMembers_how How should such code be modified?
  20. Very easy, you just need to put a STORMEIGEN_MAKE_ALIGNED_OPERATOR_NEW macro in a public part of your class, like this:
  21. \code
  22. class Foo
  23. {
  24. ...
  25. StormEigen::Vector2d v;
  26. ...
  27. public:
  28. STORMEIGEN_MAKE_ALIGNED_OPERATOR_NEW
  29. };
  30. ...
  31. Foo *foo = new Foo;
  32. \endcode
  33. This macro makes "new Foo" always return an aligned pointer.
  34. If this approach is too intrusive, see also the \ref othersolutions.
  35. \section StructHavingEigenMembers_why Why is this needed?
  36. OK let's say that your code looks like this:
  37. \code
  38. class Foo
  39. {
  40. ...
  41. StormEigen::Vector2d v;
  42. ...
  43. };
  44. ...
  45. Foo *foo = new Foo;
  46. \endcode
  47. A StormEigen::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.
  48. For this reason, Eigen takes care by itself to require 128-bit alignment for StormEigen::Vector2d, by doing two things:
  49. \li Eigen requires 128-bit alignment for the StormEigen::Vector2d's array (of 2 doubles). With GCC, this is done with a __attribute__ ((aligned(16))).
  50. \li Eigen overloads the "operator new" of StormEigen::Vector2d so it will always return 128-bit aligned pointers.
  51. Thus, normally, you don't have to worry about anything, Eigen handles alignment for you...
  52. ... 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.
  53. 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!
  54. The solution is to let class Foo have an aligned "operator new", as we showed in the previous section.
  55. \section StructHavingEigenMembers_movetotop Should I then put all the members of Eigen types at the beginning of my class?
  56. That's not required. Since Eigen takes care of declaring 128-bit alignment, all members that need it are automatically 128-bit aligned relatively to the class. So code like this works fine:
  57. \code
  58. class Foo
  59. {
  60. double x;
  61. StormEigen::Vector2d v;
  62. public:
  63. STORMEIGEN_MAKE_ALIGNED_OPERATOR_NEW
  64. };
  65. \endcode
  66. \section StructHavingEigenMembers_dynamicsize What about dynamic-size matrices and vectors?
  67. Dynamic-size matrices and vectors, such as StormEigen::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".
  68. \section StructHavingEigenMembers_bugineigen So is this a bug in Eigen?
  69. No, it's not our bug. It's more like an inherent problem of the C++98 language specification, and seems to be taken care of in the upcoming language revision: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2341.pdf">see this document</a>.
  70. \section StructHavingEigenMembers_conditional What if I want to do this conditionnally (depending on template parameters) ?
  71. For this situation, we offer the macro STORMEIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign). It will generate aligned operators like STORMEIGEN_MAKE_ALIGNED_OPERATOR_NEW if NeedsToAlign is true. It will generate operators with the default alignment if NeedsToAlign is false.
  72. Example:
  73. \code
  74. template<int n> class Foo
  75. {
  76. typedef StormEigen::Matrix<float,n,1> Vector;
  77. enum { NeedsToAlign = (sizeof(Vector)%16)==0 };
  78. ...
  79. Vector v;
  80. ...
  81. public:
  82. STORMEIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
  83. };
  84. ...
  85. Foo<4> *foo4 = new Foo<4>; // foo4 is guaranteed to be 128bit-aligned
  86. Foo<3> *foo3 = new Foo<3>; // foo3 has only the system default alignment guarantee
  87. \endcode
  88. \section StructHavingEigenMembers_othersolutions Other solutions
  89. In case putting the STORMEIGEN_MAKE_ALIGNED_OPERATOR_NEW macro everywhere is too intrusive, there exists at least two other solutions.
  90. \subsection othersolutions1 Disabling alignment
  91. The first is to disable alignment requirement for the fixed size members:
  92. \code
  93. class Foo
  94. {
  95. ...
  96. StormEigen::Matrix<double,2,1,StormEigen::DontAlign> v;
  97. ...
  98. };
  99. \endcode
  100. This has for effect to disable vectorization when using \c v.
  101. If a function of Foo uses it several times, then it still possible to re-enable vectorization by copying it into an aligned temporary vector:
  102. \code
  103. void Foo::bar()
  104. {
  105. StormEigen::Vector2d av(v);
  106. // use av instead of v
  107. ...
  108. // if av changed, then do:
  109. v = av;
  110. }
  111. \endcode
  112. \subsection othersolutions2 Private structure
  113. 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:
  114. \code
  115. struct Foo_d
  116. {
  117. STORMEIGEN_MAKE_ALIGNED_OPERATOR_NEW
  118. Vector2d v;
  119. ...
  120. };
  121. struct Foo {
  122. Foo() { init_d(); }
  123. ~Foo() { delete d; }
  124. void bar()
  125. {
  126. // use d->v instead of v
  127. ...
  128. }
  129. private:
  130. void init_d() { d = new Foo_d; }
  131. Foo_d* d;
  132. };
  133. \endcode
  134. The clear advantage here is that the class Foo remains unchanged regarding alignment issues. The drawback is that a heap allocation will be required whatsoever.
  135. */
  136. }