Boost :: serialization和boost :: mpi通过基类指针广播派生类

时间:2013-08-14 10:17:28

标签: c++ openmpi boost-serialization boost-mpi

我正在尝试使用boost :: mpi :: broadcast通过基类指针将派生类发送到所有节点。为此,我使用boost :: serialization库来序列化我的类。但是,我的代码没有编译,我得到错误“类boost :: mpi :: packed_skeleton_iarchive'没有名为'append'的成员”和“class boost :: mpi :: packed_skeleton_iarchive'没有名为'reset'的成员。 “

以下是该程序的粗略源代码:

// Base.hpp
#include <boost/serialization/serialization.hpp>

class Base
{
    public:
        Base() {}
        virtual ~Base() {}

        virtual void foo() = 0;
    private:
        friend class boost::serialization::access;

        template<class Archive>
        void serialize( Archive& /*ar*/, const unsigned int /*version*/ ) {}
}

// Derived.hpp
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/export.hpp>
#include <boost/shared_ptr.hpp>
#include <vector>

#include "Base.hpp"

class Derived : public Base
{
    public:
        Derived( int param );
        virtual ~Derived();

        void foo();
    private:
        int param_;
        std::vector< boost::shared_ptr > bar_;

        friend class boost::serialization::access;

        template<class Archive>
        void serialize( Archive& ar, const unsigned int /*version*/ )
        {
            ar & param_;
            ar & bar_;

            ar & boost::serialization::base_object< Base >( *this );
        }
}

namespace boost
{
    namespace serialization
    {
        template<class Archive>
        void load_construct_data( Archive& /*ar*/, Derived* d,
            const unsigned int /*v*/ )
        {
            ::new(d) Derived( 0 );
        }
    }
}

BOOST_CLASS_EXPORT_KEY( Derived )

// Derived.cpp
#include "Derived.hpp"

Derived::Derived( int param ) : param_( param ) {}
Derived::~Derived(){}

Derived::foo()
{
    // some stuff
}

BOOST_CLASS_EXPORT_IMPLEMENT( Derived )

// Main.cpp
#include <boost/mpi.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/shared_ptr.hpp>

#include "Derived.hpp"

int main( int argc, char* argv[] )
{
    boost::shared_ptr< Base > sp;

    if ( world.rank() == 0 )
    {
        sp = boost::shared_ptr< Base >( new Derived( 5 ) );

        boost::mpi::broadcast( world, sp, 0 );

        // produce some stuff
    } else
    {
        while ( 1 )
        {
           // consume some stuff
        }
    }

    return 0;
}

BOOST_SERIALIZATION_ASSUME_ABSTRACT( Base )

我发现只有一个关于此问题的Google网上论坛讨论但到目前为止还没有解决方案https://groups.google.com/forum/#!msg/boost-developers-archive/Ee9_ilEDO7s/cJTy-8v5lEcJ。我该如何编译?我正在使用openmpi 1.2.8-17.4,gcc 4.5.1和boost 1.54。

1 个答案:

答案 0 :(得分:1)

我很幸运能够回答我自己的问题(我认为。)当与boost :: mpi :: packed_skeleton_iarchive结合使用时,Boost无法正确处理通过基指针序列化。然后,解决方案是使用不同类型的存档,例如相反,text_iarchive / text_oarchive。

例如,播放:

std::ostringstream oss;
boost::archive::text_oarchive oa(oss);
oa << value;
std::string s = oss.str();
boost::mpi::broadcast(comm, s, root);

接收:

std::string s;
boost::mpi::broadcast(comm, s, root);
std::istringstream iss(s);
boost::archive::text_iarchive ia(iss);
ia >> value;