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 //*log << "HYPERCACHE: Storing a copy of so far most equilibrated config with nequileft = " << nequileft << endl << flush;
122 mostEquilibratedConfig.first = nequileft;
123 memcpy(mostEquilibratedConfig.second, C->getConfigMem(), C->getConfigSize());
126 /* injecting back most equilibrated config if nothing could be used anymore */
127 if ( (!readAtLeastOneConfig) && mostEquilibratedConfig.first < nequileft ) {
128 *log << "HYPERCACHE: Injecting back most equilibrated stored config with nequileft = " << mostEquilibratedConfig.first << endl << flush;
129 nequileft = mostEquilibratedConfig.first;
130 memcpy(C->getConfigMem(), mostEquilibratedConfig.second, C->getConfigSize());
131 //readAtLeastOneConfig = false;
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 ( ( lastWrittenObsEquis + NSKIP != NEQUI-nequileft-NSKIP ) && Os[obsid]->isOutFileOpen() )
149 Os[obsid]->finishOutFile();
151 if ( activeCFile != "" ) {
152 unsigned long afilehash = configcache::hash(activeCFile);
153 Os[obsid]->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long), NEQUI-nequileft-NSKIP);
155 Os[obsid]->writeConfig(NEQUI-nequileft-NSKIP);
157 lastWrittenObsEquis = NEQUI-nequileft-NSKIP;
160 void hypercache::readO(int obsid, bool& readnewconfig, int& nequileft) {
161 Os[obsid]->readConfig(readnewconfig, nequileft);
163 if ( nequileft < 0 ) {
164 unsigned long *parentconfig = (unsigned long*)Os[obsid]->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);