我正在尝试通过MPI找到一种正确的方法来发送自定义序列化对象(不是自定义MPI结构 - 请参阅下面的定义)。在阅读了几个材料和stackoverflow之后,我有一个工作示例,它使用boost::serialization
并将序列化对象发送为stringstream
。但是,我当前的解决方案看起来有点 hackish ,请参阅下面的快照(完整代码附在末尾部分)。
我的问题:您能就当前解决方案发表意见并推荐一些行业认可的方式来发送自定义序列化对象吗?
限制:不幸的是,
boost.mpi
不是一个选项,因为它与openmpi
的依赖关系在ubuntu-xenial
基础架构上存在与TCP相关的错误。我只使用纯mpich
。
自定义对象:在我的示例中,自定义对象序列化了其
base
类,std::vector
,boost::shared_ptr
和其他一些简单变量。
这是我发送/接收信息流的小程序快照。
if (rank == 1) {
std::stringstream mystream;
//...more serialization code here
int len = mystream.str().size();
MPI_Send( &len, 1, MPI_INT, 1, lentag, MPI_COMM_WORLD );
MPI_Send( (void *)mystream.str().c_str(), len, MPI_BYTE, 1, datatag, MPI_COMM_WORLD );
} else if (rank == 1) {
int len;
MPI_Recv( &len, 1, MPI_INT, 0, lentag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
char data[len+1];
MPI_Recv( data, len, MPI_BYTE, 0, datatag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
data[len] = '\0';
std::stringstream mystream;
mystream.write((const char*) data, len);
//...more deserialization code here
}
这是程序输出。您可以看到数据已成功从rank 0
转移到rank 1
。
$ mpirun.mpich -np 2 ./mpidata
Rank 0 sum in 6
Rank 0 vsize out 4
Rank 0 ptr out 30
Rank 1 sum in 6
Rank 1 vsize in 4
Rank 1 ptr in 30
完整代码如下。
#include <mpi.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
// Forward declaration of class boost::serialization::access
namespace boost {
namespace serialization {
class access;
}
}
class Obj {
public:
// Serialization expects the object to have a default constructor
Obj() : d1_(-1), d2_(-2) {}
Obj(int d1, int d2) : d1_(d1), d2_(d2) {}
bool operator==(const Obj& o) const {
return d1_ == o.d1_ && d2_ == o.d2_;
}
const int sum() const {return d1_+d2_;}
private:
int d1_;
int d2_;
// Allow serialization to access non-public data members.
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive& ar, const unsigned version) {
ar & d1_ & d2_; // Simply serialize the data members of Obj
}
};
class ObjChild : public Obj {
private:
typedef Obj _Super;
public:
ObjChild() : Obj(),d1_(-1),dv_{1,2},iptr_(new Obj()) {}
ObjChild(
int d1,
int d2,
int d1new,
std::vector<int> const& dv,
boost::shared_ptr<Obj> const& obj
) : Obj(d1,d2),d1_(d1new),dv_(dv),iptr_(obj) {}
const int sum2() const {return d1_ + sum();}
const int vsize() const {return dv_.size();}
const int ptrsum() const {return iptr_->sum();}
private:
int d1_; // Another d1_
std::vector<int> dv_;
boost::shared_ptr<Obj> iptr_;
// -------------------------------------------------------------
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive& ar, const unsigned version) {
ar & boost::serialization::base_object<_Super>(*this);
ar & d1_;
ar & dv_;
ar & iptr_;
}
// -------------------------------------------------------------
};
int main(int argc,char** argv) {
int size, rank;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (size < 2) {
if (rank == 0)
std::cerr << "Require at least 2 tasks" << std::endl;
MPI_Abort(MPI_COMM_WORLD, 1);
}
const int lentag=0;
const int datatag=1;
if (rank == 0) {
std::stringstream mystream;
ObjChild obj(1,3,2,{1,2,3,4},boost::make_shared<Obj>(10,20));
boost::archive::binary_oarchive oarchive{mystream};
oarchive << obj;
std::cout<<"Rank "<< rank << " sum in " << obj.sum2() << std::endl;
std::cout<<"Rank "<< rank << " vsize out " << obj.vsize() << std::endl;
std::cout<<"Rank "<< rank << " ptr out " << obj.ptrsum() << std::endl;
int len = mystream.str().size();
// Send length, then data
MPI_Send( &len, 1, MPI_INT, 1, lentag, MPI_COMM_WORLD );
MPI_Send( (void *)mystream.str().c_str(), len, MPI_BYTE, 1, datatag, MPI_COMM_WORLD );
} else if (rank == 1) {
int len;
MPI_Recv( &len, 1, MPI_INT, 0, lentag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
char data[len+1];
MPI_Recv( data, len, MPI_BYTE, 0, datatag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
data[len] = '\0';
std::stringstream mystream;
mystream.write((const char*) data, len);
boost::archive::binary_iarchive iarchive(mystream);
ObjChild obj;
iarchive >> obj;
std::cout<<"Rank "<< rank << " sum in "<< obj.sum2() << std::endl;
std::cout<<"Rank "<< rank << " vsize in " << obj.vsize() << std::endl;
std::cout<<"Rank "<< rank << " ptr in " << obj.ptrsum() << std::endl;
}
MPI_Finalize();
return EXIT_SUCCESS;
}
答案 0 :(得分:2)
如果您正在使用Boost.Serialization,那么绝对明智的做法是使用Boost.MPI。这基本上会隐藏通信部分的所有序列化样板,如下所示:
boost::mpi::environment env;
boost::mpi::communicator world;
auto rank = world.rank();
if (world.size() < 2) {
if (rank == 0)
std::cerr << "Require at least 2 tasks" << std::endl;
MPI_Abort(MPI_COMM_WORLD, 1);
}
const int datatag = 1;
if (rank == 0) {
ObjChild obj(1, 3, 2, {1, 2, 3, 4}, boost::make_shared<Obj>(10, 20));
std::cout << "Rank " << rank << " sum in " << obj.sum2() << std::endl;
std::cout << "Rank " << rank << " vsize out " << obj.vsize() << std::endl;
std::cout << "Rank " << rank << " ptr out " << obj.ptrsum() << std::endl;
world.send(1, datatag, obj);
} else if (rank == 1) {
ObjChild obj;
world.recv(0, datatag, obj);
std::cout << "Rank " << rank << " sum in " << obj.sum2() << std::endl;
std::cout << "Rank " << rank << " vsize in " << obj.vsize() << std::endl;
std::cout << "Rank " << rank << " ptr in " << obj.ptrsum() << std::endl;
}
POD等某些类型可能会因额外指定is_mpi_datatype
而受益,但ObjChild
因指针而无法获得资格。
不幸的是,尽管有很好的声誉,但Boost.MPI似乎几乎没有得到维护,基本问题没有得到解决甚至讨论。对序列化对象的非阻塞通信要特别小心。因此,如果您不愿意投资修复自己的内容,我无法推荐Boost.MPI作为生产代码。这可能比自己从地面建造更好。另请注意,序列化,特别是Boost的实现速度相当慢,可能不适合某些HPC用例,在这种情况下,最好将内存布局设计为首先不需要任何序列化或复杂打包。