From 597bcf776fa6ec028d0bdd8807744707e5b49fb8 Mon Sep 17 00:00:00 2001 From: Petr Baudis Date: Sat, 16 Jan 2010 02:24:52 +0100 Subject: [PATCH] UCT Threading: Split uct_playouts_threaded() to uct_pondering_start(), uct_pondering_stop() --- uct/uct.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/uct/uct.c b/uct/uct.c index 3b96df4..4018235 100644 --- a/uct/uct.c +++ b/uct/uct.c @@ -250,10 +250,13 @@ uct_done(struct engine *e) * uct_playouts() loop, doing descend-playout N=games times */ -/* Set in main thread in case the playouts should stop. */ +/* Set in thread manager in case the workers should stop. */ volatile sig_atomic_t uct_halt = 0; /* ID of the running worker thread. */ __thread int thread_id = -1; +/* ID of the thread manager. */ +static pthread_t thread_manager; +static bool thread_manager_running; static pthread_mutex_t finish_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t finish_cond = PTHREAD_COND_INITIALIZER; @@ -351,24 +354,42 @@ spawn_thread_manager(void *ctx_) return mctx; } -static int -uct_playouts_threaded(struct uct *u, struct board *b, enum stone color, struct tree *t, int games) +static void +uct_pondering_start(struct uct *u, struct board *b0, enum stone color, struct tree *t, int games) { assert(u->threads > 0); + assert(!thread_manager_running); + + /* *b0 can change in the meantime. */ + struct board b; board_copy(&b, b0); - pthread_t manager; - struct spawn_ctx ctx = { .u = u, .b = b, .color = color, .t = t, .games = games, .seed = fast_random(65536) }; + struct spawn_ctx ctx = { .u = u, .b = &b, .color = color, .t = t, .games = games, .seed = fast_random(65536) }; + static struct spawn_ctx mctx; mctx = ctx; pthread_mutex_lock(&finish_mutex); - pthread_create(&manager, NULL, spawn_thread_manager, &ctx); + pthread_create(&thread_manager, NULL, spawn_thread_manager, &mctx); + thread_manager_running = true; +} - /* We just wait until the thread manager finishes. */ +static int +uct_pondering_stop(void) +{ + assert(thread_manager_running); struct spawn_ctx *pctx; - pthread_join(manager, (void **) &pctx); + thread_manager_running = false; + pthread_join(thread_manager, (void **) &pctx); pthread_mutex_unlock(&finish_mutex); return pctx->games; } +static int +uct_playouts_threaded(struct uct *u, struct board *b, enum stone color, struct tree *t, int games) +{ + uct_pondering_start(u, b, color, t, games); + /* We just wait until the thread manager finishes. */ + return uct_pondering_stop(); +} + static coord_t * uct_genmove(struct engine *e, struct board *b, enum stone color, bool pass_all_alive) -- 2.11.4.GIT