]> git.treefish.org Git - phys/latlib.git/blobdiff - writeout.cpp
...
[phys/latlib.git] / writeout.cpp
index af67fb5fc913dfa9032aea8df877578e646007b4..41dcdf57e9d4b6d5eb3087a4ba1abd1699b16ad4 100644 (file)
 #include <errno.h>
 #include <unistd.h>
 
 #include <errno.h>
 #include <unistd.h>
 
+#ifndef MPI_DISABLED
+#include <mpi.h>
+#endif
+
 using namespace std;
 
 using namespace std;
 
-string writeout::tstamp(const long& timestamp)
+string writeout::longToStr (long arg)
 {
 {
-  stringstream sstr;
-  if(!timestamp) sstr << time (NULL);
-  else sstr << timestamp;
-  return sstr.str();
+  stringstream ss;
+  ss << arg;
+  return ss.str();
 }
 
 void writeout::newsub(string subname) {
 }
 
 void writeout::newsub(string subname) {
@@ -42,8 +45,11 @@ void writeout::newsub(string subname) {
 }
 
 writeout::writeout(const string& wdir, const string& _signature, 
 }
 
 writeout::writeout(const string& wdir, const string& _signature, 
-                  const int& _rank, const int& procs, const long& timestamp)
+                  const int& _rank, const int& procs)
 {
 {
+  long timestamp;
+  int iseq=0;
+
   fulldir = "";
   signature = _signature;
   rank = _rank;
   fulldir = "";
   signature = _signature;
   rank = _rank;
@@ -51,10 +57,38 @@ writeout::writeout(const string& wdir, const string& _signature,
   if(wdir != ""){
     numprocs = procs;
     sprintf(cRank, "%d", rank);
   if(wdir != ""){
     numprocs = procs;
     sprintf(cRank, "%d", rank);
-    fulldir = wdir + "/" + tstamp(timestamp) + "_" + signature + ".tmp";
 
 
-    mkdir(fulldir.c_str(), 0775);
+    if (rank == 0) {
+      timestamp = time(NULL);
+
+      while (true) {
+       fulldir = wdir + "/" + longToStr(timestamp) + "." + longToStr(iseq) + "_" + signature + ".tmp";
+       if ( mkdir(fulldir.c_str(), 0775) == 0 )
+         break;
+       else if ( errno != EEXIST ) {
+         cerr << "WRITEOUT: Could not create outdir!" << endl << flush;
+         break;
+       }
+       iseq++;
+      }
 
 
+#ifndef MPI_DISABLED
+      for(int idest=1; idest<numprocs; idest++) {
+       MPI_Send(&timestamp, 1, MPI_LONG, idest, 123, MPI_COMM_WORLD);
+       MPI_Send(&iseq, 1, MPI_LONG, idest, 124, MPI_COMM_WORLD);
+      }
+#endif
+
+    }
+
+#ifndef MPI_DISABLED
+    else if(rank>0) {
+      MPI_Recv(&timestamp, 1, MPI_LONG, 0, 123, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
+      MPI_Recv(&iseq, 1, MPI_LONG, 0, 124, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
+      fulldir = wdir + "/" + longToStr(timestamp) + "." + longToStr(iseq) + "_" + signature + ".tmp";
+    }
+#endif  
+    
     logf.open( (fulldir + "/rank" + cRank + ".log").c_str() );
 
     if ( !logf.is_open() ) {
     logf.open( (fulldir + "/rank" + cRank + ".log").c_str() );
 
     if ( !logf.is_open() ) {