boost mpi 简单例子测试

来源:互联网 发布:重庆java培训哪个靠谱 编辑:程序博客网 时间:2024/06/05 22:31
// testBoost1.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <boost\mpi.hpp>#include <iostream>#include <string.h>#include <stdio.h>#include <time.h>#include <map>#include <vector>using namespace std;/*Example_1*/int example1(int argc, char * argv[]){boost::mpi::environment env(argc, argv);boost::mpi::communicator world;//std::cout<<argc<<std::endl;//std::cout<<argv[0]<<std::endl;std::cout<<"hello world! from process "<<world.rank()<<std::endl;return 0;}/*Example_2*/int example2(int argc, char * argv[]){int numprocs,myrank;    MPI_Status status;    char msg[20];    MPI_Init(&argc,&argv);    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);    MPI_Comm_rank(MPI_COMM_WORLD,&myrank);    if(myrank == 0){        strcpy_s(msg,"Hello World");        MPI_Send(msg,strlen(msg) + 1,MPI_CHAR,1,99,MPI_COMM_WORLD);    }else if(myrank ==1){        MPI_Recv(msg,20,MPI_CHAR,0,99,MPI_COMM_WORLD,&status);        printf("Receive message = %s\n",msg);    }    MPI_Finalize();return 0;}/*Example_3*/int example3(int argc, char * argv[]){int myid, np;int namelen;char proc_name[MPI_MAX_PROCESSOR_NAME];MPI_Init(&argc, &argv);MPI_Comm_rank(MPI_COMM_WORLD, &myid);MPI_Comm_size(MPI_COMM_WORLD, &np);MPI_Get_processor_name(proc_name, &namelen);std::fprintf(stderr, "Hello,I am proc.%d of %d on %s\n",myid, np, proc_name);MPI_Finalize();return 0;}/*Example_4*/int example4(int argc, char * argv[]){boost::mpi::environment env(argc, argv);boost::mpi::communicator world;if (world.rank() == 0) {world.send(1, 9, 32);world.send(2, 9, 33);} else { int data;world.recv(0, 9, data);std::cout << "In process " << world.rank( ) << "with data " << data<< std::endl;} return 0;}/*Example_5*/int example5(int argc, char* argv[]){boost::mpi::environment env(argc, argv);boost::mpi::communicator world;std::string value;if (world.rank() == 0) {value = "Hello, World!";}broadcast(world, value, 0);std::cout << "Process #" << world.rank() << " says " << value<< std::endl;return 0;}/*Example_6*/int example6(int argc, char* argv[]){  boost::mpi::environment env(argc, argv);  boost::mpi::communicator world;  std::srand(time(0) + world.rank());  int my_number = std::rand();  if (world.rank() == 0)   {    std::vector<int> all_numbers;    gather(world, my_number, all_numbers, 0);    for (int proc = 0; proc < world.size(); ++proc)      std::cout << "Process #" << proc << " thought of "                << all_numbers[proc] << std::endl;  }  else   {    gather(world, my_number, 0);  }  return 0;}/*Example_7*/int example7(int argc, char* argv[]){boost::mpi::environment env(argc, argv);boost::mpi::communicator world;if (world.rank() == 0) {boost::mpi::request reqs[2];std::string msg, out_msg = "Hello";reqs[0] = world.isend(1, 0, out_msg);reqs[1] = world.irecv(1, 1, msg);boost::mpi::wait_all(reqs, reqs + 2);std::cout << msg << "!" << std::endl;} else {boost::mpi::request reqs[2];std::string msg, out_msg = "world";reqs[0] = world.isend(0, 1, out_msg);reqs[1] = world.irecv(0, 0, msg);boost::mpi::wait_all(reqs, reqs + 2);std::cout << msg << ", ";}return 0;}/*Example_8*/int example8(boost::mpi::communicator world){////boost::mpi::communicator world;//std::string *vl;//std::string recvVl;//*vl = "Hello, World! From Process ";////boost::mpi::broadcast(world, value, 0);////boost::mpi::gather(world, value, );//std::vector<std::string> vect;//boost::mpi::gather(world, vl, vect, world.rank());//////vect.push_back(*vl);////std::cout<<vect.size()<<std::endl;//std::vector<std::string>::iterator iter;//for (iter = vect.begin(); iter != vect.end(); iter ++)//{//std::cout<<*iter<<world.rank()<<std::endl;//}//return 0;//std::srand(time(0) + world.rank());int my_number = (world.rank())*100;//boost::mpi::broadcast(world, my_number, world.rank());int r = world.rank();std::vector<int> all_numbers;gather(world, my_number, all_numbers, 0);for (int proc = 0; proc < world.size(); ++proc)std::cout << "Process #" << proc << " thought of " << all_numbers[proc] << std::endl;gather(world, my_number, all_numbers, 1);for (int proc = 0; proc < world.size(); ++proc)std::cout << "Process #" << proc << " thought of " << all_numbers[proc] << std::endl;return 0;}/*Example_9*/int example9(int argc, char* argv[]){boost::mpi::environment env(argc, argv);boost::mpi::communicator world;std::string value;if (world.rank() == 0) {value = "Hello, World!";}broadcast(world, value, 0);std::cout << "Process #" << world.rank() << " says " << value << std::endl;return 0;} /*Example_10*/int example10(int argc, char* argv[]){boost::mpi::environment env(argc, argv);boost::mpi::communicator world;std::string value;value = "Hello, World!";broadcast(world, value, 0);if (world.rank() == 0) {std::cout << "Process #" << world.rank() << " send " << value << std::endl;} if(world.rank() != 0){std::cout << "Process #" << world.rank() << " receive " << value << std::endl;}return 0;}/*Example_11*/int example11(int argc, char* argv[]){boost::mpi::environment env(argc, argv);boost::mpi::communicator world;int minN,maxN;minN =world.rank();boost::mpi::all_reduce(world, minN, maxN, boost::mpi::maximum<int>());std::cout << "MaxN = " << maxN << std::endl;return 0;}class comtest{private:boost::mpi::communicator m_world;//comtest *_instance;public:comtest(){//boost::mpi::environment env(argc, argv);//boost::mpi::communicator m_world;//_instance = 0;}// comtest *initial()// {// boost::mpi::communicator m_world;// // if (_instance == 0)// {// _instance = new comtest;// }// return _instance;// }~comtest(){}int example12(int argc, char* argv[]){boost::mpi::environment env(argc, argv);boost::mpi::communicator world;int minN,maxN;minN =world.rank();boost::mpi::all_reduce(world, minN, maxN, boost::mpi::maximum<int>());std::cout << "MaxN = " << maxN << std::endl;return 0;}int example13(boost::mpi::communicator world){int minN,maxN;minN =world.rank();boost::mpi::all_reduce(world, minN, maxN, boost::mpi::maximum<int>());std::cout << "MaxN = " << maxN << std::endl;return 0;}int example14(){int minN,maxN;minN =m_world.rank();boost::mpi::all_reduce(m_world, minN, maxN, boost::mpi::maximum<int>());std::cout << "MaxN = " << maxN << std::endl;return 0;}};int example15(boost::mpi::communicator world){int minN,maxN;minN =world.rank();boost::mpi::all_reduce(world, minN, maxN, boost::mpi::maximum<int>());std::cout << "MaxN = " << maxN << std::endl;return 0;}int example16(boost::mpi::communicator world) {boost::mpi::request reqs[4];//std::string msg1, out_msg1 = "Hello";//std::string msg2, out_msg2 = "world";std::vector<string> vect1, vect2, vect3, vect4;vect1.push_back("a1,");vect1.push_back("a2,");vect3.push_back("b1,");vect3.push_back("b2,");  if (world.rank() == 0) {reqs[0] = world.isend(1, 0, vect1);    reqs[1] = world.irecv(1, 1, vect2);  } else {    reqs[2] = world.isend(0, 1, vect3);    reqs[3] = world.irecv(0, 0, vect4);  }  boost::mpi::wait_all(reqs, reqs + 4);  std::vector<string>::iterator iter;  for (iter = vect2.begin(); iter != vect2.end(); iter ++)  {  std::cout << *iter << endl;  }  std::vector<string>::iterator iter2;  for (iter2 = vect4.begin(); iter2 != vect4.end(); iter2 ++)  {  std::cout << *iter2 << endl;  }  return 0;}int example17(boost::mpi::communicator world){int minN;minN =world.rank();std::vector<int> vect;boost::mpi::all_gather(world, minN, vect);std::cout << "vect.size() = " << vect.size() << std::endl;std::vector<int>::iterator iter2;for (iter2 = vect.begin(); iter2 != vect.end(); iter2 ++){std::cout<<world.rank()<<"has:" << *iter2 << endl;}return 0;}int example18(boost::mpi::communicator world) {std::vector<boost::mpi::request> reqs;std::vector<int> vect1, vect2, vect3, vect4, vect5, vect6;vect1.push_back(1);vect1.push_back(2);vect3.push_back(3);vect3.push_back(4);vect5.push_back(5);vect5.push_back(6);//i/*if (world.rank() == 0){reqs.push_back(world.isend(1, 0, vect1));reqs.push_back(world.irecv(2, 2, vect2));reqs.push_back(world.isend(2, 1, vect1));reqs.push_back(world.irecv(1, 1, vect4));}else if (world.rank() == 1){reqs.push_back(world.isend(2, 1, vect3));reqs.push_back(world.irecv(0, 0, vect4));reqs.push_back(world.isend(0, 1, vect3));reqs.push_back(world.irecv(2, 1, vect6));}else if (world.rank() == 2){reqs.push_back(world.isend(0, 2, vect5));reqs.push_back(world.irecv(1, 1, vect6));reqs.push_back(world.isend(1, 1, vect5));reqs.push_back(world.irecv(0, 1, vect4));}//输出接收到的信息boost::mpi::wait_all(reqs.begin(), reqs.end());std::cout << "wait_all"<< endl;*/if (world.rank() == 0) {reqs.push_back(world.isend(2, 0, vect1));reqs.push_back(world.isend(1, 0, vect1));reqs.push_back(world.irecv(2, 2, vect2));} else if(world.rank() == 1){reqs.push_back(world.isend(2, 1, vect3));reqs.push_back(world.irecv(0, 0, vect4));}else if(world.rank() == 2){reqs.push_back(world.isend(0, 2, vect5));reqs.push_back(world.irecv(1, 1, vect6));}boost::mpi::wait_all(reqs.begin(), reqs.end());std::vector<int>::iterator iter;for (iter = vect2.begin(); iter != vect2.end(); iter ++){std::cout << *iter <<"  ";}std::cout << endl<< endl;std::vector<int>::iterator iter2;for (iter2 = vect4.begin(); iter2 != vect4.end(); iter2 ++){std::cout << *iter2 << "  ";}std::cout << endl<< endl;std::vector<int>::iterator iter3;for (iter3 = vect6.begin(); iter3 != vect6.end(); iter3 ++){std::cout << *iter3 << "  ";}std::cout << endl<< endl;return 0;}int main(int argc, char* argv[]){boost::mpi::environment env(argc, argv);boost::mpi::communicator m_world;return example18(m_world);}

原创粉丝点击