#include <dirent.h>
#include <errno.h>
#include <unistd.h>
+#include <mpi.h>
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) {
}
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;
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(×tamp, 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(×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 ( !logf.is_open() ) {
if((dp = opendir(fulldir.c_str())) == NULL) {
logf << "Error(" << errno << ") opening " << fulldir << endl;
+ closedir(dp);
return "";
}
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 "";
}