]> git.treefish.org Git - phys/latlib.git/blob - hypercache.cpp
minor changes to test.
[phys/latlib.git] / hypercache.cpp
1 #include "hypercache.h"
2
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;
15
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) {
19   if ( _log != NULL )
20     log = _log;
21   
22   *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, log);
23   
24   for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit)
25     (*cache)->addPara(parit->parid, parit->val);
26
27   for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit)
28     (*cache)->setPara(parit->parid, parit->val);
29
30   NEQUI = nequi;
31   NSKIP = nskip;
32 }
33
34 void hypercache::addPara(const string& parid, const double& val) {
35   para newpara; 
36   newpara.parid = parid;
37   newpara.val = val;
38   delayedParaAdd.push_back(newpara);
39
40   if (C != NULL)
41     C->addPara(parid, val);
42
43   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
44     (*osit)->addPara(parid, val);
45 }
46
47 void hypercache::setPara(const string& parid, const double& val) {
48   para newpara; 
49   newpara.parid = parid;
50   newpara.val = val;
51   delayedParaSet.push_back(newpara);
52
53   if (C != NULL)
54     C->setPara(parid, val);
55   
56   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
57     (*osit)->setPara(parid, val);
58
59   nequileft = NEQUI;
60   mostEquilibratedConfig.first = NEQUI;
61 }
62
63 void hypercache::finalize() {
64   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
65     delete *osit;
66   
67   delete C;
68 }
69
70 string hypercache::fileOfPath(const string& dressedfile) {
71   return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
72 }
73
74 bool hypercache::readC() {
75   bool readAtLeastOneConfig;
76
77   C->readConfig(readAtLeastOneConfig, nequileft, &parentConfigs);
78
79   if ( nequileft < 0 && readAtLeastOneConfig )
80     activeCFile = fileOfPath(C->getInFileName());
81   else
82     activeCFile = "";
83
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;
88
89     char *tmpconfig = (char*) malloc(C->getConfigSize());
90
91     while (true) {
92       bool readnewconfig_ex;
93       int nequileftReadConfig_ex;
94
95       memcpy (tmpconfig, C->getConfigMem(), C->getConfigSize());
96       C->readConfig(readnewconfig_ex, nequileftReadConfig_ex, NULL);
97
98       if (! readnewconfig_ex) {
99         *log << "HYPERCACHE: No more excluded config-files for possible equilibration available." << endl << flush;
100         break;
101       }
102       
103       if (nequileftReadConfig_ex <= nequileft) {
104         nequileft = nequileftReadConfig_ex;
105         readAtLeastOneConfig = false;
106       }
107       else if (nequileftReadConfig_ex > nequileft) {
108         memcpy (C->getConfigMem(), tmpconfig, C->getConfigSize());
109       }
110     }
111     free(tmpconfig);
112
113     if (nEquiLeftBeforeExcludedFileCheck > nequileft)
114       *log << "HYPERCACHE: Found more equilibrated or same equilibrated excluded config-file configuration for equilibration." << endl << flush;
115     else
116       *log << "HYPERCACHE: Excluded config-file configuration for equilibration is less equilibrated than actual config." << endl << flush;
117   }
118
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());
124   }
125
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;
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 ( ( lastWrittenObsEquis + NSKIP != NEQUI-nequileft-NSKIP ) && Os[obsid]->isOutFileOpen() )
149     Os[obsid]->finishOutFile();
150
151   if ( activeCFile != "" ) {
152     unsigned long afilehash = configcache::hash(activeCFile);
153     Os[obsid]->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long), NEQUI-nequileft-NSKIP);
154   }
155   Os[obsid]->writeConfig(NEQUI-nequileft-NSKIP); 
156
157   lastWrittenObsEquis = NEQUI-nequileft-NSKIP;
158 }
159
160 void hypercache::readO(int obsid, bool& readnewconfig, int& nequileft) {
161   Os[obsid]->readConfig(readnewconfig, nequileft);
162
163   if ( nequileft < 0 ) {
164     unsigned long *parentconfig = (unsigned long*)Os[obsid]->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 }