forked from preda/gpuowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaver.cpp
301 lines (250 loc) · 7.52 KB
/
Saver.cpp
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
// GpuOwl Mersenne primality tester; Copyright (C) Mihai Preda.
#include "Saver.h"
#include "File.h"
#include "Blake2.h"
#include "Args.h"
#include <filesystem>
#include <functional>
#include <ios>
#include <cassert>
#include <cinttypes>
namespace fs = std::filesystem;
namespace {
u32 nWords(u32 E) { return (E - 1) / 32 + 1; }
error_code& noThrow() {
static error_code dummy;
return dummy;
}
}
vector<u32> Saver::listIterations(const string& prefix, const string& ext) {
vector<u32> ret;
if (!fs::exists(base)) { fs::create_directory(base); }
for (auto entry : fs::directory_iterator(base)) {
if (entry.is_regular_file()) {
string name = entry.path().filename().string();
u32 dot = name.find('.');
if (name.size() >= prefix.size() && name.substr(0, prefix.size()) == prefix
&& dot != string::npos && name.substr(dot) == ext) {
assert(dot > prefix.size());
size_t end = 0;
u32 k = std::stoul(name.substr(prefix.size(), dot), &end);
if (end != dot - prefix.size()) {
log("Savefile ignored: '%s'\n", name.c_str());
} else {
ret.push_back(k);
}
}
}
}
return ret;
}
vector<u32> Saver::listIterations() {
return listIterations(to_string(E) + '-', ".prp");
}
void Saver::cleanup(u32 E, const Args& args) {
if (args.clean) {
fs::path here = fs::current_path();
fs::path trash = here / "trashbin";
fs::remove_all(trash, noThrow());
fs::create_directory(trash, noThrow());
fs::rename(here / to_string(E), trash / to_string(E), noThrow());
}
// fs::remove_all(base, noThrow); // redundant
}
float Saver::value(u32 k) {
assert(k > 0);
u32 dist = (k < E) ? (E - k) : 1;
u32 nice = 1;
while (k % 2 == 0) {
k /= 2;
nice *= 2;
}
while (k % 5 == 0) {
k /= 5;
nice *= 5;
}
return nice / float(dist);
}
Saver::Saver(u32 E, u32 nKeep, u32 b1, u32 startFrom) : E{E}, nKeep{max(nKeep, 5u)}, b1{b1} {
scan(startFrom);
}
void Saver::scan(u32 upToK) {
lastK = 0;
minValPRP = {};
vector<u32> iterations = listIterations();
for (u32 k : iterations) {
if (k <= upToK) {
minValPRP.push({value(k), k});
lastK = max(lastK, k);
}
}
}
void Saver::deleteBadSavefiles(u32 kBad, u32 currentK) {
assert(kBad <= currentK);
vector<u32> iterations = listIterations();
for (u32 k : iterations) {
if (k >= kBad && k <= currentK) {
log("Deleting bad savefile @ %u\n", k);
del(k);
}
}
scan(kBad);
}
void Saver::del(u32 k) {
// log("Note: deleting savefile %u\n", k);
fs::remove(pathPRP(k), noThrow());
fs::remove(pathP1(k), noThrow());
}
void Saver::savedPRP(u32 k) {
assert(k >= lastK);
lastK = k;
while (minValPRP.size() >= nKeep) {
auto kDel = minValPRP.top().second;
minValPRP.pop();
del(kDel);
}
minValPRP.push({value(k), k});
}
namespace {
vector<u32> makeVect(u32 size, u32 elem0) {
vector<u32> v(size);
v[0] = elem0;
return v;
}
}
// --- PRP ---
PRPState Saver::loadPRP(u32 iniBlockSize) {
if (lastK == 0) {
log("PRP starting from beginning\n");
u32 blockSize = iniBlockSize ? iniBlockSize : 400;
return {0, blockSize, 3, makeVect(nWords(E), 1), 0};
} else {
return loadPRPAux(lastK);
}
}
PRPState Saver::loadPRPAux(u32 k) {
assert(k > 0);
fs::path path = pathPRP(k);
File fi = File::openReadThrow(path);
string header = fi.readLine();
u32 fileE, fileK, blockSize, nErrors, crc;
u64 res64;
vector<u32> check;
u32 b1, nBits, start, nextK;
if (sscanf(header.c_str(), PRP_v12, &fileE, &fileK, &blockSize, &res64, &nErrors, &crc) == 6) {
assert(E == fileE && k == fileK);
check = fi.readWithCRC<u32>(nWords(E), crc);
} else if (sscanf(header.c_str(), PRP_v10, &fileE, &fileK, &blockSize, &res64, &nErrors) == 5
|| sscanf(header.c_str(), PRP_v11, &fileE, &fileK, &blockSize, &res64, &nErrors, &b1, &nBits, &start, &nextK, &crc) == 10) {
assert(E == fileE && k == fileK);
check = fi.read<u32>(nWords(E));
} else {
log("In file '%s': bad header '%s'\n", fi.name.c_str(), header.c_str());
throw "bad savefile";
}
return {k, blockSize, res64, check, nErrors};
}
void Saver::savePRP(const PRPState& state) {
assert(state.check.size() == nWords(E));
u32 k = state.k;
fs::path path = pathPRP(k);
{
File fo = File::openWrite(path);
if (fo.printf(PRP_v12, E, k, state.blockSize, state.res64, state.nErrors, crc32(state.check)) <= 0) {
throw(ios_base::failure("can't write header"));
}
fo.write(state.check);
}
loadPRPAux(k);
savedPRP(k);
}
// --- P1 ---
P1State Saver::loadP1(u32 k) {
fs::path path = pathP1(k);
File fi = File::openReadThrow(path);
string header = fi.readLine();
u32 fileE, fileB1, fileK, nextK, crc;
if (sscanf(header.c_str(), P1_v2, &fileE, &fileB1, &fileK, &nextK, &crc) != 5) {
log("In file '%s': bad header '%s'\n", fi.name.c_str(), header.c_str());
throw "bad savefile";
}
assert(fileE == E && fileB1 == b1 && fileK == k);
return {nextK, fi.readWithCRC<u32>(nWords(E), crc)};
}
void Saver::saveP1(u32 k, const P1State& state) {
auto& [nextK, data] = state;
assert(data.size() == nWords(E));
assert(nextK == 0 || nextK >= k);
{
File fo = File::openWrite(pathP1(k));
if (fo.printf(P1_v2, E, b1, k, nextK, crc32(data)) <= 0) {
throw(ios_base::failure("can't write header"));
}
fo.write(data);
}
loadP1(k);
}
// --- P1Final ---
vector<u32> Saver::loadP1Final() {
fs::path path = pathP1Final();
File fi = File::openReadThrow(path);
string header = fi.readLine();
u32 fileE, fileB1, crc;
if (sscanf(header.c_str(), P1Final_v1, &fileE, &fileB1, &crc) != 3) {
log("In file '%s': bad header '%s'\n", fi.name.c_str(), header.c_str());
throw "bad savefile";
}
assert(fileE == E && fileB1 == b1);
return fi.readWithCRC<u32>(nWords(E), crc);
}
void Saver::saveP1Final(const vector<u32>& data) {
assert(data.size() == nWords(E));
{
File fo = File::openWrite(pathP1Final());
if (fo.printf(P1Final_v1, E, b1, crc32(data)) <= 0) {
throw(ios_base::failure("can't write header"));
}
fo.write(data);
}
loadP1Final();
}
// --- P2 ---
u32 Saver::loadP2(u32 b2, u32 D, u32 nBuf) {
fs::path path = pathP2();
File fi = File::openRead(path);
if (!fi) {
return 0;
} else {
string header = fi.readLine();
u32 fileE, fileB1, fileB2, fileD, fileNBuf, nextBlock;
if (sscanf(header.c_str(), P2_v2, &fileE, &fileB1, &fileB2) == 3) {
assert(fileE == E && fileB1 == b1);
if (fileB2 >= b2) {
return u32(-1); // P2 finished.
} else {
log("Finish P2 with old savefile format before upgrading\n");
throw("P2 savefile version upgrade");
}
}
if (sscanf(header.c_str(), P2_v3, &fileE, &fileB1, &fileB2, &fileD, &fileNBuf, &nextBlock) != 6) {
log("In file '%s' wrong header '%s'\n", fi.name.c_str(), header.c_str());
throw "bad savefile";
}
assert(fileE == E && fileB1 == b1);
if (nextBlock == u32(-1)) {
// if P2 already finished, don't check exact match.
return nextBlock;
}
if (fileB2 != b2 || fileD != D || fileNBuf != nBuf) {
log("P2 savefile has: B2=%u, D=%u, nBuf=%u vs. B2=%u, D=%u, nBuf=%u\n", fileB2, fileD, fileNBuf, b2, D, nBuf);
throw("P2 savefile mismatch");
}
return nextBlock;
}
}
void Saver::saveP2(u32 b2, u32 D, u32 nBuf, u32 nextBlock) {
File fo = File::openWrite(pathP2());
if (fo.printf(P2_v3, E, b1, b2, D, nBuf, nextBlock) <= 0) {
throw(ios_base::failure("can't write header"));
}
}