From 47ac569ad15954f7b24ad5d8dcd242dbd3c8d4fe Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Sat, 16 Jun 2018 10:37:26 +0100 Subject: Add option to resume stopped run --- src/main/evaluation/solvability_calc.cpp | 23 ++++++++++------ src/main/evaluation/solvability_calc.h | 4 +-- .../input-output/input/command_line_helper.cpp | 32 +++++++++++++++++++--- src/main/input-output/input/command_line_helper.h | 7 +++-- src/main/main.cpp | 2 +- 5 files changed, 50 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index 2f9c40d..7597dc9 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -78,8 +78,12 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, s // SOLVING METHODS // ///////////////////// -void solvability_calc::calculate_solvability_percentage(int timeout_, int cores) { - atomic current_seed(0); +void solvability_calc::calculate_solvability_percentage(int timeout_, uint cores, const vector& resume) { + vector resume_seeds(begin(resume) + 3, end(resume)); + sort(begin(resume_seeds), end(resume_seeds)); + + atomic current_seed(resume_seeds.back()); + resume_seeds.pop_back(); set seeds_in_progress; mutex sip_mutex; @@ -90,20 +94,19 @@ void solvability_calc::calculate_solvability_percentage(int timeout_, int cores) // Spin off 'cores' threads. Each one runs solve_seed and then print_row repeatedly, taking the seed from am // atomic int vector> futures; - seed_results seed_res; + seed_results seed_res(resume); sol_rules sr = rules; uint64_t cc = cache_capacity; - for (int i = 0; i < cores; i++) { + for (uint i = 0; i < cores; i++) { futures.push_back(async( launch::async, - [¤t_seed, &seed_res, &seeds_in_progress, &sip_mutex, timeout, sr, cc](){ + [¤t_seed, &seed_res, &seeds_in_progress, &sip_mutex, timeout, sr, cc, i, resume_seeds](){ - int my_seed = 0; - while (my_seed < INT_MAX) { - my_seed = current_seed++; + int my_seed = resume_seeds.size() > i ? resume_seeds[i] : current_seed++; + while (my_seed < INT_MAX) { sip_mutex.lock(); seeds_in_progress.insert(my_seed); sip_mutex.unlock(); @@ -115,6 +118,8 @@ void solvability_calc::calculate_solvability_percentage(int timeout_, int cores) sip_mutex.unlock(); print_row(seed_res, res, seeds_in_progress, sip_mutex); + + my_seed = current_seed++; } } )); @@ -197,7 +202,7 @@ solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec tim // SEED RESULTS CLASS // //////////////////////// -solvability_calc::seed_results::seed_results() : solvable(0), unsolvable(0), intractable(0) { +solvability_calc::seed_results::seed_results(vector v) : solvable(v[0]), unsolvable(v[1]), intractable(v[2]) { } void solvability_calc::seed_results::add_result(sol_result::type t) { diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index 4be57d1..367c469 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -16,7 +16,7 @@ class solvability_calc { public: explicit solvability_calc(const sol_rules&, uint64_t); - void calculate_solvability_percentage(int timeout, int cores); + void calculate_solvability_percentage(int timeout, uint cores, const std::vector&); private: typedef std::chrono::milliseconds millisec; @@ -32,7 +32,7 @@ private: }; struct seed_results { - seed_results(); + seed_results(std::vector); void add_result(sol_result::type); std::atomic solvable; diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index cfbfd2f..2abef54 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -44,7 +44,10 @@ command_line_helper::command_line_helper() "percentage of the supplied solitaire game, given a timeout in milliseconds. Must supply " "either 'random', 'benchmark', 'solvability' or list of deals to be " "solved.") - ("cores", po::value(), "the number of cores for the solvability percentages to be run across. " + ("resume", po::value>()->multitoken(), "resumes the solvability percentage calculation from a " + "previous run. Must be supplied with the solvability option. " + "Syntax: [sol unsol intract in-progress-1 in-progress-2 ...]") + ("cores", po::value(), "the number of cores for the solvability percentages to be run across. " "Must be supplied with the solvability option.") ("benchmark", "outputs performance statistics for the solver on the " "supplied solitaire game. Must supply " @@ -119,11 +122,18 @@ bool command_line_helper::parse(int argc, const char* argv[]) { } if (vm.count("cores")) { - cores = vm["cores"].as(); + cores = vm["cores"].as(); } else { cores = 1; } + if (vm.count("resume")) { + resume = vm["resume"].as>(); + } else { + resume = {0, 0, 0}; + for (uint i = 0; i < cores; i++) resume.push_back(i); + } + benchmark = (vm.count("benchmark") != 0); @@ -139,6 +149,11 @@ bool command_line_helper::assess_errors() { if (available_game_types || !describe_game_rules.empty()) return true; + if (solvability > 0 && resume.size() != 3 + cores) { + print_resume_error(); + return false; + } + // The user must either supply input files, a random seed, or ask for the // solvability percentage, or benchmark int opt_count = (random_deal != -1) + !input_files.empty() + (solvability > 0) + benchmark; @@ -196,7 +211,12 @@ void command_line_helper::print_sol_type_rules_error() { void command_line_helper::print_too_many_opts_error() { LOG_ERROR ("Error: User must supply input file(s), the '--random' option, the 'benchmark' option,, " - "or the '--solvability' option, not multiple"); + "or the '--solvability' option, not multiple"); + print_help(); +} + +void command_line_helper::print_resume_error() { + LOG_ERROR ("Error: Resume contains " << resume.size() << " options, and should contain " << cores + 3); print_help(); } @@ -228,7 +248,11 @@ int command_line_helper::get_solvability() { return solvability; } -int command_line_helper::get_cores() { +vector command_line_helper::get_resume() { + return resume; +} + +uint command_line_helper::get_cores() { return cores; } diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h index f3320da..3ce239d 100644 --- a/src/main/input-output/input/command_line_helper.h +++ b/src/main/input-output/input/command_line_helper.h @@ -18,9 +18,10 @@ public: int get_random_deal(); bool get_classify(); int get_solvability(); - int get_cores(); + uint get_cores(); bool get_available_game_types(); bool get_benchmark(); + std::vector get_resume(); uint64_t get_cache_capacity(); std::string get_describe_game_rules(); @@ -31,6 +32,7 @@ private: void print_sol_type_rules_error(); void print_no_opts_error(); void print_too_many_opts_error(); + void print_resume_error(); boost::program_options::options_description cmdline_options; boost::program_options::options_description main_options; @@ -44,7 +46,8 @@ private: bool help; bool classify; int solvability; - int cores; + std::vector resume; + uint cores; bool available_game_types; bool benchmark; uint64_t cache_capacity; diff --git a/src/main/main.cpp b/src/main/main.cpp index f8f3d8f..c3bf652 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -51,7 +51,7 @@ int main(int argc, const char* argv[]) { // If the user has asked for a solvability percentage, calculates it if (clh.get_solvability() > 0) { solvability_calc solv_c(*rules, clh.get_cache_capacity()); - solv_c.calculate_solvability_percentage(clh.get_solvability(), clh.get_cores()); + solv_c.calculate_solvability_percentage(clh.get_solvability(), clh.get_cores(), clh.get_resume()); } // If a random deal seed has been supplied, solves it else if (clh.get_random_deal() != -1) { -- cgit v1.2.1