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个进程。
- Boost MPI send and recv
- boost mpi send recv-vector<class>
- send and recv
- linux send and recv详解
- Send / Recv Usage(1)
- Send / Recv Usage(2)
- send,recv,sendto,recvfrom
- send recv函数
- send,recv,sendto,recvfrom
- 转:send recv函数
- Socket send recv
- send和recv函数
- send,recv,sendto,recvfrom
- Send和Recv函数
- send和recv函数
- Linux socket recv & send
- send,recv,sendto,recvfrom
- 关于send recv
- DE26 Continuation: Repeated Real Eigenvalues
- Python图像处理库PIL的ImageEnhance模块介绍
- JAVA 一个简单的图形界面计算器
- 3910: 火车 LCA+并查集
- Activity的生命周期(三)——结合Activity的生命周期理解子类继承父类预加载数据
- Boost MPI send and recv
- 获取UIWebView的高度
- CS0234The type or namespace name 'Mvc' does not exist in the namespace 'Microsoft.AspNet' (are you
- 详解配置java环境变量那点事儿
- 第52课:Hadoop链式MapReduce编程实战
- 心路
- 【年度总结】——踏雪留痕
- 使用批处理清理vs工程项目
- 3207: 花神的嘲讽计划Ⅰ 主席树+hash