有人能给我一个将升序序列化与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
会自动处理内存?如果是前者,持有向量的内存大小是多少?我想它不仅应该包含vec.data()
,还应该包含vec.size()
。
最后,oa
似乎不是传递给MPI_Send
的正确变量。那么在创建档案后我应该将什么传递给MPI_Send
?
我问,因为我们服务器上的boost mpi安装似乎对邮件大小有限制。
答案 0 :(得分:2)
在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;
}