特征矩阵+升压::序列化/ C ++ 17

问题描述 投票:0回答:2

我试图使下它强烈基于提升我们的代码库++ 17 - 和boost ::中间数据的存储和传输预系列化系列化。

总之,一切都看起来很好,似乎是工作,当我们序列化征:: Matrix对象,包括共享PTR系列化升压序列化支持,除了头。

在github例如最小/ testcode: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_ptrs得到到处连载。

难道你们的一个有什么想法,什么怎么回事?或者是在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年2月6日] GCC-错误:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84075 本征缺陷:http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1676

[编辑2019年2月7日] 提高PR:https://github.com/boostorg/serialization/pull/144

c++ gcc boost eigen boost-serialization
2个回答
1
投票

不知道的错误,但你为什么需要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

工作正常,我用C ++ 17提升1.58和最近Eigen3.3或铿锵5/6征默认版本,以及GCC 6/7/8。

我添加了一个matrix.resize()这将使动态矩阵代码工作,以及为固定大小的矩阵这应该是不引入开销(与优化编译) - 实际上它应该读不可调整大小的矩阵时断言(当没有-DNDEBUG编译) 。


如果你想保持你当前基于split_free序列化可以通过添加该模板专项工作,解决此问题。它需要包括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};};
} } 

2
投票

我猜(不可靠),其C ++ 14和C ++ 17之间的差异是由于模板参数推导(编译器不能排除模板,只是因为参数的个数过少),即GCC不作为处理SFINAE。我不知道如果bug在GCC或本征,但这里是一个更减少测试用例反正

#include <Eigen/Core>

template<template<class U>class SPT>void f(SPT<class U>&);
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
void f(Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & arMatrix){}

int main()
{
  Eigen::Matrix2d m;
  f(m);
}
© www.soinside.com 2019 - 2024. All rights reserved.