gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:05:42 24 4
gpt4 key购买 nike

我正在尝试使用 boost::mpi::broadcast 通过基类指针将派生类发送到所有节点。为此,我使用 boost::serialization 库来序列化我的类。但是,我的代码无法编译,并且出现错误“class 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 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。

最佳答案

谢天谢地,我能够回答我自己的问题(我认为)。当与 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;

关于c++ - Boost::serialization 和 boost::mpi 通过基类指针广播派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18229038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com