]> git.treefish.org Git - phys/latlib.git/blob - hypercache.cpp
added support for using excluded config-files for equilibration.
[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
11 void hypercache::initCache(configcache **cache,
12                            const string& cacheid, const int& nequi, const int& nskip, const string& datadir, 
13                            char **configmem, const int& configMemSize, const int& cachemode, ostream* _log) {
14   if ( _log != NULL )
15     log = _log;
16   
17   *cache = new configcache(cacheid, nequi, nskip, datadir, configmem, configMemSize, cachemode, log);
18   
19   for (vector<para>::iterator parit=delayedParaAdd.begin(); parit != delayedParaAdd.end(); ++parit)
20     (*cache)->addPara(parit->parid, parit->val);
21
22   for (vector<para>::iterator parit=delayedParaSet.begin(); parit != delayedParaSet.end(); ++parit)
23     (*cache)->setPara(parit->parid, parit->val);
24 }
25
26 void hypercache::addPara(const string& parid, const double& val) {
27   para newpara; 
28   newpara.parid = parid;
29   newpara.val = val;
30   delayedParaAdd.push_back(newpara);
31
32   if (C != NULL)
33     C->addPara(parid, val);
34
35   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
36     (*osit)->addPara(parid, val);
37 }
38
39 void hypercache::setPara(const string& parid, const double& val) {
40   para newpara; 
41   newpara.parid = parid;
42   newpara.val = val;
43   delayedParaSet.push_back(newpara);
44
45   if (C != NULL)
46     C->setPara(parid, val);
47   
48   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
49     (*osit)->setPara(parid, val);
50 }
51
52 void hypercache::finalize() {
53   for (vector<configcache*>::iterator osit = Os.begin(); osit != Os.end(); ++osit)
54     delete *osit;
55   
56   delete C;
57 }
58
59 string hypercache::fileOfPath(const string& dressedfile) {
60   return dressedfile.substr(dressedfile.find_last_of("\\/")+1);
61 }
62
63 int hypercache::readC() { 
64   int readret;
65
66   readret = C->readConfig(&parentConfigs);
67
68   if ( readret == -1 )
69     activeCFile = fileOfPath(C->getInFileName());
70   else
71     activeCFile = "";
72
73   return readret; 
74 }
75
76 int hypercache::read1CForEqui() {
77   int readret = C->readConfig(NULL);
78   C->closeInFile();
79   return readret;
80 }
81
82 void hypercache::writeC() {
83   C->writeConfig();
84   activeCFile = fileOfPath( C->getOutFileName().substr( 0, C->getOutFileName().length()-4 ) );
85 }
86
87 void hypercache::writeO(int obsid) {
88   if ( activeCFile != "" ) {
89     unsigned long afilehash = configcache::hash(activeCFile);
90     Os[obsid]->writeHeader("concurrent_cfile", (char*)(&afilehash), sizeof(unsigned long));
91   }
92   Os[obsid]->writeConfig(); 
93 }
94
95 int hypercache::readO(int obsid) {
96   int readret;
97
98   readret = Os[obsid]->readConfig();
99
100   if ( readret == -1 ) {
101     unsigned long *parentconfig = (unsigned long*)Os[obsid]->getHeader("concurrent_cfile");
102     if ( parentconfig != NULL )
103       addParentConfig(parentconfig);
104   }
105   
106   return readret;
107 }
108
109 void hypercache::addParentConfig(const unsigned long *parentconfig) {
110   for (vector<unsigned long>::iterator parit = parentConfigs.begin(); parit != parentConfigs.end(); ++parit)
111     if ( *parit == *parentconfig )
112       return;
113
114   parentConfigs.push_back(*parentconfig);
115 }