aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--centre.cpp127
-rw-r--r--metasol.cpp98
-rw-r--r--metasol.hpp45
4 files changed, 225 insertions, 51 deletions
diff --git a/Makefile b/Makefile
index e0db9d4..165dafa 100644
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,10 @@
CXX = g++
-CXXFLAGS = -ggdb3 -O0 -Wpedantic -Wall -Werror
+CXXFLAGS = -ggdb3 -O0 -Wpedantic -Wall -Werror -std=c++17
OUTPUT_OPTION = -MMD -MP -o $@
LINK.o = $(LINK.cc)
-LDFLAGS = -Lthpool -Ldocopt -Ljsmn -Lsolvitaire
-LDLIBS = -lpthread -lsol -lthpool -ldocopt -ljsmn
+LDFLAGS = -Ldocopt -Ljsmn -Lsolvitaire
+LDLIBS = -lpthread -lsol -ldocopt -ljsmn
TRGT = centre
SRC = metasol.cpp centre.cpp
diff --git a/centre.cpp b/centre.cpp
index 0f067c5..e47271c 100644
--- a/centre.cpp
+++ b/centre.cpp
@@ -15,13 +15,17 @@ static const char USAGE[] =
R"(centre - a metasol instance.
Usage: centre <rules_file> [options]
+ centre --help|-h
Options:
- -g FILE --game FILE Specify a .json file describing a game-state.
- -h --help Display this help.
- -s SEED --seed SEED Specify a seed to use when randomly generating a game
- (set to 0 to use an unspecified seed).
- -v --version Display version.)";
+ -h --help Display this help.
+ -g FILE --game FILE Specify a file describing a game-state.
+ --dealseed SEED Specify a seed to use when randomly generating a game
+ (set to 0 to use an unspecified seed).
+ --solveseed SEED Specify a seed for the solver to use
+ (set to 0 to use an unspecified seed).
+ -s FILE --settings FILE Specify a settings file.
+ -v --version Display version.)";
static const char VERSION_STR[] = "Solver in-development";
@@ -531,12 +535,15 @@ bool solved(ms_game_state *gs, ms_rules *r, void *d) {
ms_settings get_settings(user_data *d) {
ms_settings s = fetch_default_settings();
+ s.seed = (unsigned long) time(0);
+
s.run_func = &run_single;
s.thoughtful_run_func = &thoughtful_run;
s.solved_func = &solved;
s.reserved_move_count = 0u;
s.max_concurrent_threads = 24u;
+ s.max_concurrent_games = 5u;
s.max_votes = 10u;
s.user_data = (void *) d;
@@ -708,6 +715,76 @@ int assign_foundations_base(jsmntok_t *key, ms_rules *r, char *js) {
}
}
+void json_settings(ms_settings *s, const char *filename) {
+ FILE *f = fopen(filename, "rb");
+ long length = filelen(f);
+ char *buf = (char *) malloc(length);
+
+ assert(buf);
+
+ file_str(f, buf, length);
+
+ jsmn_parser parser;
+ jsmn_init(&parser);
+ int tok_count = jsmn_parse(&parser, buf, length, NULL, 0);
+ jsmntok_t *tokens = (jsmntok_t *) malloc(tok_count * sizeof(jsmntok_t));
+
+ assert(tokens);
+
+ jsmn_init(&parser);
+ auto parse_val = jsmn_parse(&parser, buf, length, tokens, tok_count);
+
+ assert(parse_val != JSMN_ERROR_INVAL);
+ assert(parse_val != JSMN_ERROR_NOMEM);
+ assert(parse_val != JSMN_ERROR_PART);
+
+ for (int i = 0; i < tok_count;) {
+ jsmntok_t *tok = &tokens[i];
+
+ int x = 0;
+ switch (tok->type) {
+ case JSMN_OBJECT: {
+ x = 1;
+ break;
+ } case JSMN_STRING:
+ case JSMN_PRIMITIVE: {
+ if (buf[tok->start] == '#') {
+ x = 1;
+ break;
+ }
+
+# define arp(n, t, p) { \
+ x = assign_rule_prim(tok, n, t, p, buf); \
+ if (x) break; \
+ }
+
+ arp("max concurrent threads", NUMBER_TYPE,
+ &s->max_concurrent_threads);
+ arp("max_concurrent_threads", NUMBER_TYPE,
+ &s->max_concurrent_threads);
+ arp("max-concurrent-threads", NUMBER_TYPE,
+ &s->max_concurrent_threads);
+
+ arp("max concurrent games", NUMBER_TYPE,
+ &s->max_concurrent_games);
+ arp("max_concurrent_games", NUMBER_TYPE,
+ &s->max_concurrent_games);
+ arp("max-concurrent-games", NUMBER_TYPE,
+ &s->max_concurrent_games);
+
+ arp("seed", NUMBER_TYPE, &s->seed);
+
+ } default: {
+ assert(false);
+ }
+ }
+ i += x;
+ }
+
+ free(buf);
+ free(tokens);
+}
+
ms_rules json_rules(const char *filename) {
FILE *f = fopen(filename, "rb");
long length = filelen(f);
@@ -1000,8 +1077,8 @@ ms_game_state make_game_state(ms_rules *r,
return json_game_state(filename);
} else {
long seed;
- if (args["--seed"]) {
- seed = args["--seed"].asLong();
+ if (args["--dealseed"]) {
+ seed = args["--dealseed"].asLong();
} else {
seed = 0;
}
@@ -1015,13 +1092,33 @@ ms_rules make_rules(std::map<std::string, docopt::value> &args) {
return json_rules(filename);
}
-void parse_args(int argc, char **argv, ms_game_state *gs, ms_rules *r) {
- std::map<std::string, docopt::value> args = docopt::docopt(USAGE,
- { argv + 1, argv + argc }, true, VERSION_STR);
+ms_settings make_settings(std::map<std::string, docopt::value> &args,
+ user_data *d) {
+ ms_settings s = get_settings(d);
+
+ if (args["--settings"]) {
+ const char *filename = args["--settings"].asString().c_str();
+ json_settings(&s, filename);
+ }
+ if (args["--solveseed"]) {
+ s.seed = (unsigned long)
+ strtoll(args["--solveseed"].asString().c_str(), NULL, 0);
+ }
+
+ s.rng = std::default_random_engine{s.seed};
+ return s;
+}
+
+void parse_args(int argc, char **argv, ms_game_state *gs, ms_rules *r,
+ ms_settings *s, user_data *d) {
+
+ auto args = docopt::docopt(USAGE, { argv + 1, argv + argc }, true,
+ VERSION_STR);
+
+ *s = make_settings(args, d);
*r = make_rules(args);
*gs = make_game_state(r, args);
- /* make_settings(s, args); */
}
int main(int argc, char **argv) {
@@ -1029,10 +1126,10 @@ int main(int argc, char **argv) {
ms_settings s;
ms_game_state gs;
- parse_args(argc, argv, &gs, &r);
-
user_data d = get_user_data();
- s = get_settings(&d);
- return ms_run(&gs, &r, &s);
+ parse_args(argc, argv, &gs, &r, &s, &d);
+
+ ctpl::thread_pool tp(s.max_concurrent_threads);
+ return ms_run(&gs, &r, &s, &tp);
}
diff --git a/metasol.cpp b/metasol.cpp
index 6331033..b77dee4 100644
--- a/metasol.cpp
+++ b/metasol.cpp
@@ -1,11 +1,12 @@
#include <algorithm>
+#include <future>
#include <map>
#include <random>
#include <assert.h>
#include <err.h>
-#include "thpool/thpool.h"
+#include "ctpl/ctpl_stl.h"
#include "metasol.hpp"
#include "debug.h"
@@ -17,12 +18,12 @@ void lower(char *str) {
}
struct dictcmp {
- bool operator()(const char *a, const char *b) {
+ bool operator()(const char *a, const char *b) const {
return strcmp(a, b) < 0;
}
};
-unsigned face_down_count_pile(ms_card_pile *p) {
+static inline unsigned face_down_count_pile(ms_card_pile *p) {
unsigned result = 0u;
for (auto &c : *p) {
result += c.hidden ? 1u : 0u;
@@ -30,7 +31,7 @@ unsigned face_down_count_pile(ms_card_pile *p) {
return result;
}
-unsigned face_down_count_piles(std::vector<ms_card_pile> *ps) {
+static inline unsigned face_down_count_piles(std::vector<ms_card_pile> *ps) {
unsigned result = 0u;
for (auto &p : *ps) {
result += face_down_count_pile(&p);
@@ -72,7 +73,7 @@ build_policy_t str_build_policy(char *str) {
lower(str);
auto result = dict.find(str);
-
+
if (result == dict.end()) {
errx(EXIT_FAILURE, "unknown key '%s'", str);
}
@@ -123,7 +124,7 @@ spaces_policy_t str_spaces_policy(char *str) {
lower(str);
auto result = dict.find(str);
-
+
if (result == dict.end()) {
errx(EXIT_FAILURE, "unknown key '%s'", str);
}
@@ -165,7 +166,7 @@ accordion_policy_t str_accordion_policy(char *str) {
lower(str);
auto result = dict.find(str);
-
+
if (result == dict.end()) {
errx(EXIT_FAILURE, "unknown key '%s'", str);
}
@@ -468,10 +469,7 @@ unsigned total_pile_count(ms_rules *r) {
// research required!
}
-std::random_device rd;
-auto seed = static_cast<long unsigned int>(time(0));
-auto rng = std::default_random_engine{seed};
-int shuffle_hidden(ms_game_state *gs) {
+int shuffle_hidden(ms_game_state *gs, ms_settings *s) {
std::vector<ms_card *> hidden_card_pointers;
get_hidden_cards(gs, hidden_card_pointers);
@@ -479,9 +477,9 @@ int shuffle_hidden(ms_game_state *gs) {
for (ms_card *p : hidden_card_pointers)
hidden_cards.push_back(*p);
- std::shuffle(hidden_cards.begin(), hidden_cards.end(), rng);
+ std::shuffle(hidden_cards.begin(), hidden_cards.end(), s->rng);
- for (typeof(hidden_cards.size()) i = 0; i < hidden_cards.size(); ++i) {
+ for (__typeof__(hidden_cards.size()) i = 0; i < hidden_cards.size(); ++i) {
ms_card *cp = hidden_card_pointers[i];
ms_card c = hidden_cards[i];
*cp = c;
@@ -538,11 +536,10 @@ typedef struct {
finished_state *result;
} thread_info;
-void *run_thread(void *vargp) {
+void run_thread(int id, void *vargp) {
thread_info *ti = (thread_info *) vargp;
*ti->result = ti->s->run_func(ti->gs, ti->r, ti->move_buf,
ti->s->reserved_move_count, ti->s->user_data);
- return NULL;
}
bool ms_move_lt(const ms_move &a, const ms_move &b) {
@@ -555,7 +552,7 @@ bool ms_move_lt(const ms_move &a, const ms_move &b) {
}
struct votecmp {
- bool operator()(const ms_move &a, const ms_move &b) {
+ bool operator()(const ms_move &a, const ms_move &b) const {
return ms_move_lt(a, b);
}
};
@@ -740,9 +737,11 @@ bool find_majority_move(vote *v, unsigned vote_count, ms_move *m) {
return false;
}
+typedef std::vector<std::future<void>> jobs;
+
void run_new_voters(ms_game_state *gs, ms_rules *r, ms_settings *s,
- threadpool *thpool, ms_game_state *ss, vote *v, thread_info *ti,
- unsigned n) {
+ ctpl::thread_pool *thpool, ms_game_state *ss, vote *v, thread_info *ti,
+ jobs *fs, unsigned n) {
for (unsigned i = 0; i < n; ++i) {
@@ -751,7 +750,7 @@ void run_new_voters(ms_game_state *gs, ms_rules *r, ms_settings *s,
*/
if (v[i].result != SOLUTION_FOUND || v[i].moves.empty()) {
ss[i] = *gs;
- shuffle_hidden(&ss[i]);
+ shuffle_hidden(&ss[i], s);
ti[i].gs = &ss[i];
ti[i].r = r;
@@ -759,8 +758,7 @@ void run_new_voters(ms_game_state *gs, ms_rules *r, ms_settings *s,
ti[i].move_buf = &v[i].moves;
ti[i].result = &v[i].result;
- thpool_add_work(*thpool, (void (*)(void *)) &run_thread,
- (void *) &ti[i]);
+ fs->push_back(thpool->push(run_thread, &ti[i]));
}
}
}
@@ -838,7 +836,7 @@ solve_status loop_check(ms_game_state *gs, ms_rules *r, ms_settings *s) {
}
solve_status run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
- threadpool *thpool, vote *votes, thread_info *t_infos,
+ ctpl::thread_pool *thpool, vote *votes, thread_info *t_infos,
ms_game_state *shuffled_states) {
unsigned vote_count = s->max_votes;
@@ -858,25 +856,28 @@ solve_status run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
* Set up the thread_info structs and add a job to the threadpool for each
* one.
*/
- run_new_voters(gs, r, s, thpool, shuffled_states, votes, t_infos,
+ jobs fs;
+ run_new_voters(gs, r, s, thpool, shuffled_states, votes, t_infos, &fs,
vote_count);
/*
* Wait on all of the voters to complete.
*/
- thpool_wait(*thpool);
+ for (auto &f : fs) {
+ assert(f.valid());
+ f.wait();
+ }
/*
* Print information about each voter.
*/
- debug("state\t\t\tmove\tmoves left\n");
+ debug( "%-15s %-10s %3s\n", "state", "move", "moves left");
for (unsigned i = 0; i < s->max_votes; ++i) {
if (votes[i].moves.empty()) {
debug("%-15s\n", finished_state_str(votes[i].result));
} else {
ms_move m = votes[i].moves.back();
- debug("%-15s %-10s %3lu\n",
- finished_state_str(votes[i].result),
+ debug("%-15s %-10s %3lu\n", finished_state_str(votes[i].result),
move_str_buf(&m),
votes[i].moves.size());
}
@@ -901,7 +902,8 @@ solve_status run_loop(ms_game_state *gs, ms_rules *r, ms_settings *s,
return loop_check(gs, r, s);
}
-int ms_run(ms_game_state *gs, ms_rules *r, ms_settings *s) {
+int ms_run(ms_game_state *gs, ms_rules *r, ms_settings *s,
+ ctpl::thread_pool *tp) {
/*
* Here is where the memory required for storing all of the information
@@ -921,14 +923,44 @@ int ms_run(ms_game_state *gs, ms_rules *r, ms_settings *s) {
thread_info *t_infos = gimme_mem(thread_info);
ms_game_state *states = gimme_mem(ms_game_state);
- threadpool thpool = thpool_init(s->max_concurrent_threads);
-
solve_status x;
do {
- x = run_loop(gs, r, s, &thpool, &votes[0], t_infos, states);
+ x = run_loop(gs, r, s, tp, &votes[0], t_infos, states);
} while (x == KEEP_GOING);
- debug("seed: %lu\tsuccess: %s\n", seed, bool_str(x == SOLVED));
+ printf("solveseed: %lu\tdealseed: %lu\tsuccess: %s\n", s->seed, gs->seed,
+ bool_str(x == SOLVED));
+
+ return 0;
+}
+
+int run_single(ms_rules *r, ms_settings *s, unsigned long seed,
+ ctpl::thread_pool *tp) {
+ ms_game_state gs = random_game_state(seed, r);
+ return ms_run(&gs, r, s, tp);
+}
+
+int ms_run_single(ms_rules *r, ms_settings *s, unsigned long seed) {
+ ctpl::thread_pool tp(s->max_concurrent_threads);
+ return run_single(r, s, seed, &tp);
+}
+
+int ms_run_infinite(int tid, ms_rules *r, ms_settings *s, ctpl::thread_pool *tp,
+ unsigned n, unsigned i) {
+ for (unsigned long id = i; true; i += n) {
+ run_single(r, s, id, tp);
+ }
+ return 0;
+}
+
+int ms_run_many(ms_rules *r, ms_settings *s) {
+ unsigned n = s->max_concurrent_games;
+ ctpl::thread_pool game_tp(n);
+
+ ctpl::thread_pool main_tp(s->max_concurrent_threads);
+ for (unsigned i = 0; i < n; ++i) {
+ game_tp.push(ms_run_infinite, r, s, &main_tp, n, i);
+ }
return 0;
}
@@ -1196,5 +1228,7 @@ ms_game_state random_game_state(long user_seed, ms_rules *r) {
}
assert(deck.empty());
+
+ gs.seed = seed;
return gs;
}
diff --git a/metasol.hpp b/metasol.hpp
index 7094688..ba1451c 100644
--- a/metasol.hpp
+++ b/metasol.hpp
@@ -15,10 +15,13 @@
#define METASOL_H
#include <vector>
+#include <random>
#include <stdint.h>
#include <string.h>
+#include "ctpl/ctpl_stl.h"
+
/**
* Rule describing what cards other cards can be moved on top of.
*/
@@ -580,6 +583,11 @@ typedef struct {
ms_card_pile hole;
std::vector<ms_card_pile> cells;
ms_card_pile reserve;
+
+ /**
+ * The seed used for random generation of the game-state.
+ */
+ long seed;
} ms_game_state;
/**
@@ -740,10 +748,25 @@ typedef struct {
unsigned max_concurrent_threads;
/**
+ * How many games to run in parallel.
+ */
+ unsigned max_concurrent_games;
+
+ /**
* How many votes are used to determine the next move.
*/
unsigned max_votes;
+ /**
+ * The seed used when shuffling face-down cards around.
+ */
+ unsigned long seed;
+
+ /**
+ * The random generator used when shuffling face-down cards around.
+ */
+ std::default_random_engine rng;
+
} ms_settings;
/**
@@ -805,7 +828,27 @@ ms_game_state random_game_state(long seed, ms_rules *rules);
* @param game_state The game-state to solve from.
* @param rules The rules describing the game.
* @param settings The settings to use when running.
+ * @param thpool The thread pool to use.
+ */
+int ms_run(ms_game_state *game_state, ms_rules *rules, ms_settings *settings,
+ ctpl::thread_pool *thpool);
+
+/**
+ * Constructs and attempts to solve a single game-state.
+ *
+ * @param rules The rules of the game-state to create.
+ * @param settings The settings to run the solver with.
+ * @param seed The seed for generating the game-state.
+ */
+int ms_run_single(ms_rules *rules, ms_settings *settings, unsigned long seed);
+
+/**
+ * Constructs and plays games until terminated.
+ *
+ * @param rules The rules of the game-state to create.
+ * @param settings The settings to run the solvers with.
+ * @returns 0.
*/
-int ms_run(ms_game_state *game_state, ms_rules *rules, ms_settings *settings);
+int ms_run_many(ms_rules *rules, ms_settings *settings);
#endif /* METASOL_H */