gpt4 book ai didi

c++ - Eigen 未对齐数组断言

转载 作者:搜寻专家 更新时间:2023-10-31 01:34:40 26 4
gpt4 key购买 nike

我有一个类可以找到一组二维点的凸包。它包含一个具有 2 Eigen::Matrix<double, 2, 1> 的结构里面。它看起来像这样(删除了很​​多东西):

class Foo{
public:
Foo(Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>);

private:
struct edge{
unsigned int a;
unsigned int b;
Eigen::Matrix<double, 2, 1> slope;
double mag;
Eigen::Matrix<double, 2, 1> normal;
std::vector<unsigned int> list;
};

Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> points;
std::vector<edge> edges
};

向 vector 添加边时出现断言错误 edges像这样:

void Foo::DoThing(){
edges.push_back(edge());
}

准确的错误是:

Assertion failed: (reinterpret_cast(array) & 0xf) == 0 && "this assertio n is explained here: " "http://eigen.tuxfamily.org/dox-devel/group__TopicUnalign edArrayAssert.html" " **** READ THIS WEB PAGE !!! ****", file \blah\blah\blah\includes\eigen\eigen-eigen-dc6cfdf9bcec\eigen\src\core\densestorage.h, line 86

我去了网页,读到我需要添加这个宏: EIGEN_MAKE_ALIGNED_OPERATOR_NEW我将其添加到结构中,如下所示:

struct edge{
unsigned int a;
unsigned int b;
Eigen::Matrix<double, 2, 1> slope;
double mag;
Eigen::Matrix<double, 2, 1> normal;
std::vector<unsigned int> list;

EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};

但错误仍然存​​在。这发生在 VS 2013 和 Eigen 版本 3.2.9 中。未经修改的相同代码适用于 3.2.0 版。在带有 GCC 5.3 版的 Linux 上,它可以很好地与 Eigen 的 beta 版本一起工作。我在这个较新版本上做错了什么导致这个问题?

最佳答案

你有一个 std::vector包含要对齐的固定大小的 Eigen 类型。问题是 std::vector<edge>其中 edge包含固定大小对齐的 Eigen::Matrix<double, 2, 1> .在这里,EIGEN_MAKE_ALIGNED_OPERATOR_NEW还不够,您还需要选择 Eigen 的对齐分配器,如 https://eigen.tuxfamily.org/dox/group__TopicStlContainers.html 中所述

Using STL containers on fixed-size vectorizable Eigen types, or classes >having members of such types, requires taking the following two steps: (i) A 16-byte-aligned allocator must be used. Eigen does provide one ready for use: aligned_allocator. (ii) If you want to use the std::vector container, you need to #include <Eigen/StdVector>.

您应该大致像这样更正您的代码(未测试):

#include<Eigen/StdVector>
class Foo {
...
std::vector<edge,Eigen::aligned_allocator<edge> > edges;
}

警告:在 Linux 上你很幸运它能工作(可能是因为 32 位/64 位),你的代码真的应该得到纠正。

关于c++ - Eigen 未对齐数组断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38883424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com