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 readnewconfig_nonex;
76 int nequileftReadConfig_nonex;
77 bool readAtLeastOneConfig = false;
79 C->readConfig(readnewconfig_nonex, nequileftReadConfig_nonex, &parentConfigs);
81 if (readnewconfig_nonex) {
82 nequileft = nequileftReadConfig_nonex;
83 readAtLeastOneConfig = true;
86 if ( nequileft < 0 && readnewconfig_nonex )
87 activeCFile = fileOfPath(C->getInFileName());
91 /* try to find more equilibrated config-file configuration for equilibration in excluded files */
92 if ( ( nequileft >= 0 || ( ! readAtLeastOneConfig ) ) && C->inFilesLeft() > 0 ) {
93 int nEquiLeftBeforeExcludedFileCheck = nequileft;
94 *log << "HYPERCACHE: Trying to find more equilibrated config in excluded files." << endl << flush;
96 char *tmpconfig = (char*) malloc(C->getConfigSize());
99 bool readnewconfig_ex;
100 int nequileftReadConfig_ex;
102 memcpy (tmpconfig, C->getConfigMem(), C->getConfigSize());
103 C->readConfig(readnewconfig_ex, nequileftReadConfig_ex, NULL);
105 if (! readnewconfig_ex) {
106 *log << "HYPERCACHE: No more excluded config-files for possible equilibration available." << endl << flush;
110 if (nequileftReadConfig_ex <= nequileft) {
111 nequileft = nequileftReadConfig_ex;
112 readAtLeastOneConfig = false;
114 else if (nequileftReadConfig_ex > nequileft) {
115 memcpy (C->getConfigMem(), tmpconfig, C->getConfigSize());
120 if (nEquiLeftBeforeExcludedFileCheck > nequileft)
121 *log << "HYPERCACHE: Found more equilibrated or same equilibrated excluded config-file configuration for equilibration." << endl << flush;
123 *log << "HYPERCACHE: Excluded config-file configuration for equilibration is less equilibrated than actual config." << endl << flush;
126 /* storing most equilibrated config */
127 if ( nequileft < mostEquilibratedConfig.first && readAtLeastOneConfig ) {
128 //*log << "HYPERCACHE: Storing a copy of so far most equilibrated config with nequileft = " << nequileft << endl << flush;
129 mostEquilibratedConfig.first = nequileft;
130 memcpy(mostEquilibratedConfig.second, C->getConfigMem(), C->getConfigSize());
133 /* injecting back most equilibrated config if nothing could be used anymore */
134 if ( (!readAtLeastOneConfig) && mostEquilibratedConfig.first < nequileft ) {
135 *log << "HYPERCACHE: Injecting back most equilibrated stored config with nequileft = " << mostEquilibratedConfig.first << endl << flush;
136 nequileft = mostEquilibratedConfig.first;
137 memcpy(C->getConfigMem(), mostEquilibratedConfig.second, C->getConfigSize());
138 //readAtLeastOneConfig = false;
141 return readAtLeastOneConfig;
144 void hypercache::writeC() {
150 C->writeConfig(NEQUI-nequileft-NSKIP);
151 activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
154 void hypercache::writeO(int obsid) {
155 if ( ( lastWrittenObsEquis + NSKIP != NEQUI-nequileft-NSKIP ) && Os[obsid]->isOutFileOpen() )
156 Os[obsid]->finishOutFile();
158 if ( activeCFile != "" ) {
159 unsigned long afilehash = configcache::hash(activeCFile);
160 Os[obsid]->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long), NEQUI-nequileft-NSKIP);
162 Os[obsid]->writeConfig(NEQUI-nequileft-NSKIP);
164 lastWrittenObsEquis = NEQUI-nequileft-NSKIP;
167 void hypercache::readO(int obsid, bool& readnewconfig, int& nequileft) {
168 Os[obsid]->readConfig(readnewconfig, nequileft);
170 if ( nequileft < 0 ) {
171 unsigned long *parentconfig = (unsigned long*)Os[obsid]->getHeader("concurrent_cfile");
172 if ( parentconfig != NULL )
173 addParentConfig(parentconfig);
177 void hypercache::addParentConfig(const unsigned long *parentconfig) {
178 for (vector<unsigned long>::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit)
179 if ( *parit == *parentconfig )
182 parentConfigs.push_back(*parentconfig);