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;
13 pair<int, char*> hypercache::mostEquilibratedConfig;
14 int hypercache::lastWrittenObsEquis;
16 void hypercache::initCache(configcache **cache,
17 const string& cacheid, const int& nequi, const int& nskip, const string& datadir,
18 char **configmem, const int& configMemSize, const int& cachemode, ostream* _log) {
22 *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, log);
24 for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit)
25 (*cache)->addPara(parit->parid, parit->val);
27 for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit)
28 (*cache)->setPara(parit->parid, parit->val);
34 void hypercache::addPara(const string& parid, const double& val) {
36 newpara.parid = parid;
38 delayedParaAdd.push_back(newpara);
41 C->addPara(parid, val);
43 for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
44 (*osit)->addPara(parid, val);
47 void hypercache::setPara(const string& parid, const double& val) {
49 newpara.parid = parid;
51 delayedParaSet.push_back(newpara);
54 C->setPara(parid, val);
56 for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
57 (*osit)->setPara(parid, val);
60 mostEquilibratedConfig.first = NEQUI;
63 void hypercache::finalize() {
64 for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
70 string hypercache::fileOfPath(const string& dressedfile) {
71 return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
74 bool hypercache::readC() {
75 bool readAtLeastOneConfig;
77 C->readConfig(&readAtLeastOneConfig, &nequileft, &parentConfigs);
79 if ( nequileft < 0 && readAtLeastOneConfig )
80 activeCFile = fileOfPath(C->getInFileName());
84 /* try to find more equilibrated config-file configuration for equilibration in excluded files */
85 if ( ( nequileft >= 0 || ( ! readAtLeastOneConfig ) ) && C->inFilesLeft() > 0 ) {
86 int nEquiLeftBeforeExcludedFileCheck = nequileft;
87 *log << "HYPERCACHE: Trying to find more equilibrated config in excluded files." << endl << flush;
89 char *tmpconfig = (char*) malloc(C->getConfigSize());
92 bool readnewconfig_ex;
93 int nequileftReadConfig_ex;
95 memcpy (tmpconfig, C->getConfigMem(), C->getConfigSize());
96 C->readConfig(&readnewconfig_ex, &nequileftReadConfig_ex, NULL);
98 if (! readnewconfig_ex) {
99 *log << "HYPERCACHE: No more excluded config-files for possible equilibration available." << endl << flush;
103 if (nequileftReadConfig_ex <= nequileft) {
104 nequileft = nequileftReadConfig_ex;
105 readAtLeastOneConfig = false;
107 else if (nequileftReadConfig_ex > nequileft) {
108 memcpy (C->getConfigMem(), tmpconfig, C->getConfigSize());
113 if (nEquiLeftBeforeExcludedFileCheck > nequileft)
114 *log << "HYPERCACHE: Found more equilibrated or same equilibrated excluded config-file configuration for equilibration." << endl << flush;
116 *log << "HYPERCACHE: Excluded config-file configuration for equilibration is less equilibrated than actual config." << endl << flush;
119 /* storing most equilibrated config */
120 if ( nequileft < mostEquilibratedConfig.first && readAtLeastOneConfig ) {
121 mostEquilibratedConfig.first = nequileft;
122 memcpy(mostEquilibratedConfig.second, C->getConfigMem(), C->getConfigSize());
125 /* injecting back most equilibrated config if nothing could be used anymore */
126 if ( (!readAtLeastOneConfig) && mostEquilibratedConfig.first < nequileft ) {
127 *log << "HYPERCACHE: Injecting back most equilibrated stored config with nequileft = " << mostEquilibratedConfig.first << endl << flush;
128 nequileft = mostEquilibratedConfig.first;
129 memcpy(C->getConfigMem(), mostEquilibratedConfig.second, C->getConfigSize());
132 return readAtLeastOneConfig;
135 void hypercache::writeC() {
141 C->writeConfig(NEQUI-nequileft-NSKIP);
142 activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
145 void hypercache::writeO(int obsid) {
146 if ( ( lastWrittenObsEquis + NSKIP != NEQUI-nequileft-NSKIP ) && Os[obsid]->isOutFileOpen() )
147 Os[obsid]->finishOutFile();
149 if ( activeCFile != "" ) {
150 unsigned long afilehash = configcache::hash(activeCFile);
151 Os[obsid]->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long), NEQUI-nequileft-NSKIP);
153 Os[obsid]->writeConfig(NEQUI-nequileft-NSKIP);
155 lastWrittenObsEquis = NEQUI-nequileft-NSKIP;
158 void hypercache::readO(int obsid, bool *readNewObsConfig, int *nequiObsLeft) {
159 Os[obsid]->readConfig(readNewObsConfig, nequiObsLeft);
161 if ( *readNewObsConfig && *nequiObsLeft < 0 ) {
162 unsigned long *parentconfig = (unsigned long*)Os[obsid]->getHeader("concurrent_cfile");
163 if ( parentconfig != NULL )
164 addParentConfig(parentconfig);
168 void hypercache::addParentConfig(const unsigned long *parentconfig) {
169 for (vector<unsigned long>::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit)
170 if ( *parit == *parentconfig )
173 parentConfigs.push_back(*parentconfig);