3

我正在尝试使用 boost::mpi::broadcast 通过基类指针将派生类发送到所有节点。为此,我使用 boost::serialization 库来序列化我的类。但是,我的代码无法编译,并且出现错误“class boost::mpi::packed_skeleton_iarchive' has no member named 'append'”和“class boost::mpi::packed_skeleton_iarchive' has no member named '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 Groups 讨论,但到目前为止还没有解决方案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。

4

1 回答 1

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;
于 2013-08-15T12:08:28.610 回答