]> git.treefish.org Git - phys/latlib.git/blob - configcache.cpp
disabled warnings for unknown command line options.
[phys/latlib.git] / configcache.cpp
1 #include "configcache.h"
2
3 #include <stdlib.h>
4 #include <iostream>
5 #include <time.h>
6 #include <dirent.h>
7
8 #define HEADER_READOK   0
9 #define HEADER_READERR  1
10 #define HEADER_READLAST 2
11
12 configcache::configcache(const string& cacheid, const int& nequi, const int& nskip, const string& datadir, char **configmem, const int& configMemSize, const int& cachemode,
13                          ostream *_log){
14   log = _log;
15
16   NEQUI = nequi;
17   NSKIP = nskip;
18   DATADIR = datadir;
19   CACHEID = cacheid;
20
21   configMem = (char*)malloc(configMemSize);
22   tmpConfig = (char*)malloc(configMemSize);
23
24   *configmem = configMem;
25   configSize = configMemSize;
26
27   outBuffer = NULL;
28   inBuffer = NULL;
29
30   MODE = cachemode;
31
32   refetchDataFiles = false;
33 }
34
35 string configcache::getFileId(const bool& shortid)
36 {
37   stringstream fileid;
38
39   if(!shortid) fileid << CACHEID << "_" << NEQUI << "_" << NSKIP;
40   for(int ipara=0; ipara<Paras.size(); ipara++)
41     fileid << "_" << Paras[ipara].id << Paras[ipara].val;
42
43   return fileid.str();
44 }
45
46 void configcache::fetchDataFiles()
47 {
48   struct dirent *de=NULL;
49   DIR *d=NULL;
50   static infiledesc filedesc;
51   
52   d=opendir(DATADIR.c_str());
53   if(d != NULL){
54     while(de = readdir(d)){
55       string filename = de->d_name;
56       if(isValidInFile(filename, &filedesc)) 
57         {
58           inFiles.push_back(filedesc);
59         }
60     }
61   }
62 }
63
64 bool configcache::isValidInFile(const string& infile, infiledesc *filedesc)
65 {
66   char *inchar, *inParts;
67   string truncIn, truncOut;
68
69   filedesc->filename = infile;
70   filedesc->doVirtualEquilibration = false;
71
72   if( infile.size() < 4 ) return false;
73
74   if( infile.substr(infile.size()-4) == ".dat" )
75     filedesc->extended = false;
76   else if( infile.substr(infile.size()-4) == "edat" )
77     filedesc->extended = true;
78   else
79     return false;
80
81   inchar = new char [infile.size()+1];
82   strcpy (inchar, infile.c_str());
83   
84   inParts = strtok( inchar, "_" );
85   for(int iPart=0; inParts!=NULL; iPart++)
86     {
87       if( iPart>3 ) { truncIn += "_"; truncIn += inParts; }
88
89       switch(iPart)
90         {
91         case 1: if(inParts != CACHEID) return false; break;
92         case 2: 
93           if (atoi(inParts) > NEQUI) 
94             return false; 
95           else if (atoi(inParts) < NEQUI)
96             filedesc->doVirtualEquilibration = true;
97           filedesc->nequi = atoi(inParts);
98           break;
99         case 3: 
100           if(atoi(inParts) != NSKIP) 
101             return false;
102           filedesc->nskip = atoi(inParts);
103           break;
104         }
105       inParts = strtok( NULL, "_");
106     }
107   truncIn = truncIn.substr(0, truncIn.size()-4);
108
109   delete[] inchar;
110
111   if( truncIn.find( getFileId(true) + "_" ) == string::npos ) return false;
112
113   return true;
114 }
115
116 int configcache::readHeader()
117 {
118   long unsigned int headersize;
119   
120   if( readDataToMem((char *)&headersize, sizeof(long unsigned int)) == sizeof(long unsigned int) && inFile.is_open() ) {
121     if ( headersize == 0 )
122       return HEADER_READLAST;
123
124     pair<unsigned long, void *> newHeader;
125
126     if( readDataToMem((char *)&newHeader.first, sizeof(unsigned long)) == sizeof(unsigned long) && inFile.is_open() ) {
127       newHeader.second = malloc(headersize);
128
129       if( readDataToMem((char *)newHeader.second, headersize) == headersize && inFile.is_open() ) {
130         headerStore.push_back(newHeader);
131         return HEADER_READOK;
132       }
133       else {
134         if(log) *log << "CCACHE: Could not read heade-data! Closing dat-file: " << openFileDesc.filename << endl << flush;
135         inFile.close();
136         return HEADER_READERR;
137       }
138     }
139     else {
140       if(log) *log << "CCACHE: Could not read headerid-hash! Closing dat-file: " << openFileDesc.filename << endl << flush;
141       inFile.close();
142       return HEADER_READERR;
143     }
144   }
145   else {
146     if(log) *log << "CCACHE: Could not read header size. Closing dat-file: " << openFileDesc.filename << endl << flush;
147     inFile.close();
148     return HEADER_READERR;
149   }
150 }
151
152 bool configcache::readAllHeaders()
153 {
154   int readHeaderStatus;
155
156   deleteHeaderStore();
157   
158   do {
159     readHeaderStatus = readHeader();
160   }
161   while ( readHeaderStatus == HEADER_READOK );
162
163   if ( readHeaderStatus == HEADER_READLAST ) return true;
164   else if ( readHeaderStatus == HEADER_READERR ) return false;
165 }
166
167 void * configcache::getHeader(const string& headerid) {
168   for (vector< pair<unsigned long, void *> >::iterator headerStoreIt = headerStore.begin(); headerStoreIt != headerStore.end(); ++headerStoreIt)
169     if ( headerStoreIt->first == hash(headerid) )
170       return headerStoreIt->second;
171   
172   return NULL;
173 }
174
175 /* returns number of equilibration-steps left */
176 int configcache::readConfig(vector<unsigned long> *excludeFileHashes)
177 {
178   int nequileft = NEQUI;
179
180   if(DATADIR == "" || MODE == CACHE_MODE_DISABLED) return nequileft;
181
182   if(refetchDataFiles){
183     refetchDataFiles = false;
184     fetchDataFiles();
185   }
186
187   while(true)
188     {
189       vector<infiledesc>::iterator inFileIt = getNextInfile(excludeFileHashes);
190
191       if( (!inFile.is_open()) && inFileIt == inFiles.end() ) return nequileft;
192
193       while( (!inFile.is_open()) && inFiles.size() > 0 ) {
194         openFileDesc = *inFileIt;
195
196         if(log) *log << "CCACHE: Opening dat-file: " << inFileIt->filename << endl << flush;
197         inFile.open( (DATADIR + "/" + inFileIt->filename).c_str(), std::ios::binary );
198         
199         inFiles.erase(inFileIt);
200         
201         if( !inFile.is_open() ) continue;
202
203         inBuffer = new boost::iostreams::filtering_istreambuf;
204         inBuffer->push( boost::iostreams::bzip2_decompressor() );
205         inBuffer->push(inFile);
206       }
207
208       if( inFile.is_open() ) 
209         {
210           if (openFileDesc.doVirtualEquilibration) {
211             if(log) *log << "CCACHE: Trying virtual equilibration." << endl << flush;
212             openFileDesc.doVirtualEquilibration = false;
213             for (int iskip=0; iskip < (NEQUI-openFileDesc.nequi)/openFileDesc.nskip; iskip++) {
214               if( readFullBlock(tmpConfig, configSize) != configSize || ! inFile.is_open() )
215                 break;
216               else if ( (NEQUI-openFileDesc.nequi) - (iskip+1)*openFileDesc.nskip < nequileft ) {
217                 memcpy(configMem, tmpConfig, configSize);
218                 nequileft = (NEQUI-openFileDesc.nequi) - (iskip+1)*openFileDesc.nskip;
219               }
220             }
221           }
222
223           if( readFullBlock(tmpConfig, configSize) == configSize && inFile.is_open() )
224             {
225               memcpy(configMem, tmpConfig, configSize);
226               return -1;
227             }
228           else {
229             if(log) *log << "CCACHE: Could not read configuration. Closing dat-file: " << openFileDesc.filename << endl << flush;
230             inFile.close();
231           }
232         }
233     }
234 }
235
236 void configcache::openOutFile()
237 {
238   time_t secstamp = time(NULL);
239
240   outFileName.str("");
241   outFileName << DATADIR << "/" << secstamp << "_" << getFileId() << "_.edat.tmp";
242
243   outFile.open( outFileName.str().c_str(), std::ios::binary );
244
245   outBuffer = new boost::iostreams::filtering_ostreambuf;
246   outBuffer->push(boost::iostreams::bzip2_compressor());
247   outBuffer->push(outFile);
248 }
249
250 void configcache::writeHeader(const string& headerid, const char *header, long unsigned int size) {
251   unsigned long headeridhash;
252
253   if( DATADIR == "" || MODE < 2 ) return;
254
255   if(!outFile.is_open())
256     openOutFile();
257
258   headeridhash = hash(headerid);
259
260   boost::iostreams::write(*outBuffer, (char*)&size, sizeof(long unsigned int));
261   boost::iostreams::write(*outBuffer, (char*)&headeridhash, sizeof(unsigned long));
262   boost::iostreams::write(*outBuffer, header, size);
263 }
264
265 void configcache::writeConfig()
266 {
267   long unsigned int zeroheader=0;
268
269   if ( DATADIR == "" || MODE < 2 ) return;
270
271   if ( ! outFile.is_open() )
272     openOutFile();
273   
274   boost::iostreams::write(*outBuffer, (char*)&zeroheader, sizeof(long unsigned int));
275
276   boost::iostreams::write(*outBuffer, configMem, configSize);
277 }
278
279 void configcache::addPara(const string& parid, const double& val){
280   parameter newPara;
281   newPara.id = parid;
282   newPara.val = val;
283   Paras.push_back(newPara);
284 }
285
286 int configcache::getParIndex(const string& parid){
287   for(int ipara=0; ipara<Paras.size(); ipara++)
288     if(Paras[ipara].id == parid) return ipara;
289 }
290
291 void configcache::setPara(const string& parid, const double& value){
292   Paras[getParIndex(parid)].val = value;
293   finishOutFile();
294   if(inBuffer != NULL) { delete inBuffer; inBuffer=NULL; } 
295   inFile.close();
296   inFiles.clear();
297
298   refetchDataFiles = true;
299 }
300
301 configcache::~configcache()
302 {
303   finishOutFile();
304   delete inBuffer;
305   inBuffer = NULL;
306 }
307
308 void configcache::finishOutFile()
309 {
310   if( outBuffer != NULL )
311     {
312       delete outBuffer;
313       outBuffer = NULL;
314     }
315
316   if( outFile.is_open() )
317     {
318       outFile.close();
319       rename( outFileName.str().c_str(), outFileName.str().substr(0, outFileName.str().size()-4).c_str() );
320     }
321 }
322
323 int configcache::readFullBlock(char *tmpData, long unsigned int dataSize)
324 {
325   /* try to read header */
326   if ( openFileDesc.extended )
327     if ( ! readAllHeaders() ) 
328       return -1;
329
330   /* read data */
331   return readDataToMem(tmpData, dataSize);
332 }
333
334 int configcache::readDataToMem(char *tmpData, long unsigned int dataSize)
335 {
336   int readturn = -1;
337
338   if ( dataSize == 0 ) return 0;
339
340   try { readturn = boost::iostreams::read(*inBuffer, tmpData, dataSize); }
341   catch(boost::iostreams::bzip2_error& error) { 
342     if(log) *log << "CCACHE: Caught bzip2 exception with error code: " << error.error() << endl << flush;
343     inFile.close();
344   } 
345   catch (std::exception const& ex) {
346     if(log) *log << "CCACHE: Caught exception: " << ex.what() << endl << flush;
347     inFile.close();
348   }
349   catch( ... ) {
350     if(log) *log << "CCACHE: Caught unknown exception while reading." << endl << flush;
351     inFile.close();
352   }
353
354   return readturn;
355 }
356
357 unsigned long configcache::hash(const string& str)
358 {
359   unsigned long hash = 5381;
360
361   for(string::const_iterator it=str.begin();it!=str.end();it++) 
362     hash = ((hash << 5) + hash) + *it; /* hash * 33 + character */
363
364   return hash;
365 }
366
367 void configcache::deleteHeaderStore()
368 {
369   while ( headerStore.size() > 0 ) {
370     free(headerStore.back().second);
371     headerStore.pop_back();
372   }
373 }
374
375 vector<infiledesc>::iterator configcache::getNextInfile(vector<unsigned long> *excludeFileHashes) {
376   for (vector<infiledesc>::iterator init = inFiles.begin(); init != inFiles.end(); ++init) {
377     if (excludeFileHashes != NULL) {
378       bool excludethisfile = false;
379
380       for (vector<unsigned long>::iterator exit = excludeFileHashes->begin(); exit != excludeFileHashes->end(); ++exit)
381         if ( *exit == hash(init->filename) ) {
382           excludethisfile = true;
383           break;
384         }
385
386       if (excludethisfile)
387         continue;
388     }
389     return init;
390   }
391   return inFiles.end();
392 }