-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFormulaCache.cpp
More file actions
executable file
·311 lines (254 loc) · 8.93 KB
/
FormulaCache.cpp
File metadata and controls
executable file
·311 lines (254 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "FormulaCache.h"
/**
* Available memory in bytes.
*/
size_t availableMem() {
//Implementation is platform depended
#ifdef __linux__
auto pgs = getpagesize();
auto pages = get_avphys_pages();
return (pages * pgs);
#elif __APPLE__ && __MACH__
// Get page size (in bytes)
vm_size_t pgs;
auto pgsStatus = host_page_size(mach_host_self(), &pgs);
// Get free memory (in pages) from host_statistics
vm_statistics_data_t vmstats;
mach_msg_type_number_t rSize = HOST_VM_INFO_COUNT;
auto vmstatsStatus = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t) &vmstats, &rSize);
if (pgsStatus == KERN_SUCCESS && vmstatsStatus == KERN_SUCCESS)
{
return vmstats.free_count * pgs;
} else {
std::cout << "Failed to read free memory and page size, returning 100MB instead." << std::endl;
return 100*1024*1024;
}
#elif _WIN32
// relevant documentation:
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
GlobalMemoryStatusEx(&statex);
return statex.ullAvailPhys;
#elif defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
auto pages = sysconf (_SC_AVPHYS_PAGES);
auto pgs = sysconf (_SC_PAGESIZE);
return pages * pgs;
#elif SUN_OS
// This value was the previously used value for SUN_OS.
// While rewriting this code, I kept it the same...
return 100*1024*1024;
#else
// availableMem() is yet to be implemented for this platform...
#endif
}
unsigned int CFormulaCache::oldestEntryAllowed = (unsigned int) -1;
CFormulaCache::CFormulaCache()
{
iBuckets = 900001;// =299999;
theData.resize(iBuckets,NULL);
theBucketBase.reserve(iBuckets);
theEntryBase.reserve(iBuckets*10);
scoresDivTime = 50000;
lastDivTime = 0;
if (CSolverConf::maxCacheSize == 0)
CSolverConf::maxCacheSize = availableMem() / 2;
}
bool CFormulaCache::include(CComponentId &rComp, const CRealNum &val, DTNode * dtNode)
{
#ifdef DEBUG
// if everything is correct, a new value to be cached
// should not already be stored in the cache
assert(rComp.cachedAs == NIL_ENTRY);
#endif
if (rComp.empty()) return false;
iCacheTries++;
if (memUsage >= CSolverConf::maxCacheSize) return false;
long unsigned int hV = rComp.getHashKey();
CCacheBucket &rBucket = at(clip(hV));
CacheEntryId eId = newEntry();
CCacheEntry & rEntry = entry(eId);
rBucket.push_back(eId);
rEntry.createFrom(rComp);
rEntry.hashKey = hV;
rEntry.theVal = val;
rEntry.theDTNode = dtNode;
rComp.cachedAs = eId; // save in the Comp, wwhere it was saved
rEntry.theDescendants = rComp.cachedChildren; // save the cache ids of its children
rComp.cachedChildren.clear();
adjustDescendantsFather(eId);
//BEGIN satistics
auto memU = memUsage/(10*1024*1024);
memUsage += rEntry.memSize();
if (memU < memUsage/(10*1024*1024))
{
toSTDOUT("Cache: usedMem "<< memUsage<<"Bytes\n");
}
iSumCachedCompSize += rComp.countVars();
iCachedComponents++;
if (iCachedComponents % 50000 == 0)
{
double d = iSumCachedCompSize;
d /= (double) iCachedComponents;
toSTDOUT("cachedComponents:"<< iCachedComponents<<" avg. size:"<<d<<endl);
}
//END satistics
return true;
}
bool CFormulaCache::extract(CComponentId &rComp, CRealNum &val, DTNode * dtNode)
{
long unsigned int hV = rComp.getHashKey();
unsigned int v = clip(hV);
if (!isBucketAt(v)) return false;
CCacheBucket &rBucket = *theData[v]; // the location of the considered bucket
CCacheEntry *pComp;
for (CCacheBucket::iterator it = rBucket.begin(); it != rBucket.end();it++)
{
pComp = &entry(*it);
if (hV == pComp->getHashKey() && pComp->equals(rComp))
{
val = pComp->theVal;
pComp->score++;
pComp->score+= (unsigned int)pComp->sizeVarVec();
iCacheRetrievals++;
iSumRetrieveSize += rComp.countVars();
if (iCacheRetrievals % 50000 == 0)
{
double d = iSumRetrieveSize;
d /= (double) iCacheRetrievals;
toSTDOUT("cache hits:"<< iCacheRetrievals<<" avg size:"<< d<<endl);
}
pComp->theDTNode->addParent(dtNode, true);
return true;
}
}
return false;
}
int CFormulaCache::removePollutedEntries(CacheEntryId root)
{
vector<CacheEntryId>::iterator it;// theDescendants;
CCacheBucket &rBuck = at(clip(entry(root).hashKey));
unsigned int n = 0;
for (CCacheBucket::iterator jt= rBuck.begin(); jt != rBuck.end(); jt++)
{
if (*jt == root)
{
rBuck.erase(jt);
n++;
break;
}
}
for (it = entry(root).theDescendants.begin(); it != entry(root).theDescendants.end(); it++)
{
n += removePollutedEntries(*it);
}
entry(root).clear();
return n;
}
bool CFormulaCache::deleteEntries(CDecisionStack & rDecStack)
{
vector<CCacheBucket>::iterator jt;
vector<CCacheEntry>::iterator it,itWrite;
CCacheBucket::iterator bt;
if (memUsage < (size_t) (0.85 * (double) CSolverConf::maxCacheSize)) return false;
// first : go through the EntryBase and mark the entries to be deleted as deleted (i.e. EMPTY
for (it = beginEntries(); it != endEntries(); it++)
{
if (it->score <= minScoreBound)
{
deleteFromDescendantsTree(toCacheEntryId(it));
it->clear();
}
}
// then go through the BucketBase and rease all Links to empty entries
for (jt = theBucketBase.begin(); jt != theBucketBase.end(); jt++)
{
for (bt = jt->end()-1; bt != jt->begin()-1; bt--)
{
if (entry(*bt).empty()) bt = jt->erase(bt);
}
}
// now: go through the decisionStack. and delete all Links to empty entries
revalidateCacheLinksIn(rDecStack.getAllCompStack());
// finally: truly erase the empty entries, but keep the descendants tree consistent
size_t newSZ = 0;
long int SumNumOfVars= 0;
CacheEntryId idOld,idNew;
itWrite = beginEntries();
for (it = beginEntries(); it != endEntries(); it++)
{
if (!it->empty())
{
if (it != itWrite)
{
*itWrite = *it;
idNew = toCacheEntryId(itWrite);
idOld = toCacheEntryId(it);
at(clip(itWrite->getHashKey())).substituteIds(idOld,idNew);
substituteInDescTree(idOld,idNew);
substituteCacheLinksIn(rDecStack.getAllCompStack(),idOld,idNew);
}
itWrite++;
//theEntryBase.pop_back();
newSZ += itWrite->memSize();
SumNumOfVars += itWrite->sizeVarVec();
}
}
theEntryBase.erase(itWrite,theEntryBase.end());
iCachedComponents = theEntryBase.size();
iSumCachedCompSize = SumNumOfVars*sizeof(unsigned int)*8 / CCacheEntry::bitsPerVar();
memUsage = newSZ;
toSTDOUT("Cache cleaned: "<<iCachedComponents<<" Components ("<< (memUsage>>10)<< " KB remain"<<endl);
if (scoresDivTime == 0) scoresDivTime = 1;
size_t dbound = CSolverConf::maxCacheSize >> 1; // = 0.5 * maxCacheSize
if (memUsage < dbound)
{
minScoreBound/= 2;
if (memUsage < (dbound >> 1)) scoresDivTime *= 2;
}
else if (memUsage > dbound)
{
minScoreBound <<= 1;
minScoreBound++;
scoresDivTime /= 2;
if (scoresDivTime < 50000) scoresDivTime = 50000;
}
toDEBUGOUT("setting scoresDivTime: "<<scoresDivTime<<endl);
toDEBUGOUT("setting minScoreBound: "<<minScoreBound<<endl);
return true;
}
void CFormulaCache::revalidateCacheLinksIn(const vector<CComponentId *> &rComps)
{
vector<CComponentId *>::const_iterator it;
vector<unsigned int>::iterator jt;
for (it = rComps.begin(); it !=rComps.end(); it++)
{
if (!isEntry((*it)->cachedAs)) (*it)->cachedAs = 0;
if (isEntry((*it)->cachedAs) && entry((*it)->cachedAs).empty()) (*it)->cachedAs = 0;
for (jt = (*it)->cachedChildren.end()-1; jt != (*it)->cachedChildren.begin()-1; jt--)
{
if ((!isEntry(*jt)) || entry(*jt).empty())
{
toDEBUGOUT("_E");
jt =(*it)->cachedChildren.erase(jt);
}
}
}
}
void CFormulaCache::substituteCacheLinksIn(const vector<CComponentId *> &rComps, CacheEntryId idOld, CacheEntryId idNew)
{
vector<CComponentId *>::const_iterator it;
vector<unsigned int>::iterator jt;
for (it = rComps.begin(); it !=rComps.end(); it++)
{
if ((*it)->cachedAs == idOld) (*it)->cachedAs = idNew;
for (jt = (*it)->cachedChildren.begin(); jt != (*it)->cachedChildren.end(); jt++)
{
if (*jt == idOld)
{
toDEBUGOUT("_D");
*jt = idNew;
}
}
}
}