Boost MPI send and recv

#include "stdafx.h"
#include <boost/mpi.hpp>
#include <iostream>
#include <vector>
#include "VecAndMatrix.h"
namespace mpi = boost::mpi;

class cPoint
{
private:
 friend class boost::serialization::access;

template<class Archive>
 void serialize(Archive &ar, const unsigned int version)
 {
   ar & x;
   ar & y;
   ar & x;
   ar & m;
 }

public:
 double x,y,z;
 double m;
 cPoint(){;};
 cPoint(double xt,double yt,double zt,double mt):x(xt),y(yt),z(zt),m(mt){;};
};

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

  int size = world.size();
  int num = int(100*1.0/size);

  if(world.rank() == 0) {
    std::vector<cPoint> point;
    for(int i=0;i<10;i++){point.push_back(cPoint(i, i, i,5));};

    std::vector<cPoint> *vs = &point;
    for(int i=1;i<world.size();i++){world.send(i, 100, vs,1);};


  }
  else {
    std::vector<cPoint> point;
    std::vector<cPoint> *vr = &point;
    world.recv(0, 100, vr,1);

    for(int i=0;i<point.size(); i++) {
        std::cout<<world.rank()<<'\t'<<point[i].x<<'\t'<<point[i].x<<'\t'<<point[i].x<<'\t'<<point[i].m<<std::endl;}
  }

  return 0;
}

以上程序将根进程的一个vector发送给其他所有进程。vector中的元素是cPoint类。以上程序利用了指针,也可以直接用vector,只需将发送接收做如下修改,功能完全相同:

for(int i=1;i<world.size();i++){world.send(i, 100, point);};
world.recv(0, 100, point);

注意此格式只有三个参数。(boost 根据参数进行函数重载)。
template void send(int dest, int tag, const T & value) const;
(Send data to another process.(发送数据到另外一个进程)
This routine executes a potentially blocking send with tag tag to the process with rank dest. It can be received by the destination process with a matching recv call.

template
void send(int dest, int tag, const T * values, int n) const;
Send an array of values to another process.(发送一个数组到另外一个进程,必须要用一个匹配的数组接受调用接收)。

参数:
dest
The process rank of the remote process to which the data will be sent.
n
The number of values stored in the array. The destination process must call receive with at least this many elements to correctly receive the message.
tag
The tag that will be associated with this message. Tags may be any integer between zero and an implementation-defined upper limit. This limit is accessible via environment::max_tag().
values
The array of values that will be transmitted to the receiver. The type T of these values must be mapped to an MPI data type.

要用此函数,数组元素必须是 MPI数据类型,所以应该不能用此函数发送上面的 VECTOR, 但上面的程序在n=1时候能够正确执行,n=2时候不行。

如果有一个vector,想把其中元素平均分给其他进程:

#include "stdafx.h"
#include <boost/mpi.hpp>
#include <iostream>
#include <vector>
#include "VecAndMatrix.h"
namespace mpi = boost::mpi;



int main(int argc, char *argv[])   {
  mpi::environment env(argc, argv);
  mpi::communicator world;
  int tag = 99;
  std::vector<double> v;

  for(int i=0;i<10;i++)v.push_back(i);

 if (world.rank() == 0) {
    int load = v.size()/world.size();
    int start = load+v.size()%world.size();

for (int i = 1; i < world.size(); ++i){
     std::vector<double> to_send (v.begin()+start,v.begin()+start+load);
     world.send(i,tag,to_send);
     start+=load;}
 }
 else {
 std::vector<double> v;
 world.recv(0,tag,v);

 for(int i=0;i<v.size();i++){
     std::cout<<world.rank()<<'\t'<<i<<'\t'<<v[i]<<std::endl;}
}
}

以上程序将10个元素分给n个进程。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值