#include "hypercache.h"
-hypercache::defaults *hypercache::Defaults = NULL;
-configcache *hypercache::O = NULL;
configcache *hypercache::C = NULL;
+vector<hypercache::para> hypercache::delayedParaAdd;
+vector<hypercache::para> hypercache::delayedParaSet;
+string hypercache::activeCFile = "";
+vector<unsigned long> hypercache::parentConfigs;
+ostream* hypercache::log;
+vector<configcache*> hypercache::Os;
+int hypercache::NEQUI;
+int hypercache::NSKIP;
+int hypercache::nequileft;
+pair<int, char*> hypercache::mostEquilibratedConfig;
-void hypercache::initCache(configcache *cache,
+void hypercache::initCache(configcache **cache,
const string& cacheid, const int& nequi, const int& nskip, const string& datadir,
- char **configmem, const int& configMemSize, const int& cachemode, writeout *out_a) {
+ char **configmem, const int& configMemSize, const int& cachemode, ostream* _log) {
+ if ( _log != NULL )
+ log = _log;
- bool allInitBefore = true;
+ *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, log);
- if (O == NULL || C == NULL)
- allInitBefore = false;
+ for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit)
+ (*cache)->addPara(parit->parid, parit->val);
- cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, out_a);
+ 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) {
+ para newpara;
+ newpara.parid = parid;
+ newpara.val = val;
+ delayedParaAdd.push_back(newpara);
+
+ if (C != NULL)
+ C->addPara(parid, val);
+
+ for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
+ (*osit)->addPara(parid, val);
+}
+
+void hypercache::setPara(const string& parid, const double& val) {
+ para newpara;
+ newpara.parid = parid;
+ newpara.val = val;
+ delayedParaSet.push_back(newpara);
+
+ if (C != NULL)
+ C->setPara(parid, val);
- if (Defaults == NULL) {
- Defaults = new defaults;
- Defaults->nequi = nequi;
- Defaults->nskip = nskip;
- Defaults->datadir = datadir;
- Defaults->out = out_a;
+ for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
+ (*osit)->setPara(parid, val);
+
+ nequileft = NEQUI;
+ mostEquilibratedConfig.first = NEQUI;
+}
+
+void hypercache::finalize() {
+ for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
+ delete *osit;
+
+ delete C;
+}
+
+string hypercache::fileOfPath(const string& dressedfile) {
+ return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
+}
+
+bool hypercache::readC() {
+ bool readnewconfig_nonex;
+ int nequileftReadConfig_nonex;
+ bool readAtLeastOneConfig = false;
+
+ C->readConfig(readnewconfig_nonex, nequileftReadConfig_nonex, &parentConfigs);
+
+ if (readnewconfig_nonex) {
+ nequileft = nequileftReadConfig_nonex;
+ readAtLeastOneConfig = true;
}
- if (O != NULL && C != NULL && !allInitBefore) {
- for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit) {
- O->addPara(parit->parid, parit->val);
- C->addPara(parit->parid, parit->val);
- }
- for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit) {
- O->setPara(parit->parid, parit->val);
- C->setPara(parit->parid, parit->val);
+ if ( nequileft < 0 && readnewconfig_nonex )
+ 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);
}
-}
-void hypercache::initCache(configcache *cache, const string& subdir,
- const string& cacheid, char **configmem, const int& configMemSize, const int& cachemode) {
- if (Defaults == NULL) {
- cerr << "Defaults were not initialized prior to short initialization!" << endl;
- exit(1);
+ /* storing most equilibrated config */
+ if ( nequileft < mostEquilibratedConfig.first && readAtLeastOneConfig ) {
+ *log << "HYPERCACHE: Storing a copy of so far most equilibrated config with nequileft = " << nequileft << endl << flush;
+ mostEquilibratedConfig.first = nequileft;
+ memcpy(mostEquilibratedConfig.second, C->getConfigMem(), C->getConfigSize());
}
+
- initCache(cache, cacheid, Defaults->nequi, Defaults->nskip, Defaults->datadir + "/" + subdir,
- configmem, configMemSize, cachemode, Defaults->out);
+ /* injecting back most equilibrated config if nothing could be used anymore */
+ if ( nequileft > 0 && mostEquilibratedConfig.first < nequileft ) {
+ *log << "HYPERCACHE: Injecting back most equilibrated stored config with nequileft = " << mostEquilibratedConfig.first << endl << flush;
+ nequileft = mostEquilibratedConfig.first;
+ memcpy(C->getConfigMem(), mostEquilibratedConfig.second, C->getConfigSize());
+ readAtLeastOneConfig = false;
+ }
+
+ return readAtLeastOneConfig;
}
-void hypercache::addPara(const string& parid, const double& val) {
- if (O == NULL || C == NULL) {
- para newpara;
- newpara.parid = parid;
- newpara.val = val;
- delayedParaAdd.push_back(newpara);
- }
- else {
- O->addPara(parid, val);
- C->addPara(parid, val);
- }
+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 ) );
}
-void hypercache::setPara(const string& parid, const double& val) {
- if (O == NULL || C == NULL) {
- para newpara;
- newpara.parid = parid;
- newpara.val = val;
- delayedParaSet.push_back(newpara);
+void hypercache::writeO(int obsid, int actnequi) {
+ if ( activeCFile != "" ) {
+ unsigned long afilehash = configcache::hash(activeCFile);
+ Os[obsid]->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long), actnequi);
}
- else {
- O->setPara(parid, val);
- C->setPara(parid, val);
+ Os[obsid]->writeConfig(actnequi);
+}
+
+void hypercache::readO(int obsid, bool& readnewconfig, int& nequileft) {
+ Os[obsid]->readConfig(readnewconfig, nequileft);
+
+ if ( nequileft < 0 ) {
+ unsigned long *parentconfig = (unsigned long*)Os[obsid]->getHeader("concurrent_cfile");
+ if ( parentconfig != NULL )
+ addParentConfig(parentconfig);
}
}
+
+void hypercache::addParentConfig(const unsigned long *parentconfig) {
+ for (vector<unsigned long>::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit)
+ if ( *parit == *parentconfig )
+ return;
+
+ parentConfigs.push_back(*parentconfig);
+}