|
From: <sng...@us...> - 2010-05-03 09:45:32
|
Revision: 80
http://dsim.svn.sourceforge.net/dsim/?rev=80&view=rev
Author: snguyenkim
Date: 2010-05-03 09:45:23 +0000 (Mon, 03 May 2010)
Log Message:
-----------
Master & Slave example, see master.cpp for more details
Added Paths:
-----------
trunk/dsim/test/boost/mpi/master.cpp
trunk/dsim/test/boost/mpi/slave.cpp
Added: trunk/dsim/test/boost/mpi/master.cpp
===================================================================
--- trunk/dsim/test/boost/mpi/master.cpp (rev 0)
+++ trunk/dsim/test/boost/mpi/master.cpp 2010-05-03 09:45:23 UTC (rev 80)
@@ -0,0 +1,54 @@
+/*
+ * Object: Make a simple master-slave program
+ * Utilisation: User tape a number, master send it to a slave (he is chosen based on the number of slave ), that slave return the square of that number to master.
+ *
+ */
+
+#include <boost/mpi.hpp>
+#include <iostream>
+#include <string>
+#include <unistd.h>
+#include <time.h>
+#include <sys/types.h>
+#include<stdio.h>
+
+using namespace std;
+namespace mpi=boost::mpi;
+
+int main(int argc, char ** argv){
+ mpi::environment env(argc, argv);
+ mpi::communicator world;
+
+ int root = 0; //master's rank
+ int tag = 0; // the tag (or the port) used for communication
+ int num ; //number taken in by user
+ int res; // result sent by slave
+ int dest; //index of slave to which command is sent
+
+ int rank = world.rank(); //process's rank
+ int nbslave = world.size() -1 ; //number of slave
+
+ if (rank==0){
+ cout << "We have " << nbslave << " slaves who work" << endl;
+ //cout << rank << "; pid = " << getpid() << endl;
+ cout << "Type -1 for quitting the program\n";
+
+ while(1){
+
+ cout << "A number, plz..\n" ;
+ cin >> num ;
+ if (num == -1){
+ cerr << "Bye bye\n";
+ //mpi::environment::finalized();//Abort all the process
+ mpi::environment::abort(0);
+ } else{
+ dest = num % nbslave + 1;
+ world.send(dest, tag, num);
+ };
+ world.recv(dest , tag , res);
+ cout << "Resultat is " << res << endl;
+ };
+ }
+
+ return 0;
+};
Added: trunk/dsim/test/boost/mpi/slave.cpp
===================================================================
--- trunk/dsim/test/boost/mpi/slave.cpp (rev 0)
+++ trunk/dsim/test/boost/mpi/slave.cpp 2010-05-03 09:45:23 UTC (rev 80)
@@ -0,0 +1,56 @@
+#include <boost/mpi.hpp>
+#include <iostream>
+#include <string>
+#include <unistd.h>
+#include <time.h>
+#include <sys/types.h>
+#include<stdio.h>
+
+
+using namespace std;
+namespace mpi=boost::mpi;
+
+int main(int argc, char ** argv ){
+ mpi::environment env(argc, argv);
+ mpi::communicator world;
+
+ int root = 0, tag = 0, num, res;
+ int rank = world.rank();//processus number
+
+
+ //for veriying that master & slave use the same communicator
+ //if (rank==1)
+ //cout << "We have " << world.size() -1 << " slaves who work" << endl;
+
+ boost::optional<mpi::status> stat = boost::none;//stat is not initialised
+
+ if (rank > 0){
+ //cout << rank << "; pid = " << getpid() << endl;
+ int r; //number sent by master
+
+ while (1){
+
+ //inactive waiting: wake up every 1s for verifying if there is a message
+ while(1){
+ stat = world.iprobe(root, tag); //inactive waiting
+
+ //usleep(100);//we have the result faster but it takes more CPU
+ sleep(1); //better choice
+
+ if (stat)
+ break;//ah, I have message !
+ }
+ //world.probe(root,tag);//active waiting: take much more resources
+
+ world.recv(root,tag, r );
+
+ cout << "Slave " << rank << " has received :" << r << flush << endl;
+ res = r * r;
+ world.send(root, tag ,res );//return the result to master
+
+ stat = boost::none; //stat goes back to waiting state
+ }
+ }
+ return 0;
+};
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|