Commit 679d906c authored by Adam P. Goucher's avatar Adam P. Goucher

Apple Bottom's patch to enable mid-search quitting

parent 66148c2f
#include <stdio.h>
#include <sys/select.h>
// determine whether there's a keystroke waiting
int keyWaiting() {
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds); // STDIN_FILENO is 0
select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
return FD_ISSET(STDIN_FILENO, &fds);
}
#pragma once
#ifdef USE_OPEN_MP
void parallelSearch(int n, int m, std::string payoshaKey, std::string seed, int local_log) {
bool parallelSearch(int n, int m, std::string payoshaKey, std::string seed, int local_log) {
SoupSearcher globalSoup;
......@@ -54,12 +73,14 @@ void parallelSearch(int n, int m, std::string payoshaKey, std::string seed, int
}
std::cout << "----------------------------------------------------------------------" << std::endl;
}
return false;
}
#endif
void runSearch(int n, std::string payoshaKey, std::string seed, int local_log, bool testing) {
bool runSearch(int n, std::string payoshaKey, std::string seed, int local_log, bool testing) {
SoupSearcher soup;
apg::lifetree<uint32_t, BITPLANES> lt(400);
......@@ -73,8 +94,9 @@ void runSearch(int n, std::string payoshaKey, std::string seed, int local_log, b
int64_t lasti = 0;
bool finishedSearch = false;
bool quitByUser = false;
while (finishedSearch == false) {
while ((finishedSearch == false) && (quitByUser == false)) {
std::ostringstream ss;
ss << i;
......@@ -89,9 +111,16 @@ void runSearch(int n, std::string payoshaKey, std::string seed, int local_log, b
std::cout << i << " soups completed (" << ((int) ((i - lasti) / elapsed)) << " soups per second)." << std::endl;
lasti = i;
start = clock();
if(keyWaiting()) {
char c = fgetc(stdin);
if ((c == 'q') || (c == 'Q'))
quitByUser = true;
}
}
if (i % n == 0) {
if ((i % n == 0) || quitByUser) {
std::cout << "----------------------------------------------------------------------" << std::endl;
std::cout << i << " soups completed." << std::endl;
std::cout << "Attempting to contact payosha256." << std::endl;
......@@ -108,6 +137,8 @@ void runSearch(int n, std::string payoshaKey, std::string seed, int local_log, b
}
}
return quitByUser;
}
......
......@@ -7,6 +7,7 @@
#include <ctime>
#include <cmath>
#include <unistd.h>
#include <termios.h>
#ifdef USE_OPEN_MP
#include <omp.h>
......@@ -16,7 +17,7 @@
#include "lifelib/classifier.h"
#include "lifelib/incubator.h"
#define APG_VERSION "v4.13-" LIFELIB_VERSION
#define APG_VERSION "v4.14-" LIFELIB_VERSION
#include "includes/params.h"
#include "includes/sha256.h"
......@@ -41,7 +42,9 @@ int main (int argc, char *argv[]) {
int local_log = 0;
bool testing = false;
int nullargs = 1;
bool quitByUser = false;
struct termios ttystate;
// Extract options:
for (int i = 1; i < argc - 1; i++) {
if (strcmp(argv[i], "-k") == 0) {
......@@ -95,6 +98,12 @@ int main (int argc, char *argv[]) {
if (verifications < 0) {
verifications = (parallelisation <= 4) ? 3 : 0;
}
// turn on non-blocking reads
tcgetattr(STDIN_FILENO, &ttystate);
ttystate.c_lflag &= ~ICANON;
ttystate.c_cc[VMIN] = 1;
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
std::cout << "\nGreetings, this is \033[1;33mapgluxe " << APG_VERSION;
std::cout << "\033[0m, configured for \033[1;34m" << RULESTRING << "/";
......@@ -106,7 +115,7 @@ int main (int argc, char *argv[]) {
std::cout << std::endl;
while (true) {
while (!quitByUser) {
if (verifications > 0) {
std::cout << "Peer-reviewing hauls:\n" << std::endl;
// Verify some hauls:
......@@ -120,17 +129,22 @@ int main (int argc, char *argv[]) {
std::cout << "Using seed " << seed << std::endl;
if (parallelisation > 0) {
#ifdef USE_OPEN_MP
parallelSearch(soups_per_haul, parallelisation, payoshaKey, seed, local_log);
quitByUser = parallelSearch(soups_per_haul, parallelisation, payoshaKey, seed, local_log);
#else
runSearch(soups_per_haul, payoshaKey, seed, local_log, false);
quitByUser = runSearch(soups_per_haul, payoshaKey, seed, local_log, false);
#endif
} else {
runSearch(soups_per_haul, payoshaKey, seed, local_log, testing);
quitByUser = runSearch(soups_per_haul, payoshaKey, seed, local_log, testing);
}
seed = reseed(seed);
if (testing) { break; }
}
return 0;
// turn on blocking reads
tcgetattr(STDIN_FILENO, &ttystate);
ttystate.c_lflag |= ICANON;
tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
return quitByUser ? 1 : 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment