1 #include "hypercache.h"
 
   3 configcache *hypercache::C = NULL;
 
   4 vector<hypercache::para> hypercache::delayedParaAdd;
 
   5 vector<hypercache::para> hypercache::delayedParaSet;
 
   6 string hypercache::activeCFile = "";
 
   7 vector<unsigned long> hypercache::parentConfigs;
 
   8 ostream* hypercache::log;
 
   9 vector<configcache*> hypercache::Os;
 
  10 int hypercache::NEQUI;
 
  11 int hypercache::NSKIP;
 
  12 int hypercache::nequileft;
 
  14 void hypercache::initCache(configcache **cache,
 
  15                            const string& cacheid, const int& nequi, const int& nskip, const string& datadir, 
 
  16                            char **configmem, const int& configMemSize, const int& cachemode, ostream* _log) {
 
  20   *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, log);
 
  22   for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit)
 
  23     (*cache)->addPara(parit->parid, parit->val);
 
  25   for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit)
 
  26     (*cache)->setPara(parit->parid, parit->val);
 
  32 void hypercache::addPara(const string& parid, const double& val) {
 
  34   newpara.parid = parid;
 
  36   delayedParaAdd.push_back(newpara);
 
  39     C->addPara(parid, val);
 
  41   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
 
  42     (*osit)->addPara(parid, val);
 
  45 void hypercache::setPara(const string& parid, const double& val) {
 
  47   newpara.parid = parid;
 
  49   delayedParaSet.push_back(newpara);
 
  52     C->setPara(parid, val);
 
  54   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
 
  55     (*osit)->setPara(parid, val);
 
  60 void hypercache::finalize() {
 
  61   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
 
  67 string hypercache::fileOfPath(const string& dressedfile) {
 
  68   return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
 
  71 bool hypercache::readC() {
 
  72   bool readnewconfig_nonex;
 
  73   int nequileftReadConfig_nonex;
 
  74   bool readAtLeastOneConfig = false;
 
  76   C->readConfig(readnewconfig_nonex, nequileftReadConfig_nonex, &parentConfigs);
 
  78   if (readnewconfig_nonex) {
 
  79     cout << ":" << nequileftReadConfig_nonex << endl;
 
  80     nequileft = nequileftReadConfig_nonex;
 
  81     readAtLeastOneConfig = true;
 
  85     activeCFile = fileOfPath(C->getInFileName());
 
  89   /* try to find more equilibrated config-file configuration for equilibration in excluded files */
 
  90   if ( nequileft > 0 && C->inFilesLeft() > 0 ) {
 
  91     *log << "HYPERCACHE: Trying to find more equilibrated config in excluded files." << endl << flush;
 
  93     char *tmpconfig = (char*) malloc(C->getConfigSize());
 
  96       bool readnewconfig_ex;
 
  97       int nequileftReadConfig_ex;
 
  99       memcpy (tmpconfig, C->getConfigMem(), C->getConfigSize());
 
 100       C->readConfig(readnewconfig_ex, nequileftReadConfig_ex, NULL);
 
 102       if (! readnewconfig_ex) {
 
 103         *log << "HYPERCACHE: No more excluded config-files for possible equilibration available." << endl << flush;
 
 107       if (nequileftReadConfig_ex <= nequileft) {
 
 108         *log << "HYPERCACHE: Found more equilibrated or same equilibrated excluded config-file configuration for equilibration." << endl << flush;
 
 109         nequileft = nequileftReadConfig_ex;
 
 110         readAtLeastOneConfig = true;
 
 112       else if (nequileftReadConfig_ex > nequileft) {
 
 113         *log << "HYPERCACHE: Excluded config-file configuration for equilibration is less equilibrated than actual config." << endl << flush;
 
 114         memcpy (C->getConfigMem(), tmpconfig, C->getConfigSize());
 
 120   return readAtLeastOneConfig;
 
 123 void hypercache::writeC() {
 
 129   C->writeConfig(NEQUI-nequileft-NSKIP);
 
 130   activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
 
 133 void hypercache::writeO(int obsid, int actnequi) {
 
 134   if ( activeCFile != "" ) {
 
 135     unsigned long afilehash = configcache::hash(activeCFile);
 
 136     Os[obsid]->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long), actnequi);
 
 138   Os[obsid]->writeConfig(actnequi); 
 
 141 void hypercache::readO(int obsid, bool& readnewconfig, int& nequileft) {
 
 142   Os[obsid]->readConfig(readnewconfig, nequileft);
 
 144   if ( nequileft < 0 ) {
 
 145     unsigned long *parentconfig = (unsigned long*)Os[obsid]->getHeader("concurrent_cfile");
 
 146     if ( parentconfig != NULL )
 
 147       addParentConfig(parentconfig);
 
 151 void hypercache::addParentConfig(const unsigned long *parentconfig) {
 
 152   for (vector<unsigned long>::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit)
 
 153     if ( *parit == *parentconfig )
 
 156   parentConfigs.push_back(*parentconfig);