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<hypercache::observable> hypercache::Os;
10 int hypercache::NEQUI;
11 int hypercache::NSKIP;
12 int hypercache::nequileft;
13 pair<int, char*> hypercache::mostEquilibratedConfig;
15 void hypercache::initCache(configcache **cache,
16 const string& cacheid, const int& nequi, const int& nskip, const string& datadir,
17 char **configmem, const int& configMemSize, const int& cachemode, ostream* _log) {
21 *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, log);
23 for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit)
24 (*cache)->addPara(parit->parid, parit->val);
26 for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit)
27 (*cache)->setPara(parit->parid, parit->val);
33 void hypercache::addPara(const string& parid, const double& val) {
35 newpara.parid = parid;
37 delayedParaAdd.push_back(newpara);
40 C->addPara(parid, val);
42 for (vector<observable>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
43 osit->c->addPara(parid, val);
46 void hypercache::setPara(const string& parid, const double& val) {
48 newpara.parid = parid;
50 delayedParaSet.push_back(newpara);
53 C->setPara(parid, val);
55 for (vector<observable>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
56 osit->c->setPara(parid, val);
59 mostEquilibratedConfig.first = NEQUI;
62 void hypercache::finalize() {
63 for (vector<observable>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
69 string hypercache::fileOfPath(const string& dressedfile) {
70 return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
73 bool hypercache::readC() {
74 bool readAtLeastOneConfig;
76 C->readConfig(&readAtLeastOneConfig, &nequileft, &parentConfigs);
78 if ( nequileft < 0 && readAtLeastOneConfig )
79 activeCFile = fileOfPath(C->getInFileName());
83 /* try to find more equilibrated config-file configuration for equilibration in excluded files */
84 if ( ( nequileft >= 0 || ( ! readAtLeastOneConfig ) ) && C->inFilesLeft() > 0 ) {
85 int nEquiLeftBeforeExcludedFileCheck = nequileft;
86 *log << "HYPERCACHE: Trying to find more equilibrated config in excluded files." << endl << flush;
88 char *tmpconfig = (char*) malloc(C->getConfigSize());
91 bool readnewconfig_ex;
92 int nequileftReadConfig_ex;
94 memcpy (tmpconfig, C->getConfigMem(), C->getConfigSize());
95 C->readConfig(&readnewconfig_ex, &nequileftReadConfig_ex, NULL);
97 if (! readnewconfig_ex) {
98 *log << "HYPERCACHE: No more excluded config-files for possible equilibration available." << endl << flush;
102 if (nequileftReadConfig_ex <= nequileft) {
103 nequileft = nequileftReadConfig_ex;
104 readAtLeastOneConfig = false;
106 else if (nequileftReadConfig_ex > nequileft) {
107 memcpy (C->getConfigMem(), tmpconfig, C->getConfigSize());
112 if (nEquiLeftBeforeExcludedFileCheck > nequileft)
113 *log << "HYPERCACHE: Found more equilibrated or same equilibrated excluded config-file configuration for equilibration." << endl << flush;
115 *log << "HYPERCACHE: Excluded config-file configuration for equilibration is less equilibrated than actual config." << endl << flush;
118 /* storing most equilibrated config */
119 if ( nequileft < mostEquilibratedConfig.first && readAtLeastOneConfig ) {
120 mostEquilibratedConfig.first = nequileft;
121 memcpy(mostEquilibratedConfig.second, C->getConfigMem(), C->getConfigSize());
124 /* injecting back most equilibrated config if nothing could be used anymore */
125 if ( (!readAtLeastOneConfig) && mostEquilibratedConfig.first < nequileft ) {
126 *log << "HYPERCACHE: Injecting back most equilibrated stored config with nequileft = " << mostEquilibratedConfig.first << endl << flush;
127 nequileft = mostEquilibratedConfig.first;
128 memcpy(C->getConfigMem(), mostEquilibratedConfig.second, C->getConfigSize());
131 return readAtLeastOneConfig;
134 void hypercache::writeC() {
140 C->writeConfig(NEQUI-nequileft-NSKIP);
141 activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
144 void hypercache::writeO(int obsid) {
145 if ( ( Os[obsid].lastWrittenObsEquis + NSKIP != NEQUI-nequileft-NSKIP ) && Os[obsid].c->isOutFileOpen() )
146 Os[obsid].c->finishOutFile();
148 if ( activeCFile != "" ) {
149 unsigned long afilehash = configcache::hash(activeCFile);
150 Os[obsid].c->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long), NEQUI-nequileft-NSKIP);
152 Os[obsid].c->writeConfig(NEQUI-nequileft-NSKIP);
154 Os[obsid].lastWrittenObsEquis = NEQUI-nequileft-NSKIP;
157 void hypercache::readO(int obsid, bool *readNewObsConfig, int *nequiObsLeft) {
158 Os[obsid].c->readConfig(readNewObsConfig, nequiObsLeft);
160 if ( *readNewObsConfig && *nequiObsLeft < 0 ) {
161 unsigned long *parentconfig = (unsigned long*)Os[obsid].c->getHeader("concurrent_cfile");
162 if ( parentconfig != NULL )
163 addParentConfig(parentconfig);
167 void hypercache::addParentConfig(const unsigned long *parentconfig) {
168 for (vector<unsigned long>::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit)
169 if ( *parit == *parentconfig )
172 parentConfigs.push_back(*parentconfig);