Boost MPI send and recv
2016-02-27 11:56
274 查看
#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个进程。
相关文章推荐
- Spark Core Runtime分析: DAGScheduler, TaskScheduler, SchedulerBackend
- 【leetcode】【15】3Sum
- Android中JNI的使用之HelloWorld
- Android开发中出现的问题及解决(二)
- iOS NSString 没有去除首尾空格,换行符,空字符--导致UIlabel不能正确计算换行后的动态高度
- PHP常见面试题(简答部分二)
- Android即时通讯--仿QQ即时聊天:(三)核心代码抽取与登录逻辑
- 在Eclipse中使用JUnit4进行单元测试(初级篇)
- day03—html之页面布局
- java数据类型与转义字符的讲解
- 用socket编程实现的简单的聊天软件
- PDF阅读器系列之--MuPDF源码分析过程(一)
- property参数strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解
- 银河英雄传说 VIJOS 1443 并查集
- iOS 根据百分比获得渐进色 由绿到红
- for的叠代和穷举
- 一共81个,开源大数据处理工具汇总(上)
- javascript的 {} 语句块详解
- imageLoade加载图片策略(图片)
- hadoop面试题答案