简体   繁体   English

C ++ Eigen Matrix澄清

[英]C++ Eigen Matrix clarifications

I have only recently started exploring C++ Eigen library and a little puzzled with some of the documentation. 我最近才开始探索C ++ Eigen库,并对一些文档感到有些困惑。 It would be great if someone can clarify this. 如果有人能澄清这一点,那就太棒了。

  1. In the common pitfalls ( https://eigen.tuxfamily.org/dox-devel/TopicPitfalls.html ) Alignment Issues section, it says " Indeed, since C++17, C++ does not have quite good enough support for explicit data alignment.". 在常见的陷阱( https://eigen.tuxfamily.org/dox-devel/TopicPitfalls.html对齐问题部分,它说“确实,自C ++ 17以来,C ++对显式数据对齐没有足够的支持“。

    The page on how to get rid of alignment issues ( https://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html#getrid ), the documentation says, "If you can target [c++17] only with a recent compiler (eg, GCC>=7, clang>=5, MSVC>=19.12), then you're lucky: enabling c++17 should be enough" . 关于如何摆脱对齐问题的页面( https://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html#getrid ),文档说,“如果你只能用一个目标[c ++ 17]最近的编译器(例如,GCC> = 7,clang> = 5,MSVC> = 19.12),那么你很幸运:启用c ++ 17就足够了“。

    So is alignment not an issue with Eigen Matrix if I am using c++ 17 with gcc>=7.0? 因此,如果我使用c ++ 17和gcc> = 7.0,那么对齐不是Eigen Matrix的问题吗? Have I understood this right? 我明白了吗? And that the macro EIGEN_MAKE_ALIGNED_OPERATOR_NEW won't be needed? 并且不需要宏EIGEN_MAKE_ALIGNED_OPERATOR_NEW? And if this is correct, what is different between c++14/c++17 which takes care of the alignment issues? 如果这是正确的,c ++ 14 / c ++ 17之间有什么不同,它会处理对齐问题?

  2. The second question is regarding the pass-by-value section ( https://eigen.tuxfamily.org/dox-devel/group__TopicPassingByValue.html ). 第二个问题是关于传值的部分( https://eigen.tuxfamily.org/dox-devel/group__TopicPassingByValue.html )。 The documentation claims that pass-by-value could be illegal and could crash the program. 该文档声称,按值传递可能是非法的,并可能导致程序崩溃。 This is very puzzling to me. 这对我来说非常令人费解。 Wouldn't pass-by-value just invoke a copy constructor? 不会传递值只是调用复制构造函数? As an example. 举个例子。

Eigen::Vector3f veca = ComputeVecA();
Eigen::Vector3f vecb = veca; //< If pass-by-value is unsafe, is this operation safe?
  1. And lastly, can I rely on RVO/NRVO for Eigen fixed sized matrix class? 最后,我可以依靠RVO / NRVO作为特征固定大小的矩阵类吗? I suspect the answer to this is yes. 我怀疑答案是肯定的。

In the common pitfalls ( https://eigen.tuxfamily.org/dox-devel/TopicPitfalls.html ) Alignment Issues section, it says "Indeed, since C++17, C++ does not have quite good enough support for explicit data alignment." 在常见的陷阱( https://eigen.tuxfamily.org/dox-devel/TopicPitfalls.html对齐问题部分,它说“确实,自C ++ 17以来,C ++对显式数据对齐没有足够的支持“。

This seems to be a typo. 这似乎是一个错字。 It should say "until C++17" instead of "since C++17" because C++17 actually added support for allocation with extraordinary alignment restrictions. 它应该说“直到C ++ 17”而不是“自C ++ 17以来”,因为C ++ 17实际上增加了对具有非凡对齐限制的分配的支持。 Two comments agree with me. 两条 评论同意我的观点

The page on how to get rid of alignment issues ( https://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html#getrid ), the documentation says, "If you can target [C++17] only with a recent compiler (eg, GCC >= 7, Clang >= 5, MSVC >= 19.12), then you're lucky: enabling C++17 should be enough." 关于如何摆脱对齐问题的页面( https://eigen.tuxfamily.org/dox-devel/group__TopicUnalignedArrayAssert.html#getrid ),文档说,“如果你只能用一个目标[C ++ 17]最近的编译器(例如,GCC> = 7,Clang> = 5,MSVC> = 19.12),那么你很幸运:启用C ++ 17就足够了。“

So is alignment not an issue with Eigen Matrix if I am using C++17 with gcc >= 7.0? 因此,如果我使用C ++ 17并且gcc> = 7.0,那么对齐不是Eigen Matrix的问题吗? Have I understood this right? 我明白了吗? And that the macro EIGEN_MAKE_ALIGNED_OPERATOR_NEW won't be needed? 并且不需要宏EIGEN_MAKE_ALIGNED_OPERATOR_NEW

Yes. 是。

And if this is correct, what is different between C++14/C++17 which takes care of the alignment issues? 如果这是正确的,C ++ 14 / C ++ 17之间有什么不同,它会处理对齐问题?

C++17 supports Dynamic memory allocation for over-aligned data . C ++ 17支持过度对齐数据的动态内存分配 operator new now properly allocates over-aligned memory with the align_val_t argument. operator new现在可以使用align_val_t参数正确分配过度对齐的内存。

The second question is regarding the pass-by-value section ( https://eigen.tuxfamily.org/dox-devel/group__TopicPassingByValue.html ). 第二个问题是关于传值的部分( https://eigen.tuxfamily.org/dox-devel/group__TopicPassingByValue.html )。 The documentation claims that pass-by-value could be illegal and could crash the program. 该文档声称,按值传递可能是非法的,并可能导致程序崩溃。 This is very puzzling to me. 这对我来说非常令人费解。 Wouldn't pass-by-value just invoke a copy constructor? 不会传递值只是调用复制构造函数?

If the variable is a local variable (as vecb in your example), then the compiler and the library take care to ensure that vecb meets the special alignment restriction required by Eigen. 如果变量是局部变量(在示例中为vecb ),则编译器和库会注意确保vecb满足Eigen所需的特殊对齐限制。 However, if the variable is a function parameter, this alignment restriction is not respected, meaning the program may operate on ill-aligned memory, thus crash. 但是,如果变量是函数参数,则不遵守此对齐限制,这意味着程序可能在不对齐的内存上运行,从而崩溃。 (This has little to do with the copy constructor.) (这与复制构造函数没什么关系。)

And lastly, can I rely on RVO/NRVO for Eigen fixed sized matrix class? 最后,我可以依靠RVO / NRVO作为特征固定大小的矩阵类吗? I suspect the answer to this is yes. 我怀疑答案是肯定的。

The answer is pretty much the same for Eigen classes and other classes: try and see. 对于Eigen类和其他类,答案几乎相同:试试看。 Usually the answer is yes. 通常答案是肯定的。

  • Q1: as already commented, this was a typo when updating this paragraph for c++17. Q1:正如已经评论的那样,在为c ++ 17更新此段落时,这是一个错字。 This is already fixed. 这已经修复了。

  • Q2: I don't remember all the details about this one but it is related to two technical issues. Q2:我不记得有关这个的所有细节,但它与两个技术问题有关。

    1. Some compilers failed to properly align the stack, in this case it is hopeless to get aligned function parameters. 有些编译器无法正确对齐堆栈,在这种情况下,获取对齐的函数参数是没有希望的。
    2. Old ABI specifications did not allowed overalignment of function parameters. 旧的ABI规范不允许对齐函数参数。 I would expect that since c++11 and the usage of the standardized alignas keyword this is not an issue anymore, but maybe this is still a problem on some exotic compiler-OS combinations. 我希望,因为c ++ 11和标准化的alignas关键字的使用这不再是一个问题,但也许这仍然是一些alignas编译器alignas组合的问题。
  • Q3: there is nothing preventing RVO/NRVO, and from my experience when it can apply it does apply. 问题3:没有什么可以阻止RVO / NRVO,根据我的经验,它可以应用它确实适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM