1 #include "hypercache.h"
6 configcache *hypercache::C = NULL;
7 vector<hypercache::para> hypercache::delayedParaAdd;
8 vector<hypercache::para> hypercache::delayedParaSet;
9 string hypercache::activeCFile = "";
10 vector<unsigned long> hypercache::parentConfigs;
11 ostream* hypercache::log;
12 vector<hypercache::observable> hypercache::Os;
13 int hypercache::NEQUI;
14 int hypercache::NSKIP;
15 int hypercache::nequileft;
16 pair<int, char*> hypercache::mostEquilibratedConfig;
18 void hypercache::initCache(configcache **cache,
19 const string& cacheid, const int& nequi, const int& nskip, const string& datadir,
20 char **configmem, const int& configMemSize, const int& cachemode, ostream* _log) {
24 *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, log);
26 for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit)
27 (*cache)->addPara(parit->parid, parit->val);
29 for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit)
30 (*cache)->setPara(parit->parid, parit->val);
36 void hypercache::addPara(const string& parid, const double& val) {
38 newpara.parid = parid;
40 delayedParaAdd.push_back(newpara);
43 C->addPara(parid, val);
45 for (vector<observable>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
46 osit->c->addPara(parid, val);
49 void hypercache::setPara(const string& parid, const double& val) {
51 newpara.parid = parid;
53 delayedParaSet.push_back(newpara);
56 C->setPara(parid, val);
58 for (vector<observable>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
59 osit->c->setPara(parid, val);
62 mostEquilibratedConfig.first = NEQUI;
65 void hypercache::finalize() {
66 for (vector<observable>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
72 string hypercache::fileOfPath(const string& dressedfile) {
73 return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
76 bool hypercache::readC() {
77 bool readAtLeastOneConfig;
79 C->readConfig(&readAtLeastOneConfig, &nequileft, &parentConfigs);
81 if ( nequileft < 0 && readAtLeastOneConfig )
82 activeCFile = fileOfPath(C->getInFileName());
86 /* try to find more equilibrated config-file configuration for equilibration in excluded files */
87 if ( ( nequileft >= 0 || ( ! readAtLeastOneConfig ) ) && C->inFilesLeft() > 0 ) {
88 int nEquiLeftBeforeExcludedFileCheck = nequileft;
89 *log << "HYPERCACHE: Trying to find more equilibrated config in excluded files." << endl << flush;
91 char *tmpconfig = (char*) malloc(C->getConfigSize());
94 bool readnewconfig_ex;
95 int nequileftReadConfig_ex;
97 memcpy (tmpconfig, C->getConfigMem(), C->getConfigSize());
98 C->readConfig(&readnewconfig_ex, &nequileftReadConfig_ex, NULL);
100 if (! readnewconfig_ex) {
101 *log << "HYPERCACHE: No more excluded config-files for possible equilibration available." << endl << flush;
105 if (nequileftReadConfig_ex <= nequileft) {
106 nequileft = nequileftReadConfig_ex;
107 readAtLeastOneConfig = false;
109 else if (nequileftReadConfig_ex > nequileft) {
110 memcpy (C->getConfigMem(), tmpconfig, C->getConfigSize());
115 if (nEquiLeftBeforeExcludedFileCheck > nequileft)
116 *log << "HYPERCACHE: Found more equilibrated or same equilibrated excluded config-file configuration for equilibration." << endl << flush;
118 *log << "HYPERCACHE: Excluded config-file configuration for equilibration is less equilibrated than actual config." << endl << flush;
121 /* storing most equilibrated config */
122 if ( nequileft < mostEquilibratedConfig.first && readAtLeastOneConfig ) {
123 mostEquilibratedConfig.first = nequileft;
124 memcpy(mostEquilibratedConfig.second, C->getConfigMem(), C->getConfigSize());
127 /* injecting back most equilibrated config if nothing could be used anymore */
128 if ( (!readAtLeastOneConfig) && mostEquilibratedConfig.first < nequileft ) {
129 *log << "HYPERCACHE: Injecting back most equilibrated stored config with nequileft = " << mostEquilibratedConfig.first << endl << flush;
130 nequileft = mostEquilibratedConfig.first;
131 memcpy(C->getConfigMem(), mostEquilibratedConfig.second, C->getConfigSize());
134 return readAtLeastOneConfig;
137 void hypercache::writeC() {
143 C->writeConfig(NEQUI-nequileft-NSKIP);
144 activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
147 void hypercache::writeO(int obsid) {
148 if ( ( Os[obsid].lastWrittenObsEquis + NSKIP != NEQUI-nequileft-NSKIP ) && Os[obsid].c->isOutFileOpen() )
149 Os[obsid].c->finishOutFile();
151 if ( activeCFile != "" ) {
152 unsigned long afilehash = configcache::hash(activeCFile);
153 Os[obsid].c->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long), NEQUI-nequileft-NSKIP);
155 Os[obsid].c->writeConfig(NEQUI-nequileft-NSKIP);
157 Os[obsid].lastWrittenObsEquis = NEQUI-nequileft-NSKIP;
160 void hypercache::readO(int obsid, bool *readNewObsConfig, int *nequiObsLeft) {
161 Os[obsid].c->readConfig(readNewObsConfig, nequiObsLeft);
163 if ( *readNewObsConfig && *nequiObsLeft < 0 ) {
164 unsigned long *parentconfig = (unsigned long*)Os[obsid].c->getHeader("concurrent_cfile");
165 if ( parentconfig != NULL )
166 addParentConfig(parentconfig);
170 void hypercache::addParentConfig(const unsigned long *parentconfig) {
171 for (vector<unsigned long>::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit)
172 if ( *parit == *parentconfig )
175 parentConfigs.push_back(*parentconfig);