|
From: <sng...@us...> - 2010-07-09 13:13:17
|
Revision: 89
http://dsim.svn.sourceforge.net/dsim/?rev=89&view=rev
Author: snguyenkim
Date: 2010-07-09 13:13:11 +0000 (Fri, 09 Jul 2010)
Log Message:
-----------
Example for a simple log server
Added Paths:
-----------
trunk/dsim/test/boost/mpi/log_server/
trunk/dsim/test/boost/mpi/log_server/Makefile
trunk/dsim/test/boost/mpi/log_server/client
trunk/dsim/test/boost/mpi/log_server/client.cpp
trunk/dsim/test/boost/mpi/log_server/ex.log
trunk/dsim/test/boost/mpi/log_server/log_server
trunk/dsim/test/boost/mpi/log_server/log_server.cpp
trunk/dsim/test/boost/mpi/log_server/log_server_client.sh
Added: trunk/dsim/test/boost/mpi/log_server/Makefile
===================================================================
--- trunk/dsim/test/boost/mpi/log_server/Makefile (rev 0)
+++ trunk/dsim/test/boost/mpi/log_server/Makefile 2010-07-09 13:13:11 UTC (rev 89)
@@ -0,0 +1,11 @@
+ALL: log_server client
+
+CC = mpicxx -lmpi_cxx -L/usr/lib/openmpi/lib/ -lboost_mpi
+
+log_server: log_server.cpp
+ $(CC) log_server.cpp -o log_server
+client: client.cpp
+ $(CC) client.cpp -o client
+
+clean:
+ rm -fv log_server client
Added: trunk/dsim/test/boost/mpi/log_server/client
===================================================================
(Binary files differ)
Property changes on: trunk/dsim/test/boost/mpi/log_server/client
___________________________________________________________________
Added: svn:executable
+ *
Added: svn:mime-type
+ application/octet-stream
Added: trunk/dsim/test/boost/mpi/log_server/client.cpp
===================================================================
--- trunk/dsim/test/boost/mpi/log_server/client.cpp (rev 0)
+++ trunk/dsim/test/boost/mpi/log_server/client.cpp 2010-07-09 13:13:11 UTC (rev 89)
@@ -0,0 +1,64 @@
+#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;
+
+//inactive waiting: wake up every 1s for verifying if there is a message
+void inactive_wait(mpi::communicator& world){
+ boost::optional<mpi::status> stat = boost::none;//stat is not initialised
+ while(1){
+ stat = world.iprobe(mpi::any_source, mpi::any_tag); //inactive waiting
+
+ //usleep(100);//we have the faster result but it takes more CPU
+ sleep(1); //better choice
+
+ if (stat){
+ stat = boost::none; //stat goes back to waiting state
+ return;//ah, someone calls me !
+ }
+
+ }
+ //world.probe(root,tag);//active waiting: take much more resources
+}
+
+int main(int argc, char ** argv ){
+ mpi::environment env(argc, argv);
+ mpi::communicator world;
+
+ int root = 0; //server's rank
+ int tag = 0; // the tag (or the port) used for communication
+ int dest;
+
+ int rank = world.rank(); //process's rank
+ string message; // message received from client
+ bool tmp;
+
+ if (rank > 0){
+ ostringstream oss;
+ oss << rank << " says alo";
+ message = oss.str();
+
+ while (1){
+ world.send(root, tag, rank); //try to get a place
+
+ //waits for a response, might takes a while as server takes care of someone else
+ inactive_wait(world);
+ world.recv(root, tag, tmp );
+
+ // Now I can send my message
+ cout << "sending :" << message << endl;
+ world.send(root,tag, message);
+
+ sleep (1); //not good to be too actif ...
+ }
+ }
+ return 0;
+};
+
Added: trunk/dsim/test/boost/mpi/log_server/ex.log
===================================================================
--- trunk/dsim/test/boost/mpi/log_server/ex.log (rev 0)
+++ trunk/dsim/test/boost/mpi/log_server/ex.log 2010-07-09 13:13:11 UTC (rev 89)
@@ -0,0 +1,18 @@
+1 says alo
+2 says alo
+1 says alo
+2 says alo
+1 says alo
+2 says alo
+1 says alo
+2 says alo
+1 says alo
+2 says alo
+1 says alo
+2 says alo
+1 says alo
+2 says alo
+1 says alo
+2 says alo
+1 says alo
+2 says alo
Added: trunk/dsim/test/boost/mpi/log_server/log_server
===================================================================
(Binary files differ)
Property changes on: trunk/dsim/test/boost/mpi/log_server/log_server
___________________________________________________________________
Added: svn:executable
+ *
Added: svn:mime-type
+ application/octet-stream
Added: trunk/dsim/test/boost/mpi/log_server/log_server.cpp
===================================================================
--- trunk/dsim/test/boost/mpi/log_server/log_server.cpp (rev 0)
+++ trunk/dsim/test/boost/mpi/log_server/log_server.cpp 2010-07-09 13:13:11 UTC (rev 89)
@@ -0,0 +1,81 @@
+/*
+* Object: Make a simple log server
+* Utilisation: Run log_server_client.sh
+* Problem: avoid competitive sending, i.d 2 clients send in a same time
+* Solution: Server takes care of client one by one
+* Note: Usage of inactive_wait funtion, which reduces CPU charge evidently
+*/
+
+#include <boost/mpi.hpp>
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <unistd.h>
+#include <time.h>
+#include <sys/types.h>
+#include<stdio.h>
+
+using namespace std;
+namespace mpi=boost::mpi;
+
+/* Write message to filename */
+void logToFile(string filename, string message){
+ ofstream out;
+ out.open(filename.c_str(), ios_base::app);
+ out << message << "\n" ;
+ out.close();
+}
+
+//inactive waiting: wake up every 1s for verifying if there is a message
+void inactive_wait(mpi::communicator& world){
+ boost::optional<mpi::status> stat = boost::none;//stat is not initialised
+ while(1){
+ stat = world.iprobe(mpi::any_source, mpi::any_tag); //inactive waiting
+
+ //usleep(100);//we have the faster result but it takes more CPU
+ sleep(1); //better choice
+
+ if (stat){
+ stat = boost::none; //stat goes back to waiting state
+ return;//ah, someone calls me !
+ }
+
+ }
+ //world.probe(root,tag);//active waiting: take much more resources
+}
+
+int main(int argc, char ** argv){
+ mpi::environment env(argc, argv);
+ mpi::communicator world;
+
+ int root = 0; //server's rank
+ int tag = 0; // the tag (or the port) used for communication
+ int dest;
+
+ int rank = world.rank(); //process's rank
+ string message; // message received from client
+ bool yes=true, tmp;
+
+ if (rank==0){
+ cout << "We have " << world.size() -1 << " clients " << endl;
+ cout << "=============================================================" << endl;
+
+ while (1){
+ //waits for a demand, might takes a while as client aren't always gossip
+ inactive_wait(world);
+ world.recv(mpi::any_source,tag,dest);
+
+ cout << "server received from client: " << dest << endl ;
+ world.send(dest,tag,yes); //ok, you can do it
+
+ // waiting for message from client
+ inactive_wait(world);
+ world.recv(dest,tag,message);
+
+ logToFile("ex.log", message);
+ cout << "server received: " << message << endl ;
+ }
+ }
+
+ return 0;
+};
Added: trunk/dsim/test/boost/mpi/log_server/log_server_client.sh
===================================================================
--- trunk/dsim/test/boost/mpi/log_server/log_server_client.sh (rev 0)
+++ trunk/dsim/test/boost/mpi/log_server/log_server_client.sh 2010-07-09 13:13:11 UTC (rev 89)
@@ -0,0 +1,5 @@
+# Execute log_server on localhost, 4 client on localhost (or fed1, fed2)
+#!/bin/sh
+
+/usr/lib/openmpi/bin/mpirun --host localhost -n 1 log_server :\
+ --host localhost -n 2 client
Property changes on: trunk/dsim/test/boost/mpi/log_server/log_server_client.sh
___________________________________________________________________
Added: svn:executable
+ *
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|