日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Eigen库对齐问题:declspec(align('16')) 的形参将不被对齐

發布時間:2023/12/31 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)

每種情況可以對照官方的說法。可以參考如下鏈接

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頭文件,就可以正常使用了。? 注意紅色部分.....

是微軟坑呢?還是Eigen坑?個人感覺微軟更坑!!!

后記:

?????? 添加 修改為 869行為引用之后,引發2562行的錯誤,修改方式為 把第二個參數0去掉。

總結

以上是生活随笔為你收集整理的Eigen库对齐问题:declspec(align('16')) 的形参将不被对齐的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。