From 46e492935d31a36641b8e8a5cd27bd86e7df96c4 Mon Sep 17 00:00:00 2001
From: Alex Schmidt <alex@treefish.org>
Date: Tue, 18 Jun 2013 15:46:56 +0200
Subject: [PATCH 1/1] ...

---
 configcache.cpp | 24 +++++++++++++------
 configcache.h   |  6 ++++-
 hypercache.cpp  | 64 +++++++++++++++++++++++++++++++++++++++++++++----
 hypercache.h    | 10 ++++++--
 o815/o815.cpp   |  2 --
 o815/o815.h     |  1 -
 o815/sim.cpp    | 58 +++++++++++---------------------------------
 7 files changed, 104 insertions(+), 61 deletions(-)

diff --git a/configcache.cpp b/configcache.cpp
index 168de5e..35e8548 100644
--- a/configcache.cpp
+++ b/configcache.cpp
@@ -73,7 +73,7 @@ bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
 
   filedesc->filename = infile;
   filedesc->doVirtualEquilibration = false;
-  filedesc->readEquilibratedConfigs = 0;
+  filedesc->firstConfig = true;
 
   if( infile.size() < 4 ) return false;
 
@@ -183,7 +183,6 @@ void * configcache::getHeader(const string& headerid) {
 void configcache::readConfig(bool& readnewconfig, int& nequileft, vector<unsigned long> *excludeFileHashes)
 {
   readnewconfig = false;
-  nequileft = NEQUI;
 
   if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return;
 
@@ -196,7 +195,11 @@ void configcache::readConfig(bool& readnewconfig, int& nequileft, vector<unsigne
     {
       vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
 
-      if( (!inFile.is_open()) && inFileIt == inFiles.end() ) return;
+      if( (!inFile.is_open()) && inFileIt == inFiles.end() ) {
+	cout << "RETURNING..." << endl;
+	nequileft = nequileft_internal;
+	return;
+      }
 
       while( (!inFile.is_open()) && inFiles.size() > 0 ) {
 	openFileDesc = *inFileIt;
@@ -221,20 +224,27 @@ void configcache::readConfig(bool& readnewconfig, int& nequileft, vector<unsigne
 	    for (int iskip=0; iskip < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iskip++) {
 	      if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
 		break;
-	      else if ( (NEQUI-openFileDesc.nequi) - (iskip+1)*openFileDesc.nskip < nequileft ) {
+	      else if ( (NEQUI-openFileDesc.nequi) - (iskip+1)*openFileDesc.nskip < nequileft_internal ) {
 		memcpy(configMem, tmpConfig, configSize);
-		nequileft = (NEQUI-openFileDesc.nequi) - (iskip+1)*openFileDesc.nskip;
+		nequileft_internal = NEQUI - openFileDesc.nequi - (iskip+1)*openFileDesc.nskip;
+		cout << "blabla:" << nequileft_internal << endl;
 		readnewconfig = true;
 	      }
 	    }
+	    openFileDesc.firstConfig = false;
 	  }
 
 	  if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
 	    {
 	      memcpy(configMem, tmpConfig, configSize);
 	      readnewconfig = true;
-	      nequileft = NEQUI - openFileDesc.nequi - openFileDesc.readEquilibratedConfigs*openFileDesc.nskip;
-	      openFileDesc.readEquilibratedConfigs++;
+	      if (openFileDesc.firstConfig) {
+		openFileDesc.firstConfig = false;
+		nequileft_internal = NEQUI - openFileDesc.nequi;
+	      }
+	      nequileft_internal -= openFileDesc.nskip;
+	      cout << "sadasd:" << nequileft_internal << endl;
+	      nequileft = nequileft_internal;
 	      return;
 	    }
 	  else {
diff --git a/configcache.h b/configcache.h
index 0af533a..869fed6 100644
--- a/configcache.h
+++ b/configcache.h
@@ -30,7 +30,7 @@ struct infiledesc {
   int nskip;
   bool doVirtualEquilibration;
   bool extended;
-  int readEquilibratedConfigs;
+  bool firstConfig;
 };
 
 class configcache{
@@ -49,6 +49,8 @@ class configcache{
   static unsigned long hash(const string& str);
   void closeInFile() { inFile.close(); }
   int inFilesLeft() { return inFiles.size(); }
+  char* getConfigMem() { return configMem; }
+  int getConfigSize() { return configSize; }
 
  private:
   ostream* log;
@@ -105,6 +107,8 @@ class configcache{
   bool readAllHeaders();
 
   vector<infiledesc>::iterator getNextInfile(vector<unsigned long> *excludeFileHashes);
+
+  int nequileft_internal;
 };
 
 #endif
diff --git a/hypercache.cpp b/hypercache.cpp
index 7b3f470..0f8d856 100644
--- a/hypercache.cpp
+++ b/hypercache.cpp
@@ -7,6 +7,9 @@ string hypercache::activeCFile = "";
 vector<unsigned long> hypercache::parentConfigs;
 ostream* hypercache::log;
 vector<configcache*> hypercache::Os;
+int hypercache::NEQUI;
+int hypercache::NSKIP;
+int hypercache::nequileft;
 
 void hypercache::initCache(configcache **cache,
 			   const string& cacheid, const int& nequi, const int& nskip, const string& datadir, 
@@ -21,6 +24,9 @@ void hypercache::initCache(configcache **cache,
 
   for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit)
     (*cache)->setPara(parit->parid, parit->val);
+
+  NEQUI = nequi;
+  NSKIP = nskip;
 }
 
 void hypercache::addPara(const string& parid, const double& val) {
@@ -47,6 +53,8 @@ void hypercache::setPara(const string& parid, const double& val) {
   
   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
     (*osit)->setPara(parid, val);
+
+  nequileft = NEQUI;
 }
 
 void hypercache::finalize() {
@@ -60,17 +68,65 @@ string hypercache::fileOfPath(const string& dressedfile) {
   return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
 }
 
-void hypercache::readC(bool& readnewconfig, int& nequileft) {
-  C->readConfig(readnewconfig, nequileft, &parentConfigs);
+bool hypercache::readC() {
+  bool readnewconfig_nonex;
+  int nequileftReadConfig_nonex;
+  bool readAtLeastOneConfig = false;
+
+  C->readConfig(readnewconfig_nonex, nequileftReadConfig_nonex, &parentConfigs);
+
+  if (readnewconfig_nonex) {
+    cout << ":" << nequileftReadConfig_nonex << endl;
+    nequileft = nequileftReadConfig_nonex;
+    readAtLeastOneConfig = true;
+  }
 
   if ( nequileft < 0 )
     activeCFile = fileOfPath(C->getInFileName());
   else
     activeCFile = "";
+
+  /* try to find more equilibrated config-file configuration for equilibration in excluded files */
+  if ( nequileft > 0 && C->inFilesLeft() > 0 ) {
+    *log << "HYPERCACHE: Trying to find more equilibrated config in excluded files." << endl << flush;
+
+    char *tmpconfig = (char*) malloc(C->getConfigSize());
+
+    while (true) {
+      bool readnewconfig_ex;
+      int nequileftReadConfig_ex;
+
+      memcpy (tmpconfig, C->getConfigMem(), C->getConfigSize());
+      C->readConfig(readnewconfig_ex, nequileftReadConfig_ex, NULL);
+
+      if (! readnewconfig_ex) {
+	*log << "HYPERCACHE: No more excluded config-files for possible equilibration available." << endl << flush;
+	break;
+      }
+      
+      if (nequileftReadConfig_ex <= nequileft) {
+	*log << "HYPERCACHE: Found more equilibrated or same equilibrated excluded config-file configuration for equilibration." << endl << flush;
+	nequileft = nequileftReadConfig_ex;
+	readAtLeastOneConfig = true;
+      }
+      else if (nequileftReadConfig_ex > nequileft) {
+	*log << "HYPERCACHE: Excluded config-file configuration for equilibration is less equilibrated than actual config." << endl << flush;
+	memcpy (C->getConfigMem(), tmpconfig, C->getConfigSize());
+      }
+    }
+    free(tmpconfig);
+  }
+
+  return readAtLeastOneConfig;
 }
 
-void hypercache::writeC(int actnequi) {
-  C->writeConfig(actnequi);
+void hypercache::writeC() {
+  if (nequileft > 0)
+    nequileft = 0;
+
+   nequileft -= NSKIP;
+
+  C->writeConfig(NEQUI-nequileft-NSKIP);
   activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
 }
 
diff --git a/hypercache.h b/hypercache.h
index 1daa675..79fe825 100644
--- a/hypercache.h
+++ b/hypercache.h
@@ -20,13 +20,15 @@ class hypercache {
 		    char **configmem, const int& configMemSize, const int& cachemode=CACHE_MODE_FULL, ostream *_log=NULL) {
     initCache(&C, cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, _log);
   }
+
+  static int getNEquiLeft() { return nequileft; }
   
   static void addPara(const string& parid, const double& val=0);
   static void setPara(const string& parid, const double& value);
   static void readO(int obsid, bool& readnewconfig, int& nequileft);
-  static void readC(bool& readnewconfig, int& nequileft);
+  static bool readC();
   static void writeO(int obsid, int actnequi);
-  static void writeC(int actnequi);
+  static void writeC();
   static void writeHeaderO(int obsid, const string& headerid, char *header, long unsigned int size, int actnequi) { Os[obsid]->writeHeader(headerid, header, size, actnequi); }
   static void *getHeaderO(int obsid, const string& headerid) { Os[obsid]->getHeader(headerid); }
   static void writeHeaderC(const string& headerid, char *header, long unsigned int size, int actnequi) { C->writeHeader(headerid, header, size, actnequi); }
@@ -59,6 +61,10 @@ class hypercache {
   static vector<unsigned long> parentConfigs;
   static void addParentConfig(const unsigned long *parentconfig);
   static ostream* log;
+
+  static int NEQUI;
+  static int NSKIP;
+  static int nequileft;
 };
 
 #endif
diff --git a/o815/o815.cpp b/o815/o815.cpp
index 878df0f..9afaa0c 100644
--- a/o815/o815.cpp
+++ b/o815/o815.cpp
@@ -135,8 +135,6 @@ void o815::mainLoop() {
       *out->log << " " << (*obsit)->obsid;
     *out->log << endl << flush;
 
-    Sim->nequileft = comargs.nequi;
-
     for( int imeas=0; imeas<comargs.nmeas; imeas++ ) {
       bool nextAlready = false;
       for (vector<obs*>::iterator obsit = observables.begin(); obsit != observables.end(); ++obsit) {
diff --git a/o815/o815.h b/o815/o815.h
index 67ef320..ac192cf 100644
--- a/o815/o815.h
+++ b/o815/o815.h
@@ -45,7 +45,6 @@ class o815 {
     void nextConfig();
     char *confMem;
     virtual void _newParas() {};
-    int nequileft;
   private:
     virtual void _reset()=0;
     virtual void _makeSweep()=0;
diff --git a/o815/sim.cpp b/o815/sim.cpp
index b37549a..6abf7c6 100644
--- a/o815/sim.cpp
+++ b/o815/sim.cpp
@@ -18,67 +18,37 @@ void o815::sim::nextConfig() {
   bool skippedInEqui = false;
   bool readNoSingleConfig = true;
   bool createdNoNewConfigs = true;
+  bool readAtLeastOneConfig;
 
-  hypercache::readC(readnewconfig, nequileftReadConfig);
-
-  if (readnewconfig) {
-    nequileft = nequileftReadConfig;
-    readNoSingleConfig = false;
-  }
-
-  /* try to find more equilibrated config-file for equilibration including excluded files */
-  if ( nequileft > 0 && hypercache::CFilesLeft() > 0 ) {
-    char *tmpconfig = (char*) malloc(confmemSize);
-
-    while (true) {
-      memcpy (tmpconfig, confMem, confmemSize);
-      hypercache::readC(readnewconfig, nequileftReadConfig);
-
-      if (! readnewconfig) {
-	*log << "SIM: No more excluded config-files for possible equilibration available." << endl << flush;
-	break;
-      }
-      
-      if (nequileftReadConfig <= nequileft) {
-	*log << "SIM: Found more equilibrated or same equilibrated excluded config-file configuration for equilibration." << endl << flush;
-	nequileft = nequileftReadConfig;
-	readNoSingleConfig = false;
-      }
-      else if (nequileftReadConfig > nequileft) {
-	*log << "SIM: Excluded config-file configuration for equilibration is less equilibrated than actual config." << endl << flush;
-	memcpy (confMem, tmpconfig, confmemSize);
-      }
-    }
-    free(tmpconfig);
-  }
+  readAtLeastOneConfig = hypercache::readC();
 
   /* equilibrate if necessary */
-  if (nequileft > 0) {
+  if (hypercache::getNEquiLeft() > 0) {
     *log << "SIM: Starting equilibration." << endl << flush;
-    if (nequileft != nequi)
-      *log << "SIM: " << nequileft << " of " << nequi << " equilibration steps left after virtual equilibration." << endl << flush;
-    progress equiProg(nequileft);
-    for ( int iequi=0; iequi<nequileft; iequi++ ) {
+    if (hypercache::getNEquiLeft() != nequi)
+      *log << "SIM: " << hypercache::getNEquiLeft() << " of " << nequi << " equilibration steps left after virtual equilibration." << endl << flush;
+    progress equiProg(hypercache::getNEquiLeft());
+    for ( int iequi = 0; iequi < hypercache::getNEquiLeft(); iequi++ ) {
       _makeSweep();
       while( equiProg.madeStep(iequi) ) *log << "SIM: " << equiProg.done()*100 << "% of equilibration done." << endl << flush;
     }
-    nequileft = 0;
     skippedInEqui = true;
     createdNoNewConfigs = false;
   }
 
   /* make skip if no config could be read */
-  if ( ! skippedInEqui && readNoSingleConfig ) {
-    cout << "here" << endl;
+  if ( (! readAtLeastOneConfig) ) {
+    cout << "SKIPPING" << endl;
     for (int iskip=0; iskip<nskip; iskip++) {
       _makeSweep();
-      nequileft--;
     }
     createdNoNewConfigs = false;
   }
  
-  if (! createdNoNewConfigs)
-    hypercache::writeC(nequi-nequileft);
+  if (! createdNoNewConfigs) {
+    cout << "::" << hypercache::getNEquiLeft() << endl;
+    hypercache::writeC();
+  }
 
-  cout << nequileft << endl;
+  cout << hypercache::getNEquiLeft() << endl;
 }
-- 
2.39.5