From b5a6bc4a481cec8db2afc0bc507e3eea21cc30ed Mon Sep 17 00:00:00 2001 From: Jean-loup Gailly Date: Wed, 13 Jan 2010 01:18:54 +0100 Subject: [PATCH] Define a memory limit for nodes to avoid crashing later. --- uct/tree.c | 1 + uct/tree.h | 1 + uct/walk.c | 7 +++++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/uct/tree.c b/uct/tree.c index 77cf9d4..b431d6b 100644 --- a/uct/tree.c +++ b/uct/tree.c @@ -27,6 +27,7 @@ tree_init_node(struct tree *t, coord_t coord, int depth) fprintf(stderr, "tree_init_node(): OUT OF MEMORY\n"); exit(1); } + __sync_fetch_and_add(&t->node_sizes, sizeof(*n)); n->coord = coord; n->depth = depth; volatile static long c = 1000000; diff --git a/uct/tree.h b/uct/tree.h index e74fa5f..97cb5f8 100644 --- a/uct/tree.h +++ b/uct/tree.h @@ -69,6 +69,7 @@ struct tree { // Statistics int max_depth; + volatile long node_sizes; // byte size of all allocated nodes }; struct tree *tree_init(struct board *board, enum stone color); diff --git a/uct/walk.c b/uct/walk.c index f1361b1..4ea2f28 100644 --- a/uct/walk.c +++ b/uct/walk.c @@ -19,6 +19,8 @@ #include "uct/uct.h" #include "uct/walk.h" +// This should become a dynamic parameter. 7G is suitable for 20k*23 threads. +#define MAX_NODE_SIZES (7L*1024*1024*1024) float uct_get_extra_komi(struct uct *u, struct board *b) @@ -91,7 +93,7 @@ uct_leaf_node(struct uct *u, struct board *b, enum stone player_color, { enum stone next_color = stone_other(node_color); int parity = (next_color == player_color ? 1 : -1); - if (n->u.playouts >= u->expand_p) { + if (n->u.playouts >= u->expand_p && t->node_sizes < MAX_NODE_SIZES) { // fprintf(stderr, "expanding %s (%p ^-%p)\n", coord2sstr(n->coord, b), n, n->parent); if (!u->parallel_tree) { /* Single-threaded, life is easy. */ @@ -102,7 +104,8 @@ uct_leaf_node(struct uct *u, struct board *b, enum stone player_color, * threads to meet in the same node, the latter * one will simply do another simulation from * the node itself, no big deal. */ - if (!__sync_lock_test_and_set(&n->is_expanded, 1)) { + if (!__sync_lock_test_and_set(&n->is_expanded, 1) && + t->node_sizes < MAX_NODE_SIZES) { assert(tree_leaf_node(n)); tree_expand_node(t, n, b, next_color, u, parity); } -- 2.11.4.GIT