X-Git-Url: http://git.treefish.org/~alex/phys/latlib.git/blobdiff_plain/4d3aa1df919cc19d1bf2f66e5272d9ecda95d463..38ffdcb3843eec92d62b23d32a735107a49fb7d8:/writeout.cpp diff --git a/writeout.cpp b/writeout.cpp index 81f1e02..a756394 100644 --- a/writeout.cpp +++ b/writeout.cpp @@ -11,70 +11,98 @@ #include #include +#ifndef MPI_DISABLED +#include +#endif + using namespace std; -bool writeout::DirectoryExists( const char* pzPath ) +string writeout::longToStr (long arg) { - if ( pzPath == NULL) return false; + stringstream ss; + ss << arg; + return ss.str(); +} - DIR *pDir; - bool bExists = false; +void writeout::newsub(string subname) { + of[subname] = new ofstream; - pDir = opendir (pzPath); + 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 (pDir != NULL) - { - bExists = true; - (void) closedir (pDir); + if ( !of[subname]->is_open() ) { + logf << "WRITEOUT: Could not open output-file!" << endl << flush; + exit(1); } - return bExists; -} + buf[subname] = of[subname]->rdbuf(); + } + else + buf[subname] = cout.rdbuf(); -string writeout::tstamp(const long& timestamp) -{ - stringstream sstr; - if(!timestamp) sstr << time (NULL); - else sstr << timestamp; - return sstr.str(); + out[subname] = new ostream(buf[subname]); } -writeout::writeout(const string& wdir, const string& signature, - const int& rank, const int& procs, const long& timestamp) +writeout::writeout(const string& wdir, const string& _signature, + const int& _rank, const int& procs) { + long timestamp; + int iseq=0; + + fulldir = ""; + signature = _signature; + rank = _rank; + if(wdir != ""){ numprocs = procs; sprintf(cRank, "%d", rank); - fulldir = wdir + "/" + tstamp(timestamp) + "_" + signature + ".tmp"; - for(int itry=0; itry<100 && (!DirectoryExists(fulldir.c_str())); itry++) { - if (rank==0) - mkdir( fulldir.c_str(), 0775); - sleep(1); - } + if (rank == 0) { + timestamp = time(NULL); - if(rank>0) of.open( (fulldir + "/rank" + cRank + ".tmp").c_str() ); - else of.open( (fulldir + "/" + signature + ".dat").c_str() ); + 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; idest0) { + MPI_Recv(×tamp, 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 ( (!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); } @@ -89,47 +117,46 @@ 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(); + } + else { + ofit->second->close(); + 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(); } -string writeout::getdatfile() +string writeout::getdatfile(string subname) { string myfile; DIR *dp; @@ -137,15 +164,21 @@ string writeout::getdatfile() 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); - 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; + } } + closedir(dp); return ""; }