<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <meta name="generator" content="Doxygen 1.8.5"/> <title>Eigen: Structures Having Eigen Members</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <link href="navtree.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="resize.js"></script> <script type="text/javascript" src="navtree.js"></script> <script type="text/javascript"> $(document).ready(initResizable); $(window).load(resizeHeight); </script> <link href="search/search.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="search/search.js"></script> <script type="text/javascript"> $(document).ready(function() { searchBox.OnSelectItem(0); }); </script> <link href="doxygen.css" rel="stylesheet" type="text/css" /> <link href="eigendoxy.css" rel="stylesheet" type="text/css"> <!-- --> <script type="text/javascript" src="eigen_navtree_hacks.js"></script> <!-- <script type="text/javascript"> --> <!-- </script> --> </head> <body> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <!-- <a name="top"></a> --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td> <td style="padding-left: 0.5em;"> <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>  <span id="projectnumber">3.2.0</span> </div> </td> <td> <div id="MSearchBox" class="MSearchBoxInactive"> <span class="left"> <img id="MSearchSelect" src="search/mag_sel.png" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" alt=""/> <input type="text" id="MSearchField" value="Search" accesskey="S" onfocus="searchBox.OnSearchFieldFocus(true)" onblur="searchBox.OnSearchFieldFocus(false)" onkeyup="searchBox.OnSearchFieldChange(event)"/> </span><span class="right"> <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a> </span> </div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.8.5 --> <script type="text/javascript"> var searchBox = new SearchBox("searchBox", "search",false,'Search'); </script> </div><!-- top --> <div id="side-nav" class="ui-resizable side-nav-resizable"> <div id="nav-tree"> <div id="nav-tree-contents"> <div id="nav-sync" class="sync"></div> </div> </div> <div id="splitbar" style="-moz-user-select:none;" class="ui-resizable-handle"> </div> </div> <script type="text/javascript"> $(document).ready(function(){initNavTree('group__TopicStructHavingEigenMembers.html','');}); </script> <div id="doc-content"> <!-- window showing the filter options --> <div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)"> <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark"> </span>Pages</a></div> <!-- iframe showing the search results (closed by default) --> <div id="MSearchResultsWindow"> <iframe src="javascript:void(0)" frameborder="0" name="MSearchResults" id="MSearchResults"> </iframe> </div> <div class="header"> <div class="headertitle"> <div class="title">Structures Having Eigen Members<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__Alignement.html">Alignment issues</a></div></div> </div> </div><!--header--> <div class="contents"> <h1><a class="anchor" id="summary"></a> Executive Summary</h1> <p>If you define a structure having members of <a class="el" href="group__TopicFixedSizeVectorizable.html">fixed-size vectorizable Eigen types</a>, you must overload its "operator new" so that it generates 16-bytes-aligned pointers. Fortunately, <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> provides you with a macro EIGEN_MAKE_ALIGNED_OPERATOR_NEW that does that for you.</p> <h1><a class="anchor" id="what"></a> What kind of code needs to be changed?</h1> <p>The kind of code that needs to be changed is this:</p> <div class="fragment"><div class="line"><span class="keyword">class </span>Foo</div> <div class="line">{</div> <div class="line"> ...</div> <div class="line"> <a class="code" href="classEigen_1_1Matrix.html">Eigen::Vector2d</a> v;</div> <div class="line"> ...</div> <div class="line">};</div> <div class="line"></div> <div class="line">...</div> <div class="line"></div> <div class="line">Foo *foo = <span class="keyword">new</span> Foo;</div> </div><!-- fragment --><p>In other words: you have a class that has as a member a <a class="el" href="group__TopicFixedSizeVectorizable.html">fixed-size vectorizable Eigen object</a>, and then you dynamically create an object of that class.</p> <h1><a class="anchor" id="how"></a> How should such code be modified?</h1> <p>Very easy, you just need to put a EIGEN_MAKE_ALIGNED_OPERATOR_NEW macro in a public part of your class, like this:</p> <div class="fragment"><div class="line"><span class="keyword">class </span>Foo</div> <div class="line">{</div> <div class="line"> ...</div> <div class="line"> <a class="code" href="classEigen_1_1Matrix.html">Eigen::Vector2d</a> v;</div> <div class="line"> ...</div> <div class="line">public:</div> <div class="line"> EIGEN_MAKE_ALIGNED_OPERATOR_NEW</div> <div class="line">};</div> <div class="line"></div> <div class="line">...</div> <div class="line"></div> <div class="line">Foo *foo = <span class="keyword">new</span> Foo;</div> </div><!-- fragment --><p>This macro makes "new Foo" always return an aligned pointer.</p> <p>If this approach is too intrusive, see also the <a class="el" href="group__TopicStructHavingEigenMembers.html#othersolutions">Other solutions</a>.</p> <h1><a class="anchor" id="why"></a> Why is this needed?</h1> <p>OK let's say that your code looks like this:</p> <div class="fragment"><div class="line"><span class="keyword">class </span>Foo</div> <div class="line">{</div> <div class="line"> ...</div> <div class="line"> <a class="code" href="classEigen_1_1Matrix.html">Eigen::Vector2d</a> v;</div> <div class="line"> ...</div> <div class="line">};</div> <div class="line"></div> <div class="line">...</div> <div class="line"></div> <div class="line">Foo *foo = <span class="keyword">new</span> Foo;</div> </div><!-- fragment --><p>A <a class="el" href="group__matrixtypedefs.html#ga685d563d586f4820b4a2df9a07d98c23">Eigen::Vector2d</a> 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 <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> uses, which are the fast ones) require 128-bit alignment. Otherwise you get a segmentation fault.</p> <p>For this reason, <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> takes care by itself to require 128-bit alignment for <a class="el" href="group__matrixtypedefs.html#ga685d563d586f4820b4a2df9a07d98c23">Eigen::Vector2d</a>, by doing two things: </p> <ul> <li><a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> requires 128-bit alignment for the <a class="el" href="group__matrixtypedefs.html#ga685d563d586f4820b4a2df9a07d98c23">Eigen::Vector2d</a>'s array (of 2 doubles). With GCC, this is done with a <b>attribute</b> ((aligned(16))). </li> <li><a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> overloads the "operator new" of <a class="el" href="group__matrixtypedefs.html#ga685d563d586f4820b4a2df9a07d98c23">Eigen::Vector2d</a> so it will always return 128-bit aligned pointers.</li> </ul> <p>Thus, normally, you don't have to worry about anything, <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> handles alignment for you...</p> <p>... 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.</p> <p>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!</p> <p>The solution is to let class Foo have an aligned "operator new", as we showed in the previous section.</p> <h1><a class="anchor" id="movetotop"></a> Should I then put all the members of Eigen types at the beginning of my class?</h1> <p>That's not required. Since <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library. ">Eigen</a> 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:</p> <div class="fragment"><div class="line"><span class="keyword">class </span>Foo</div> <div class="line">{</div> <div class="line"> <span class="keywordtype">double</span> x;</div> <div class="line"> <a class="code" href="classEigen_1_1Matrix.html">Eigen::Vector2d</a> v;</div> <div class="line"><span class="keyword">public</span>:</div> <div class="line"> EIGEN_MAKE_ALIGNED_OPERATOR_NEW</div> <div class="line">};</div> </div><!-- fragment --><h1><a class="anchor" id="dynamicsize"></a> What about dynamic-size matrices and vectors?</h1> <p>Dynamic-size matrices and vectors, such as <a class="el" href="group__matrixtypedefs.html#ga3da45e59796fbacf67fa568297927bd1">Eigen::VectorXd</a>, 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 <a class="el" href="group__TopicFixedSizeVectorizable.html">fixed-size vectorizable matrices and vectors</a>.</p> <h1><a class="anchor" id="bugineigen"></a> So is this a bug in Eigen?</h1> <p>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>.</p> <h1><a class="anchor" id="conditional"></a> What if I want to do this conditionnally (depending on template parameters) ?</h1> <p>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.</p> <p>Example:</p> <div class="fragment"><div class="line"><span class="keyword">template</span><<span class="keywordtype">int</span> n> <span class="keyword">class </span>Foo</div> <div class="line">{</div> <div class="line"> <span class="keyword">typedef</span> <a class="code" href="classEigen_1_1Matrix.html">Eigen::Matrix<float,n,1></a> Vector;</div> <div class="line"> <span class="keyword">enum</span> { NeedsToAlign = (<span class="keyword">sizeof</span>(Vector)%16)==0 };</div> <div class="line"> ...</div> <div class="line"> Vector v;</div> <div class="line"> ...</div> <div class="line">public:</div> <div class="line"> EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)</div> <div class="line">};</div> <div class="line"></div> <div class="line">...</div> <div class="line"></div> <div class="line">Foo<4> *foo4 = <span class="keyword">new</span> Foo<4>; <span class="comment">// foo4 is guaranteed to be 128bit-aligned</span></div> <div class="line">Foo<3> *foo3 = <span class="keyword">new</span> Foo<3>; <span class="comment">// foo3 has only the system default alignment guarantee</span></div> </div><!-- fragment --><h1><a class="anchor" id="othersolutions"></a> Other solutions</h1> <p>In case putting the EIGEN_MAKE_ALIGNED_OPERATOR_NEW macro everywhere is too intrusive, there exists at least two other solutions.</p> <h2><a class="anchor" id="othersolutions1"></a> Disabling alignment</h2> <p>The first is to disable alignment requirement for the fixed size members: </p> <div class="fragment"><div class="line"><span class="keyword">class </span>Foo</div> <div class="line">{</div> <div class="line"> ...</div> <div class="line"> <a class="code" href="classEigen_1_1Matrix.html">Eigen::Matrix<double,2,1,Eigen::DontAlign></a> v;</div> <div class="line"> ...</div> <div class="line">};</div> </div><!-- fragment --><p> This has for effect to disable vectorization when using <code>v</code>. 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: </p> <div class="fragment"><div class="line"><span class="keywordtype">void</span> Foo::bar()</div> <div class="line">{</div> <div class="line"> <a class="code" href="classEigen_1_1Matrix.html">Eigen::Vector2d</a> av(v);</div> <div class="line"> <span class="comment">// use av instead of v</span></div> <div class="line"> ...</div> <div class="line"> <span class="comment">// if av changed, then do:</span></div> <div class="line"> v = av;</div> <div class="line">}</div> </div><!-- fragment --><h2><a class="anchor" id="othersolutions2"></a> Private structure</h2> <p>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:</p> <div class="fragment"><div class="line"><span class="keyword">struct </span>Foo_d</div> <div class="line">{</div> <div class="line"> EIGEN_MAKE_ALIGNED_OPERATOR_NEW</div> <div class="line"> <a class="code" href="group__matrixtypedefs.html#ga685d563d586f4820b4a2df9a07d98c23">Vector2d</a> v;</div> <div class="line"> ...</div> <div class="line">};</div> <div class="line"></div> <div class="line"></div> <div class="line"><span class="keyword">struct </span>Foo {</div> <div class="line"> Foo() { init_d(); }</div> <div class="line"> ~Foo() { <span class="keyword">delete</span> d; }</div> <div class="line"> <span class="keywordtype">void</span> bar()</div> <div class="line"> {</div> <div class="line"> <span class="comment">// use d->v instead of v</span></div> <div class="line"> ...</div> <div class="line"> }</div> <div class="line"><span class="keyword">private</span>:</div> <div class="line"> <span class="keywordtype">void</span> init_d() { d = <span class="keyword">new</span> Foo_d; }</div> <div class="line"> Foo_d* d;</div> <div class="line">};</div> </div><!-- fragment --><p>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. </p> </div><!-- contents --> </div><!-- doc-content --> <!-- start footer part --> <div id="nav-path" class="navpath"><!-- id is needed for treeview function! --> <ul> <li class="footer">Generated on Mon Oct 28 2013 11:04:27 for Eigen by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.5 </li> </ul> </div> <!-- Piwik --> <!-- <script type="text/javascript"> var pkBaseURL = (("https:" == document.location.protocol) ? "https://stats.sylphide-consulting.com/piwik/" : "http://stats.sylphide-consulting.com/piwik/"); document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E")); </script><script type="text/javascript"> try { var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 20); piwikTracker.trackPageView(); piwikTracker.enableLinkTracking(); } catch( err ) {} </script><noscript><p><img src="http://stats.sylphide-consulting.com/piwik/piwik.php?idsite=20" style="border:0" alt="" /></p></noscript> --> <!-- End Piwik Tracking Code --> </body> </html>