]> git.treefish.org Git - phys/latlib.git/commitdiff
added writeout class
authorAlex Schmidt <alex@treefish.org>
Mon, 25 Jun 2012 12:46:23 +0000 (22:46 +1000)
committerAlex Schmidt <alex@treefish.org>
Mon, 25 Jun 2012 12:46:23 +0000 (22:46 +1000)
.gitignore
writeout.cpp [new file with mode: 0644]
writeout.h [new file with mode: 0644]

index 9673182b24ad4c28b4f89f9935414d41838db7d7..382dfaaf3f90a130bc22a092ce1f554da610e03c 100644 (file)
@@ -3,3 +3,4 @@ CMakeFiles
 cmake_install.cmake
 Makefile
 *.a
 cmake_install.cmake
 Makefile
 *.a
+*~
diff --git a/writeout.cpp b/writeout.cpp
new file mode 100644 (file)
index 0000000..4fb68e1
--- /dev/null
@@ -0,0 +1,101 @@
+#include "writeout.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <iostream>
+#include <time.h>
+#include <sstream>
+#include <dirent.h>
+#include <errno.h>
+
+using namespace std;
+
+string writeout::tstamp()
+{
+    stringstream sstr;
+    sstr << time (NULL);
+    return sstr.str();
+}
+
+writeout::writeout(const string& wdir, const string& signature, 
+                  const int& rank, const int& procs)
+{
+  if(wdir != ""){
+    numprocs = procs;
+    sprintf(cRank, "%d", rank);
+    fulldir = wdir + "/" + tstamp() + "_" + signature;
+    mkdir( fulldir.c_str(),  0775);
+
+    if(rank>0) of.open( (fulldir + "/rank" + cRank + ".tmp").c_str() );
+    else of.open( (fulldir + "/" + signature + ".dat").c_str() );
+
+    buf = of.rdbuf();
+  }
+  else{
+    buf = cout.rdbuf();
+  }
+  out = new ostream(buf);
+}
+
+writeout::~writeout()
+{
+  if(fulldir != "")
+    {
+      if( cRank[0] == '0' )
+       {
+         int jobsdone=0;
+         while(jobsdone<numprocs-1)
+           {
+             string nextfile;
+             if( (nextfile=getdatfile()) == "" ) sleep(1);
+             else
+               {
+                 cerr << "collecting " << nextfile << endl;
+
+                 ifstream myfile( (fulldir + "/" + nextfile).c_str() );
+                 while(myfile.good()){
+                   string line;
+                   getline(myfile, line);
+                   of << line << flush;
+                 }
+                 myfile.close();
+                 of << endl << flush;
+
+                 remove( (fulldir + "/" + nextfile).c_str() );
+                 jobsdone++;
+               }
+           }
+         of << "#end" << endl << flush;
+         of.close();
+       }
+      else
+       {
+         of.close();
+         rename((fulldir + "/rank" + cRank + ".tmp").c_str(),
+                (fulldir + "/rank" + cRank + ".part").c_str());
+       }
+    }
+}
+
+string writeout::getdatfile()
+{
+  string myfile;
+  DIR *dp;
+  struct dirent *dirp;
+
+  if((dp  = opendir(fulldir.c_str())) == NULL) {
+    cerr << "Error(" << errno << ") opening " << fulldir << endl;
+    return "";
+  }
+  
+  while ((dirp = readdir(dp)) != NULL)
+    {
+      myfile = string(dirp->d_name);
+      if(myfile.length() > 3 && myfile.substr(myfile.length()-4) == "part")
+       return myfile;
+    }
+
+  return "";
+}
diff --git a/writeout.h b/writeout.h
new file mode 100644 (file)
index 0000000..48057d4
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef WRITEOUT_H
+#define WRITEOUT_H
+
+#include <ostream>
+#include <fstream>
+
+using namespace std;
+
+class writeout
+{
+ public:
+  writeout(const string& wdir, const string& signature, 
+          const int& rank, const int& procs);
+  ostream *out;
+  ~writeout();
+
+ private:
+  ofstream of;
+  streambuf *buf;
+  string fulldir;
+  char cRank[20];
+  string tstamp();
+  int numprocs;
+  string getdatfile();
+};
+
+#endif