X-Git-Url: http://git.treefish.org/~alex/phys/latlib.git/blobdiff_plain/b3e5f37ca561e20879f984d53db68adae89ef413..3db2602cfe30c61445e89cc334541ebb4ff76ed8:/writeout.cpp diff --git a/writeout.cpp b/writeout.cpp index 3849ac9..7834d6b 100644 --- a/writeout.cpp +++ b/writeout.cpp @@ -21,9 +21,33 @@ string writeout::tstamp(const long& timestamp) return sstr.str(); } -writeout::writeout(const string& wdir, const string& signature, - const int& rank, const int& procs, const long& timestamp) +void writeout::newsub(string subname) { + of[subname] = new ofstream; + + 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 ( !of[subname]->is_open() ) { + logf << "WRITEOUT: Could not open output-file!" << endl << flush; + exit(1); + } + + buf[subname] = of[subname]->rdbuf(); + } + else + buf[subname] = cout.rdbuf(); + + out[subname] = new ostream(buf[subname]); +} + +writeout::writeout(const string& wdir, const string& _signature, + const int& _rank, const int& procs, const long& timestamp) { + fulldir = ""; + signature = _signature; + rank = _rank; + if(wdir != ""){ numprocs = procs; sprintf(cRank, "%d", rank); @@ -31,28 +55,20 @@ writeout::writeout(const string& wdir, const string& 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() ); - logf.open( (fulldir + "/rank" + cRank + ".log").c_str() ); - if ( (!of.is_open()) || (!logf.is_open()) ) { - cerr << "WRITEOUT: Could not open output- and/or log-file!" << endl << flush; + if ( !logf.is_open() ) { + cerr << "WRITEOUT: Could not open log-file!" << endl << flush; exit(1); } logf << "[ " << timestring() << " ] Log starts here." << endl; - buf = of.rdbuf(); logbuf = logf.rdbuf(); - - cout << buf << endl; } else{ - buf = cout.rdbuf(); logbuf = cerr.rdbuf(); } - out = new ostream(buf); log = new ostream(logbuf); } @@ -67,47 +83,45 @@ string writeout::timestring() writeout::~writeout() { - if(fulldir != "") - { - if( cRank[0] == '0' ) - { - int jobsdone=0; - while(jobsdone::iterator ofit = of.begin(); ofit != of.end(); ++ofit) { + if( cRank[0] == '0' ) { + int jobsdone=0; + while(jobsdonefirst)) == "" ) + sleep(1); + else { + logf << "collecting " << nextfile << endl; + + ifstream myfile( (fulldir + "/" + nextfile).c_str() ); + while(true) { + string line; + getline(myfile, line); + if( !myfile.good() ) break; + *ofit->second << line << endl << flush; } - of << "#end" << endl << flush; - of.close(); - rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() ); - } - else - { - of.close(); - rename((fulldir + "/rank" + cRank + ".tmp").c_str(), - (fulldir + "/rank" + cRank + ".part").c_str()); + myfile.close(); + remove( (fulldir + "/" + nextfile).c_str() ); + jobsdone++; + } } + *ofit->second << "#end" << endl << flush; + ofit->second->close(); + rename( fulldir.c_str(), fulldir.substr(0, fulldir.length()-4).c_str() ); + } + else { + ofit->second->close(); + rename((fulldir + "/rank" + cRank + ".tmp").c_str(), + (fulldir + "/rank" + cRank + ".part").c_str()); + } } + } logf << "[ " << timestring() << " ] Log ends here." << endl; logf.close(); } -string writeout::getdatfile() +string writeout::getdatfile(string subname) { string myfile; DIR *dp; @@ -121,8 +135,9 @@ string writeout::getdatfile() 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") { return myfile; + } } return "";