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;
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<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
43 (*osit)->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<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
56 (*osit)->setPara(parid, val);
59 mostEquilibratedConfig.first = NEQUI;
62 void hypercache::finalize() {
63 for (vector<configcache*>::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 readnewconfig_nonex;
75 int nequileftReadConfig_nonex;
76 bool readAtLeastOneConfig = false;
78 C->readConfig(readnewconfig_nonex, nequileftReadConfig_nonex, &parentConfigs);
80 if (readnewconfig_nonex) {
81 nequileft = nequileftReadConfig_nonex;
82 readAtLeastOneConfig = true;
85 if ( nequileft < 0 && readnewconfig_nonex )
86 activeCFile = fileOfPath(C->getInFileName());
90 /* try to find more equilibrated config-file configuration for equilibration in excluded files */
91 if ( nequileft > 0 && C->inFilesLeft() > 0 ) {
92 *log << "HYPERCACHE: Trying to find more equilibrated config in excluded files." << endl << flush;
94 char *tmpconfig = (char*) malloc(C->getConfigSize());
97 bool readnewconfig_ex;
98 int nequileftReadConfig_ex;
100 memcpy (tmpconfig, C->getConfigMem(), C->getConfigSize());
101 C->readConfig(readnewconfig_ex, nequileftReadConfig_ex, NULL);
103 if (! readnewconfig_ex) {
104 *log << "HYPERCACHE: No more excluded config-files for possible equilibration available." << endl << flush;
108 if (nequileftReadConfig_ex <= nequileft) {
109 *log << "HYPERCACHE: Found more equilibrated or same equilibrated excluded config-file configuration for equilibration." << endl << flush;
110 nequileft = nequileftReadConfig_ex;
111 readAtLeastOneConfig = true;
113 else if (nequileftReadConfig_ex > nequileft) {
114 *log << "HYPERCACHE: Excluded config-file configuration for equilibration is less equilibrated than actual config." << endl << flush;
115 memcpy (C->getConfigMem(), tmpconfig, C->getConfigSize());
121 /* storing most equilibrated config */
122 if ( nequileft < mostEquilibratedConfig.first && readAtLeastOneConfig ) {
123 *log << "HYPERCACHE: Storing a copy of so far most equilibrated config with nequileft = " << nequileft << endl << flush;
124 mostEquilibratedConfig.first = nequileft;
125 memcpy(mostEquilibratedConfig.second, C->getConfigMem(), C->getConfigSize());
129 /* injecting back most equilibrated config if nothing could be used anymore */
130 if ( nequileft > 0 && mostEquilibratedConfig.first < nequileft ) {
131 *log << "HYPERCACHE: Injecting back most equilibrated stored config with nequileft = " << mostEquilibratedConfig.first << endl << flush;
132 nequileft = mostEquilibratedConfig.first;
133 memcpy(C->getConfigMem(), mostEquilibratedConfig.second, C->getConfigSize());
134 readAtLeastOneConfig = false;
137 return readAtLeastOneConfig;
140 void hypercache::writeC() {
146 C->writeConfig(NEQUI-nequileft-NSKIP);
147 activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
150 void hypercache::writeO(int obsid, int actnequi) {
151 if ( activeCFile != "" ) {
152 unsigned long afilehash = configcache::hash(activeCFile);
153 Os[obsid]->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long), actnequi);
155 Os[obsid]->writeConfig(actnequi);
158 void hypercache::readO(int obsid, bool& readnewconfig, int& nequileft) {
159 Os[obsid]->readConfig(readnewconfig, nequileft);
161 if ( nequileft < 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);