]> git.treefish.org Git - phys/latlib.git/blobdiff - writeout.cpp
Implemented new writeout file naming scheme using hashed range string.
[phys/latlib.git] / writeout.cpp
index 7834d6b743173b70e239dd4849a1c69171dc7847..a7563947f2e222b6f71409c213e78f60e632afaf 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) {
-   of[subname] = new ofstream;
+  of[subname] = new ofstream;
 
   if ( fulldir != "" ) {
 
   if ( fulldir != "" ) {
-    if(rank>0) of[subname]->open( (fulldir + "/rank" + cRank + "-" + subname + ".tmp").c_str() );
-    else of[subname]->open( (fulldir + "/" + signature + "-" + subname + ".dat").c_str() );
+    if(rank>0) of[subname]->open( (fulldir + "/rank" + cRank + "_" + subname + ".tmp").c_str() );
+    else of[subname]->open( (fulldir + "/" + signature + "_" + subname + ".dat").c_str() );
 
     if ( !of[subname]->is_open() ) {
       logf << "WRITEOUT: Could not open output-file!" << endl << flush;
 
     if ( !of[subname]->is_open() ) {
       logf << "WRITEOUT: Could not open output-file!" << endl << flush;
@@ -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() ) {
@@ -85,7 +119,7 @@ writeout::~writeout()
 {
   if(fulldir != "") {
     for (map<string,ofstream*>::iterator ofit = of.begin(); ofit != of.end(); ++ofit) {
 {
   if(fulldir != "") {
     for (map<string,ofstream*>::iterator ofit = of.begin(); ofit != of.end(); ++ofit) {
-      if( cRank[0] == '0' ) {    
+      if( cRank[0] == '0' ) {
        int jobsdone=0;
        while(jobsdone<numprocs-1) {
          string nextfile;
        int jobsdone=0;
        while(jobsdone<numprocs-1) {
          string nextfile;
@@ -108,14 +142,15 @@ writeout::~writeout()
        }
        *ofit->second << "#end" << endl << flush;
        ofit->second->close();
        }
        *ofit->second << "#end" << endl << flush;
        ofit->second->close();
-       rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() );
       }
       else {
        ofit->second->close();
       }
       else {
        ofit->second->close();
-       rename((fulldir + "/rank" + cRank + ".tmp").c_str(),
-              (fulldir + "/rank" + cRank + ".part").c_str());
+       rename((fulldir + "/rank" + cRank + "_" + ofit->first + ".tmp").c_str(),
+              (fulldir + "/rank" + cRank + "_" + ofit->first + ".part").c_str());
       }
     }
       }
     }
+    if( cRank[0] == '0' )
+      rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() );
   }
   logf << "[ " << timestring() << " ] Log ends here." << endl;
   logf.close();
   }
   logf << "[ " << timestring() << " ] Log ends here." << endl;
   logf.close();
@@ -129,16 +164,21 @@ string writeout::getdatfile(string subname)
 
   if((dp  = opendir(fulldir.c_str())) == NULL) {
     logf << "Error(" << errno << ") opening " << fulldir << endl;
 
   if((dp  = opendir(fulldir.c_str())) == NULL) {
     logf << "Error(" << errno << ") opening " << fulldir << endl;
+    closedir(dp);
     return "";
   }
   
   while ((dirp = readdir(dp)) != NULL)
     {
       myfile = string(dirp->d_name);
     return "";
   }
   
   while ((dirp = readdir(dp)) != NULL)
     {
       myfile = string(dirp->d_name);
-      if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part") {
+
+      if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part" &&
+        subname == myfile.substr( myfile.find("_")+1, myfile.rfind(".")-myfile.find("_")-1 ) ) {
+       closedir(dp);
        return myfile;
       }
     }
 
        return myfile;
       }
     }
 
+  closedir(dp);
   return "";
 }
   return "";
 }