c++ - Eigen 矩阵 + Boost::Serialization/C++17

标签 c++ gcc boost eigen boost-serialization

我正在尝试为我们的代码库启用 C++17,它强烈基于 boost - 和用于中间数据存储和传输前序列化的 boost::serialization。

总的来说,一切看起来都很好并且似乎在工作,除了当我们序列化 Eigen::Matrix 对象时并且包含用于共享 ptr 序列化的 boost 序列化支持 header 。

github 上的最小示例/测试代码:https://github.com/nightsparc/EigenSerialize

[编辑] @Marc Glisse 在下方提供了一个简化的测试用例。请参阅:https://stackoverflow.com/a/54536756/1267320

我用不同的编译器(GCC6/7/8 和 Clang6)做了一些测试。我们通常使用 GCC 系统,即 Ubuntu 18.04 的 GCC7.3。 对我来说,这似乎是与 GCC7 及更高版本的 C++17 模式相关的问题。

我的意思是,我没有在最小示例中使用 shared_ptr,因此我可以将其删除,一切都会好起来的......尽管如此,在我们的代码库中,shared_ptr 会在任何地方被序列化。

你们中有人知道这里发生了什么吗?还是 GCC C++17 模式的错误?

测试代码(没有正确的错误处理和东西......):

#include <fstream>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/split_free.hpp>

#include <Eigen/Core>

// !! Conflicting include! Whenever the serialization wrapper for shared_ptrs is included
// the compilation fails!
// /usr/local/include/Eigen/src/Core/util/ForwardDeclarations.h:32:
// error: incomplete type ‘Eigen::internal::traits<boost::serialization::U>’ used in nested name specifier
// enum { has_direct_access = (traits<Derived>::Flags & DirectAccessBit) ? 1 : 0,
#include <boost/serialization/shared_ptr.hpp>

// Serialization methods for fixed-size Eigen::Matrix type
namespace boost {
    namespace serialization {
        template<
                class Archive,
                typename _Scalar,
                int _Rows,
                int _Cols,
                int _Options,
                int _MaxRows,
                int _MaxCols
                >
        inline void serialize(Archive & arArchive,
                              Eigen::Matrix<_Scalar,
                              _Rows,
                              _Cols,
                              _Options,
                              _MaxRows,
                              _MaxCols> & arMatrix,
                              const unsigned int aVersion)
        {
            boost::serialization::split_free(arArchive, arMatrix, aVersion);
        }

        template<
                class Archive,
                typename _Scalar,
                int _Rows,
                int _Cols,
                int _Options,
                int _MaxRows,
                int _MaxCols
                >
        inline void save(Archive & arArchive,
                         const Eigen::Matrix<_Scalar,
                         _Rows,
                         _Cols,
                         _Options,
                         _MaxRows,
                         _MaxCols> & arMatrix,
                         const unsigned int)
        {
            typedef typename Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Index TEigenIndex;

            const TEigenIndex lRows = arMatrix.rows();
            const TEigenIndex lCols = arMatrix.cols();

            arArchive << lRows;
            arArchive << lCols;

            if(lRows > 0 && lCols > 0)
            {
                arArchive & boost::serialization::make_array(arMatrix.data(), arMatrix.size());
            }
        }

        template<
                class Archive,
                typename _Scalar,
                int _Rows,
                int _Cols,
                int _Options,
                int _MaxRows,
                int _MaxCols
                >
        inline void load(Archive & arArchive,
                         Eigen::Matrix<_Scalar,
                         _Rows,
                         _Cols,
                         _Options,
                         _MaxRows,
                         _MaxCols> & arMatrix,
                         const unsigned int)
        {
            typedef typename Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Index TEigenIndex;

            TEigenIndex lRows, lCols;

            // deserialize meta data
            arArchive & lRows;
            arArchive & lCols;

            // do some error handling here

            if(lRows > 0 && lCols > 0)
            {
                // deserialize data
                arArchive & boost::serialization::make_array(arMatrix.data(), arMatrix.size());
            }
        }

    }
}

class TestClass
{
    public:
        TestClass()
        {
            // fill eigen
            m(0,0) = 3;
            m(1,0) = 2.5;
            m(0,1) = -1;
            m(1,1) = m(1,0) + m(0,1);
        }

    private:
        friend class boost::serialization::access;
        Eigen::Matrix2d m;

        template<class Archive>
        void serialize(Archive &ar, const unsigned int)
        {
            ar & m;
        }
};


int main(void)
{
    using namespace boost::archive;

    // Serialize
    TestClass TestA;
    std::ofstream oss("test.log");
    {
        text_oarchive oa(oss);
        oa << TestA;
    }

    // deserialize now
    TestClass TestB;
    std::ifstream iss("test.log");
    {
        text_iarchive ia(iss);
        ia >> TestB;
    }
}

[编辑 2019-02-06]
GCC 错误:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84075
Eigen 错误:http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1676

[编辑 2019-02-07]
boost 公关:https://github.com/boostorg/serialization/pull/144

最佳答案

不确定错误,但为什么需要 boost::serialization::split_free而不是简单地这样做:

// Serialization methods for fixed or dynamic-size Eigen::Matrix type
namespace boost {namespace serialization {
template<class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
inline void serialize(Archive & ar,
        Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & matrix,
        const unsigned int /* aVersion */)
{
    Eigen::Index rows = matrix.rows();
    Eigen::Index cols = matrix.cols();
    ar & (rows);
    ar & (cols);
    if(rows != matrix.rows() || cols != matrix.cols())
        matrix.resize(rows, cols);
    if(matrix.size() !=0)
        ar &  boost::serialization::make_array(matrix.data(), rows * cols);
}
} } // namespace boost::serialization

在 clang 5/6 和 gcc 6/7/8 上使用带有 boost 1.58 的 C++17 和最新的 Eigen3.3 或 Eigen-default 版本对我来说效果很好。

我添加了一个 matrix.resize()这应该使代码也适用于动态矩阵,对于固定大小的矩阵,这应该不会引入任何开销(当使用优化编译时)——实际上它应该在读取不可调整大小的矩阵时断言(当编译时没有 -DNDEBUG )。


如果您想保留当前的 ​​split_free -based 序列化,您可以通过添加此模板特化来解决此问题。它需要在包含 Eigen/Core 之后的某个地方, 在声明序列化之前,是否 <boost/serialization/shared_ptr.hpp> 并不重要是否包含在内。

namespace boost { namespace serialization {
   struct U;  // forward-declaration for Bug 1676
} } // boost::serialization 

namespace Eigen { namespace internal {
  // Workaround for bug 1676
  template<>
  struct traits<boost::serialization::U> {enum {Flags=0};};
} } 

关于c++ - Eigen 矩阵 + Boost::Serialization/C++17,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54534047/

相关文章:

c++ - 这个用于增长数组的 C++ 代码可能会中断吗?

c++ - EXTERN 或 STATIC 用于维护许多变量

c++ - 在 .o 文件中本地化函数体 block

c++ - 无法包含来自 boost 库的 header

c++ - 在 C++ 中制作可移植的字节序正确的文件读取/写入代码的简洁方法

c++ - 查找数组中连续空白的最大长度

c++ - 如何从 UTF-8 字符串的每个字符中获取 UNICODE 代码?

c++ - 它是 C++ gcc HEAD 10.0.0 20190 相对于友元函数的错误吗

C预处理器展开顺序

c++ - 如何将 int 复制到 boost/std::char 数组?