Skip to content

Commit

Permalink
Savefiles PRP/LL v13 including labels and elapsed-time. LL_DC=106 in …
Browse files Browse the repository at this point in the history
…primenet.py
  • Loading branch information
preda committed Aug 1, 2024
1 parent 4bd667d commit 3c66373
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 77 deletions.
37 changes: 24 additions & 13 deletions src/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,28 @@

namespace fs = std::filesystem;

struct CRCError {
std::string name;
};

struct ReadError {
std::string name;
};

struct WriteError {
std::string name;
};

class File {
FILE* f = nullptr;
const bool readOnly;

File(const fs::path &path, const string& mode, bool throwOnError);

bool readNoThrow(void* data, u32 nBytes) { return fread(data, nBytes, 1, get()); }
bool readNoThrow(void* data, u32 nBytes) const { return fread(data, nBytes, 1, get()); }

void read(void* data, u32 nBytes) {
if (!readNoThrow(data, nBytes)) { throw(std::ios_base::failure(name + ": can't read")); }
void read(void* data, u32 nBytes) const {
if (!readNoThrow(data, nBytes)) { throw ReadError{name}; }
}

void datasync() {
Expand Down Expand Up @@ -104,14 +116,13 @@ class File {
void write(const T& x) const { write(&x, sizeof(T)); }

void write(const void* data, u32 nBytes) const {
if (!fwrite(data, nBytes, 1, get())) { throw(std::ios_base::failure((name + ": can't write data").c_str())); }
if (!fwrite(data, nBytes, 1, get())) { throw WriteError{name}; }
}

void seek(long offset, int whence = SEEK_SET) {
int ret = fseek(get(), offset, whence);
if (ret) {
throw(std::ios_base::failure(("fseek: "s + to_string(ret)).c_str()));
}
if (ret) { throw ReadError{name}; }
// throw(std::ios_base::failure(("fseek: "s + to_string(ret)).c_str()));
}

void flush() { fflush(get()); }
Expand Down Expand Up @@ -173,7 +184,7 @@ class File {
string line = buf;
if (line.empty() || line.back() != '\n') {
log("%s : line \"%s\" does not end with a newline\n", name.c_str(), line.c_str());
throw "lines must end with newline";
throw ReadError{name};
}
return line;
}
Expand All @@ -185,31 +196,31 @@ class File {
}

template<typename T>
std::vector<T> read(u32 nWords) {
std::vector<T> read(u32 nWords) const {
vector<T> ret;
ret.resize(nWords);
read(ret.data(), nWords * sizeof(T));
return ret;
}

template<typename T>
std::vector<T> readChecked(u32 nWords) {
std::vector<T> readChecked(u32 nWords) const {
u32 expectedCRC = read<u32>(1)[0];
return readWithCRC<T>(nWords, expectedCRC);
}

template<typename T>
void writeChecked(const vector<T>& data) {
void writeChecked(const vector<T>& data) const {
write(u32(crc32(data)));
write(data);
}

template<typename T>
std::vector<T> readWithCRC(u32 nWords, u32 crc) {
std::vector<T> readWithCRC(u32 nWords, u32 crc) const {
auto data = read<T>(nWords);
if (crc != crc32(data)) {
log("File '%s' : CRC: expected %u, actual %u\n", name.c_str(), crc, crc32(data));
throw "CRC";
throw CRCError{name};
}
return data;
}
Expand Down
49 changes: 34 additions & 15 deletions src/Gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -955,24 +955,25 @@ fs::path Gpu::saveProof(const Args& args, const ProofSet& proofSet) {
throw "bad proof generation";
}

std::tuple<u32, u32, u32> Gpu::loadPRP(Saver<PRPState>& saver) {
PRPState Gpu::loadPRP(Saver<PRPState>& saver) {
// We do at most 4 attempts: the most recent savefile twice, plus two more.
for (int nTries = 0; nTries < 4; ++nTries) {
PRPState loaded = saver.load();
writeState(loaded.check, loaded.blockSize);
PRPState state = saver.load();
writeState(state.check, state.blockSize);
u64 res = dataResidue();

if (res == loaded.res64) {
log("OK %9u on-load: blockSize %d, %016" PRIx64 "\n", loaded.k, loaded.blockSize, res);
return {loaded.k, loaded.blockSize, loaded.nErrors};
if (res == state.res64) {
log("OK %9u on-load: blockSize %d, %016" PRIx64 "\n", state.k, state.blockSize, res);
return state;
// return {loaded.k, loaded.blockSize, loaded.nErrors};
}

log("EE %9u on-load: %016" PRIx64 " vs. %016" PRIx64 "\n", loaded.k, res, loaded.res64);
log("EE %9u on-load: %016" PRIx64 " vs. %016" PRIx64 "\n", state.k, res, state.res64);

// Attempt to load the first savefile twice (to verify res64 reproducibility)
if (!nTries) { continue; }

if (!loaded.k) { break; } // We failed on PRP start
if (!state.k) { break; } // We failed on PRP start

saver.dropMostRecent(); // Try an earlier savefile
}
Expand Down Expand Up @@ -1192,16 +1193,27 @@ double Gpu::timePRP() {

PRPResult Gpu::isPrimePRP(const Task& task) {
const constexpr u32 LOG_STEP = 20'000; // log every 20k its

assert(E == task.exponent);

// This timer is used to measure total elapsed time to be written to the savefile.
Timer elapsedTimer;

u32 nErrors = 0;
int nSeqErrors = 0;
u64 lastFailedRes64 = 0;

reload:
auto [k, blockSize, nLoadErrors] = loadPRP(*getSaver());
nErrors = std::max(nErrors, nLoadErrors);
elapsedTimer.reset();
u32 blockSize{}, k{};
double elapsedBefore = 0;

{
PRPState state = loadPRP(*getSaver());
nErrors = std::max(nErrors, state.nErrors);
blockSize = state.blockSize;
k = state.k;
elapsedBefore = state.elapsed;
}

assert(blockSize > 0 && LOG_STEP % blockSize == 0);

Expand Down Expand Up @@ -1308,7 +1320,8 @@ PRPResult Gpu::isPrimePRP(const Task& task) {

if (!doCheck) {
(*background)([=, this] {
getSaver()->saveUnverified({E, k, blockSize, res, compactBits(rawCheck, E), nErrors});
getSaver()->saveUnverified({E, k, blockSize, res, compactBits(rawCheck, E), nErrors,
elapsedBefore + elapsedTimer.at()});
});

log(" %9u %016" PRIx64 " %4.0f\n", k, res, /*k / float(kEndEnd) * 100*,*/ secsPerIt * 1'000'000);
Expand All @@ -1329,7 +1342,7 @@ PRPResult Gpu::isPrimePRP(const Task& task) {

if (k < kEnd) {
(*background)([=, this, rawCheck = std::move(rawCheck)] {
getSaver()->save({E, k, blockSize, res, compactBits(rawCheck, E), nErrors});
getSaver()->save({E, k, blockSize, res, compactBits(rawCheck, E), nErrors, elapsedBefore + elapsedTimer.at()});
});
}

Expand Down Expand Up @@ -1370,13 +1383,19 @@ LLResult Gpu::isPrimeLL(const Task& task) {
assert(E == task.exponent);
wantROE = 0;

Saver<LLState> saver{E, 0, args.nSavefiles};
Timer elapsedTimer;

Saver<LLState> saver{E, 1000, args.nSavefiles};

reload:
elapsedTimer.reset();

u32 startK = 0;
double elapsedBefore = 0;
{
LLState state = saver.load();

elapsedBefore = state.elapsed;
startK = state.k;
u64 expectedRes = (u64(state.data[1]) << 32) | state.data[0];
writeIn(bufData, std::move(state.data));
Expand Down Expand Up @@ -1429,7 +1448,7 @@ LLResult Gpu::isPrimeLL(const Task& task) {
} else {
assert(data.size() >= 2);
res64 = (u64(data[1]) << 32) | data[0];
saver.save({E, k, std::move(data)});
saver.save({E, k, std::move(data), elapsedBefore + elapsedTimer.at()});
}

float secsPerIt = iterationTimer.reset(k);
Expand Down
2 changes: 1 addition & 1 deletion src/Gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class Gpu {

u32 updateCarryPos(u32 bit);

std::tuple<u32, u32, u32> loadPRP(Saver<PRPState>& saver);
PRPState loadPRP(Saver<PRPState>& saver);

vector<int> readChecked(Buffer<int>& buf);

Expand Down
Loading

0 comments on commit 3c66373

Please sign in to comment.