]> git.treefish.org Git - phys/latlib.git/blob - hypercache.cpp
Made c++11 standard dependency obsolete.
[phys/latlib.git] / hypercache.cpp
1 #include "hypercache.h"
2
3 #include <cstdlib>
4 #include <cstring>
5
6 configcache *hypercache::C = NULL;
7 vector<hypercache::para> hypercache::delayedParaAdd;
8 vector<hypercache::para> hypercache::delayedParaSet;
9 string hypercache::activeCFile = "";
10 vector<unsigned long> hypercache::parentConfigs;
11 ostream* hypercache::log;
12 vector<hypercache::observable> hypercache::Os;
13 int hypercache::NEQUI;
14 int hypercache::NSKIP;
15 int hypercache::nequileft;
16 pair<int, char*> hypercache::mostEquilibratedConfig;
17
18 void hypercache::initCache(configcache **cache,
19                            const string& cacheid, const int& nequi, const int& nskip, const string& datadir, 
20                            char **configmem, const int& configMemSize, const int& cachemode, ostream* _log) {
21   if ( _log != NULL )
22     log = _log;
23   
24   *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, log);
25   
26   for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit)
27     (*cache)->addPara(parit->parid, parit->val);
28
29   for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit)
30     (*cache)->setPara(parit->parid, parit->val);
31
32   NEQUI = nequi;
33   NSKIP = nskip;
34 }
35
36 void hypercache::addPara(const string& parid, const double& val) {
37   para newpara; 
38   newpara.parid = parid;
39   newpara.val = val;
40   delayedParaAdd.push_back(newpara);
41
42   if (C != NULL)
43     C->addPara(parid, val);
44
45   for (vector<observable>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
46     osit->c->addPara(parid, val);
47 }
48
49 void hypercache::setPara(const string& parid, const double& val) {
50   para newpara; 
51   newpara.parid = parid;
52   newpara.val = val;
53   delayedParaSet.push_back(newpara);
54
55   if (C != NULL)
56     C->setPara(parid, val);
57   
58   for (vector<observable>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
59     osit->c->setPara(parid, val);
60
61   nequileft = NEQUI;
62   mostEquilibratedConfig.first = NEQUI;
63 }
64
65 void hypercache::finalize() {
66   for (vector<observable>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
67     delete osit->c;
68   
69   delete C;
70 }
71
72 string hypercache::fileOfPath(const string& dressedfile) {
73   return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
74 }
75
76 bool hypercache::readC() {
77   bool readAtLeastOneConfig;
78
79   C->readConfig(&readAtLeastOneConfig, &nequileft, &parentConfigs);
80
81   if ( nequileft < 0 && readAtLeastOneConfig )
82     activeCFile = fileOfPath(C->getInFileName());
83   else
84     activeCFile = "";
85
86   /* try to find more equilibrated config-file configuration for equilibration in excluded files */
87   if ( ( nequileft >= 0 || ( ! readAtLeastOneConfig ) ) && C->inFilesLeft() > 0 ) {
88     int nEquiLeftBeforeExcludedFileCheck = nequileft;
89     *log << "HYPERCACHE: Trying to find more equilibrated config in excluded files." << endl << flush;
90
91     char *tmpconfig = (char*) malloc(C->getConfigSize());
92
93     while (true) {
94       bool readnewconfig_ex;
95       int nequileftReadConfig_ex;
96
97       memcpy (tmpconfig, C->getConfigMem(), C->getConfigSize());
98       C->readConfig(&readnewconfig_ex, &nequileftReadConfig_ex, NULL);
99
100       if (! readnewconfig_ex) {
101         *log << "HYPERCACHE: No more excluded config-files for possible equilibration available." << endl << flush;
102         break;
103       }
104       
105       if (nequileftReadConfig_ex <= nequileft) {
106         nequileft = nequileftReadConfig_ex;
107         readAtLeastOneConfig = false;
108       }
109       else if (nequileftReadConfig_ex > nequileft) {
110         memcpy (C->getConfigMem(), tmpconfig, C->getConfigSize());
111       }
112     }
113     free(tmpconfig);
114
115     if (nEquiLeftBeforeExcludedFileCheck > nequileft)
116       *log << "HYPERCACHE: Found more equilibrated or same equilibrated excluded config-file configuration for equilibration." << endl << flush;
117     else
118       *log << "HYPERCACHE: Excluded config-file configuration for equilibration is less equilibrated than actual config." << endl << flush;
119   }
120
121   /* storing most equilibrated config */
122   if ( nequileft < mostEquilibratedConfig.first && readAtLeastOneConfig ) {
123     mostEquilibratedConfig.first = nequileft;
124     memcpy(mostEquilibratedConfig.second, C->getConfigMem(), C->getConfigSize());
125   }
126
127   /* injecting back most equilibrated config if nothing could be used anymore */
128   if ( (!readAtLeastOneConfig) && mostEquilibratedConfig.first < nequileft ) {
129     *log << "HYPERCACHE: Injecting back most equilibrated stored config with nequileft = " << mostEquilibratedConfig.first << endl << flush;
130     nequileft = mostEquilibratedConfig.first;
131     memcpy(C->getConfigMem(), mostEquilibratedConfig.second, C->getConfigSize());
132   }
133
134   return readAtLeastOneConfig;
135 }
136
137 void hypercache::writeC() {
138   if (nequileft > 0)
139     nequileft = 0;
140
141    nequileft -= NSKIP;
142
143   C->writeConfig(NEQUI-nequileft-NSKIP);
144   activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
145 }
146
147 void hypercache::writeO(int obsid) {
148   if ( ( Os[obsid].lastWrittenObsEquis + NSKIP != NEQUI-nequileft-NSKIP ) && Os[obsid].c->isOutFileOpen() )
149     Os[obsid].c->finishOutFile();
150
151   if ( activeCFile != "" ) {
152     unsigned long afilehash = configcache::hash(activeCFile);
153     Os[obsid].c->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long), NEQUI-nequileft-NSKIP);
154   }
155   Os[obsid].c->writeConfig(NEQUI-nequileft-NSKIP); 
156
157   Os[obsid].lastWrittenObsEquis = NEQUI-nequileft-NSKIP;
158 }
159
160 void hypercache::readO(int obsid, bool *readNewObsConfig, int *nequiObsLeft) {
161   Os[obsid].c->readConfig(readNewObsConfig, nequiObsLeft);
162
163   if ( *readNewObsConfig && *nequiObsLeft < 0 ) {
164     unsigned long *parentconfig = (unsigned long*)Os[obsid].c->getHeader("concurrent_cfile");
165     if ( parentconfig != NULL )
166       addParentConfig(parentconfig);
167   }
168 }
169
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 )
173       return;
174
175   parentConfigs.push_back(*parentconfig);
176 }