Boost MPI send and recv

来源:互联网 发布:姬存希 知乎 编辑:程序博客网 时间:2024/06/06 07:27
#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 0
原创粉丝点击