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
|
// compile with:
// cl -EHsc -Ox zeta.cxx
#if _MSC_VER >= 1400
#define _CRT_SECURE_NO_DEPRECATE
#endif
#include <stdio.h>
#include <float.h>
#include <map>
#include <set>
using namespace std;
const float INVALID_CACHE_VALUE = FLT_MAX;
class Cache : private map <int, map <int, float>* >
{
private:
typedef map<int, float> innercont_t;
unsigned long m_nHits;
unsigned long m_nMisses;
bool m_bAltered;
public:
Cache ()
: m_nHits (0),
m_nMisses (0),
m_bAltered (false)
{}
virtual ~Cache ()
{
// free memory
const_iterator cit = begin ();
for (; cit != end (); ++cit)
delete (*cit).second;
}
// read cache file
void read (const char *pFilename)
{
FILE* f = fopen (pFilename, "rb");
if (f)
{
int m, n;
unsigned long nSize;
float v;
innercont_t* pCont;
while (fread (&m, sizeof (int), 1, f) == 1)
{
// read number of mappings
fread (&nSize, sizeof (unsigned long), 1, f);
// create new map for current m
pCont = new innercont_t ();
insert (make_pair (m, pCont));
// read all mappings
for (unsigned long i = 0; i < nSize; ++i)
{
fread (&n, sizeof (int), 1, f);
fread (&v, sizeof (float), 1, f);
// add mapping
pCont->insert (make_pair (n, v));
}
}
fclose (f);
}
}
// write cache file
void write (const char *pFilename)
{
// save cache only if something was changed
if (m_bAltered)
{
FILE* f = fopen (pFilename, "wb");
if (f == NULL)
{
fprintf (stderr, "Failed to open cache file %s for writing\n", pFilename);
return;
}
for (const_iterator cit = begin (); cit != end (); ++cit)
{
const int m = (*cit).first;
innercont_t* pCont = (*cit).second;
const unsigned long nSize = pCont->size ();
fwrite (&m, sizeof (int), 1, f);
fwrite (&nSize, sizeof (unsigned long), 1, f);
for (innercont_t::const_iterator cit2 = pCont->begin (); cit2 != pCont->end (); ++cit2)
{
const int n = (*cit2).first;
const float v = (*cit2).second;
fwrite (&n, sizeof (int), 1, f);
fwrite (&v, sizeof (float), 1, f);
}
}
fclose (f);
}
}
// returns INVALID_CACHE_VALUE if not found
float getCachedValue (const int m, const int n)
{
// is m contained?
const_iterator cit = find (m);
if (cit != end ())
{
// yes -> is n contained?
innercont_t* pCont = (*cit).second;
innercont_t::const_iterator cit2 = pCont->find (n);
if (cit2 != pCont->end ())
{
++m_nHits;
return (*cit2).second;
}
}
++m_nMisses;
return INVALID_CACHE_VALUE;
}
void addToCache (const int m, const int n, const float aValue)
{
// get container mapping of m
innercont_t* pCont;
const_iterator cit = find (m);
if (cit != end ())
pCont = (*cit).second;
else
{
pCont = new innercont_t ();
insert (make_pair (m, pCont));
}
// add to inner cont
pCont->insert (make_pair (n, aValue));
m_bAltered = true;
}
unsigned long getHits () const
{
return m_nHits;
}
unsigned long getMisses () const
{
return m_nMisses;
}
};
// global variables for the cache
Cache g_aCacheZeta0, g_aCacheZeta1;
float zeta_0(const int m, const int n)
{
// check at the beginning - faster than cache lookup
if (m == 0 || n == 0)
return 0;
// query cache
float ret = g_aCacheZeta0.getCachedValue (m, n);
if (ret == INVALID_CACHE_VALUE)
{
// calculate
const float epsilon=1;
const float delta=6;
const float epsilonmn = epsilon * m * n;
const float deltan = delta * n;
ret = 1+((deltan*zeta_0(m,n-1)+epsilonmn*zeta_0(m-1,n+1))/(deltan+epsilonmn));
// add to cache
g_aCacheZeta0.addToCache (m, n, ret);
}
return ret;
}
float zeta_1 (const int m, const int n)
{
// check at the beginning - faster than cache lookup
if(m == 0 || n == 0)
return 0;
// query cache
float ret = g_aCacheZeta1.getCachedValue (m, n);
if (ret == INVALID_CACHE_VALUE)
{
// calculate
const float M=6;
const float rho=6;
const float Mn = M * n;
const float mn = m * n;
ret = ((M/rho)+Mn+mn+M*zeta_0(m+1,n)+Mn*zeta_1(m,n-1)+mn*zeta_1(m-1,n+1)-M*zeta_0(m,n))/(Mn+mn);
// add to cache
g_aCacheZeta1.addToCache (m, n, ret);
}
return ret;
}
int main()
{
printf ("Reading cache\n");
g_aCacheZeta0.read ("zeta0");
g_aCacheZeta1.read ("zeta1");
printf ("Calculating\n");
int m = 1000;
int n = 1000;
printf("%f\n",zeta_0(m,n));
printf("%f\n",zeta_1(m,n));
// print cache stats
printf ("Cache hits zeta_0: %lu\n", g_aCacheZeta0.getHits ());
printf ("Cache misses zeta_0: %lu\n", g_aCacheZeta0.getMisses ());
printf ("Cache hits zeta_1: %lu\n", g_aCacheZeta1.getHits ());
printf ("Cache misses zeta_1: %lu\n", g_aCacheZeta1.getMisses ());
printf ("Writing cache\n");
g_aCacheZeta0.write ("zeta0");
g_aCacheZeta1.write ("zeta1");
printf ("Cleaning up\n");
return 0;
}
|