如何通过Boost MPI传递序列化类的向量

How to pass vectors of serialized class via boost MPI?

本文关键字:序列化 向量 何通过 Boost MPI      更新时间:2023-10-16

我是并行计算和提升库的全新新手。但是在我当前的项目中,我需要发送/recv vector包含串行的类对象,并且大小将在运行时间内决定。阅读了Boost :: MPI和BOOST :: Serialization文档后,我在Google中搜索并使用VS2008对其进行编译时,发现以下代码,没有任何错误。

#include <boost/mpi.hpp>
#include <iostream>
#include <vector>
namespace mpi = boost::mpi;
class gps_position
{
private:
 friend class boost::serialization::access;
template<class Archive>
 void serialize(Archive &ar, const unsigned int version)
 {
   ar & degrees;
   ar & minutes;
   ar & seconds;
 }
public:
 int degrees;
 int minutes;
 float seconds;
 gps_position() {};
 gps_position(int d, int m, float s) :
 degrees(d), minutes(m), seconds(s)
 {}
};

int main(int argc, char *argv[])   {
  mpi::environment env(argc, argv);
  mpi::communicator world;
  if(world.rank() == 0) {
    std::vector<gps_position> positions;
    positions.push_back(gps_position(1, 2, 3.));
    positions.push_back(gps_position(5, 6, 10.0));
    std::cout<< "Sent GPS positions:"<<positions.size()<<std::endl;
    world.send(1, 0, positions);
  }
  else {
    std::vector<gps_position> positions;

    world.recv(0, 0, positions);
    std::cout << "Received GPS positions: "<<positions.size() << std::endl;
    for(unsigned int i=0;i<positions.size(); i++) {
      std::cout << positions[i].degrees << "t"
                << positions[i].minutes << "t"
                << positions[i].seconds << std::endl;
    }
  }
  return 0;
}

但是该程序无法正常工作。看起来Process1永远无法接收矢量包含来自Process0的gps_position对象。输出为

c:mpi3>mpiexec -n 2 mpitest 
Sent GPS positions:2 
Received GPS positions: 0

我已经修改了代码,以使其传递单个元素,而不是整个向量,并且可以完美地运行。因此,我完全不知道原始代码有什么问题。BOOST :: MPI是否能够通过这种类型的向量?任何建议都非常感谢。

预先感谢大家

zac

Boost说它可以处理向量...您的类型本身显然也可以处理。天真的我会期望工作。

从Boost文档中查看以下内容

将数据发送到另一个过程。

此例程执行一个潜在的阻塞发送标签, 与排名dest的过程。可以通过目标过程收到 带有匹配的RECV调用。

给定值必须适合通过MPI传输。有 满足这些要求的几类类型:

Types with mappings to MPI data types: If is_mpi_datatype<T> is convertible to mpl::true_, then value will be transmitted using the

MPI数据类型GET_MPI_DATATYPE()。所有原始的C 数据类型 具有MPI等效物,例如int,float,char,double等 内置映射到MPI数据类型。您可能会变成可序列化的类型 通过专业化将固定结构置于MPI数据类型 IS_MPI_DATATYPE用于您的类型。

Serializable types: Any type that provides the serialize() functionality required by the Boost.Serialization library can be

传输并接收。

Packed archives and skeletons: Data that has been packed into an mpi::packed_oarchive or the skeletons of data that have been backed

可以传输到mpi :: packed_skeleton_oarchive,但会是 接收为mpi :: packed_iarchive和mpi :: packed_skeleton_iarchive, 分别允许值(或骨骼)提取 目标过程。

Content: Content associated with a previously-transmitted skeleton can be transmitted by send and received by recv. The receiving process

只能将内容接收到已有的值的内容中 用匹配的骨骼构建。

对于具有映射到MPI数据类型的类型(包括 浓度为),调用此例程将导致 单个MPI_SEND调用。对于可变长度数据,例如序列化类型 和包装的档案,将通过mpi_send发送两条消息:一个 包含数据的长度和第二个包含数据的 本身。请注意,可变长度数据的传输模式是 实现细节可能会更改。

谢谢大家的帮助。

最终通过根据VS 2010进行重新编译解决了此问题。不确定根本原因。我想头部文件和libs中的一些不匹配?