Eigen库对齐问题:declspec(align('16')) 的形参将不被对齐
一:錯誤提示:error C2719: '_Val': formal parameter with __declspec(align('16')) won't be aligned
英文提示:error C2719: 'p': formal parameter with __declspec(align('16')) won't be aligned
中文提示:error C2719: “p”: 具有 __declspec(align('16')) 的形參將不被對齊
導致整個現象的主要原因是使用了Eigen庫,Eigen為了使用SSE加速,所以內存分配上使用了128位的指針。
更加準確的說法:
“First, "fixed-size" should be clear: an?Eigen?object has fixed size if its number of rows and its number of columns are fixed at compile-time. So for example Matrix3f has fixed size, but MatrixXf doesn't (the opposite of fixed-size is dynamic-size).
The array of coefficients of a fixed-size?Eigen?object is a plain "static array", it is not dynamically allocated. For example, the data behind a Matrix4f is just a "float array[16]".
Fixed-size objects are typically very small, which means that we want to handle them with zero runtime overhead -- both in terms of memory usage and of speed.
Now, vectorization (both SSE and AltiVec) works with 128-bit packets. Moreover, for performance reasons, these packets need to be have 128-bit alignment.
So it turns out that the only way that fixed-size?Eigen?objects can be vectorized, is if their size is a multiple of 128 bits, or 16 bytes.?Eigen?will then request 16-byte alignment for these objects, and henceforth rely on these objects being aligned so no runtime check for alignment is performed.”
二:解決方案:
分為四種情況:
- Cause 1: Structures having Eigen objects as members
- Cause 2: STL Containers
- Cause 3: Passing Eigen objects by value
- Cause 4: Compiler making a wrong assumption on stack alignment (for instance GCC on Windows)
每種情況可以對照官方的說法??梢詤⒖既缦骆溄?/span>
http://eigen.tuxfamily.org/dox/TopicUnalignedArrayAssert.html
在此不再重復表述。
三:我的解決方法:eigen 3.1.2有 bug,? 重裝Eigen;
??????????????? 關閉使用 預編譯頭;
??????????????? 關閉編譯選項里面的16位對齊;
??????????????? 把Eigen::Vector4f??? 傳值改為?傳引用 Eigen::Vector4f &;
??????????????? 把結構體 傳值 全部轉換為 傳引用。躲過對齊!!!
編譯器選項:C/C++ ---代碼生成---結構圖成員對齊;
可惡:忘記了當初是怎么解決的!!!!!
四:對stl vector 進行修改:http://blog.csdn.net/pkueecser/article/details/8602656
產生問題的resize()方法,改為:
????void?resize(size_type _Newsize,?const?_Ty?&?_Val)????{?//?determine new length, padding with _Val elements as needed
????????if?(size()?<?_Newsize)
????????????_Insert_n(end(), _Newsize?-?size(), _Val);
????????else?if?(_Newsize?<?size())
????????????erase(begin()?+?_Newsize, end());
????}
注意其中紅色部分,將傳值改為傳參,避免在參數棧上創建被對齊的結構的對象。然后,在我們使用std::vector< Foo<5, float> >之前包含我們的foo_vector.hpp頭文件,就可以正常使用了。? 注意紅色部分.....
后記:
?????? 添加 修改為 869行為引用之后,引發2562行的錯誤,修改方式為 把第二個參數0去掉。
總結
以上是生活随笔為你收集整理的Eigen库对齐问题:declspec(align('16')) 的形参将不被对齐的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: boost多线程使用简例
- 下一篇: 亚马逊云CEO谈生成式AI:十公里刚跑出