c++ - 使用带有 C MPI 函数的 boost 序列化

标签 c++ serialization boost mpi

谁能给我一个将 boost 序列化与 C MPI 函数混合使用的示例?

我想我需要使用 boost::mpi::oarchive。但是我应该如何初始化缓冲区参数,然后我应该将什么传递给 MPI_Send 呢?

更具体地说,我正在尝试执行以下操作:

mpi::environment env;
mpi::communicator world;
typedef vector <int> ParticleList_t;
#define MSG_LEN 100000
ParticleList_t sendbuf(MSG_LEN, 1), recvbuf(MSG_LEN);
mpi::packed_oarchive::buffer_type buffer(sizeof(sendbuf[0])*sendbuf.size());  
mpi::packed_oarchive oa(world, buffer, boost::archive::no_header);
oa & sendbuf;
if(world.rank()==0)
  MPI_Send(oa, 1, MPI_PACKED, 1, 0, MPI_COMM_WORLD);

我是否必须确保缓冲区足够大,或者 oarchive 会自动处理内存?如果是前者,保存 vector 的正确内存大小是多少?我想它应该不仅包含 vec.data(),还包含 vec.size()

最后,oa 似乎不是传递给 MPI_Send 的正确变量。那么我应该在创建存档后传递给 MPI_Send 什么?

我问是因为我们服务器上的 boost mpi 安装似乎对消息大小有限制。

最佳答案

在 boost.MPI 邮件列表的帮助下,我整理了以下示例:

using namespace std;
#include <iostream>
#include <string>   
#include <boost/mpi.hpp>
namespace mpi = boost::mpi;

int main(int argc, char **argv)
{
  mpi::environment env;
  mpi::communicator world;

#define MSG_LEN 100000
  vector <int> sendbuf(MSG_LEN, 1), recvbuf(MSG_LEN);

  MPI_Comm comm=world;
  if(world.rank()==0)
  {
    mpi::packed_oarchive oa(comm);
    oa << sendbuf;
    auto sendptr = const_cast<void*>(oa.address());
    // cast to int because MPI uses ints for sizes like it's still 1990
    int sendsize = static_cast<int>(oa.size());
    MPI_Send(&sendsize, 1, MPI_INT, 1, 0, comm);
    MPI_Send(sendptr, sendsize, MPI_PACKED, 1, 0, comm);
  }
  else if (world.rank()==1)
  {
    mpi::packed_iarchive ia(comm);
    int recvsize;
    MPI_Recv(&recvsize, 1, MPI_INT, 0, 0, comm, MPI_STATUS_IGNORE);
    ia.resize(recvsize);
    auto recvptr = ia.address();
    MPI_Recv(recvptr, recvsize, MPI_PACKED, 0, 0, comm, MPI_STATUS_IGNORE);
    ia >> recvbuf;
    cout<<"Data received: "<<recvbuf[0]<<","<<recvbuf[1]<<"...\n";
  }

  return 0;
}

关于c++ - 使用带有 C MPI 函数的 boost 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807669/

相关文章:

java - 在数据库中存储字符串[][]数组

java - 如何在 Java Berkeley DB 中存储 ArrayList 字段?

c++ - 链接 Visual Studio 2008 Express '/Mtd' 设置时 Boost 崩溃

c++ - CERN 根目录 : generate streamer for an extern "C" struct in a namespace

c++ - 如何使用boost单例

c++ - 学习使用 Boost 共享指针,控制台输出正确吗?

c++ - std::uniform_real_distribution 包含范围

c++ - 模板模板参数的通用引用

c++ - Vim 如何添加接受输入的键绑定(bind)

c++ - 编译时 std::ratio 的 std::ratio 功率?