ref: 064bf1d47185e98e4defe6199befbc1d80c3b39b
parent: fffcd26f5d24f73d4e1dfb5ea53f0356db58a63b
author: zamfofex <zamfofex@twdb.moe>
date: Thu Nov 28 04:47:02 EST 2024
make threading improvements
--- a/main.c
+++ b/main.c
@@ -13,9 +13,10 @@
struct moonfish_info {struct moonfish_root *root;
- int thread_count;
+ _Atomic int thread_count;
_Atomic unsigned char searching;
#ifndef moonfish_no_threads
+ unsigned char has_thread;
thrd_t thread;
#endif
};
@@ -96,7 +97,7 @@
moonfish_best_move(info->root, &result, &options);
moonfish_to_uci(&chess, &result.move, name);
- info->searching = 2;
+ info->searching = 0;
printf("info depth 1 score cp %d nodes %ld multipv 1 pv %s\n", result.score, result.node_count, name); printf("bestmove %s\n", name);fflush(stdout);
@@ -169,7 +170,7 @@
if (!moonfish_equal(&chess0, &chess)) moonfish_reroot(root, &chess);
}
-static void moonfish_setoption(int *thread_count)
+static void moonfish_setoption(_Atomic int *thread_count)
{char *arg, *end;
long int count;
@@ -225,6 +226,7 @@
info.searching = 0;
#ifndef moonfish_no_threads
+ info.has_thread = 0;
info.thread_count = sysconf(_SC_NPROCESSORS_ONLN);
if (info.thread_count > 256) info.thread_count = 256;
if (info.thread_count < 1) info.thread_count = 4;
@@ -244,27 +246,29 @@
if (arg == NULL) continue;
if (!strcmp(arg, "go")) {+
#ifdef moonfish_no_threads
moonfish_go(&info);
#else
- if (info.searching == 2) {+ if (info.searching) {+ fprintf(stderr, "cannot start search while searching\n");
+ exit(1);
+ }
+ info.searching = 1;
+ if (info.has_thread) { if (thrd_join(info.thread, NULL) != thrd_success) {fprintf(stderr, "could not join thread\n");
exit(1);
}
}
- else {- if (info.searching) {- fprintf(stderr, "cannot start search while searching\n");
- exit(1);
- }
- }
- info.searching = 1;
+ info.has_thread = 1;
+ moonfish_unstop(info.root);
if (thrd_create(&info.thread, &moonfish_go, &info) != thrd_success) {fprintf(stderr, "could not create thread\n");
exit(1);
}
#endif
+
continue;
}
@@ -271,7 +275,7 @@
if (!strcmp(arg, "quit")) break;
if (!strcmp(arg, "position")) {- if (info.searching == 1) {+ if (info.searching) {fprintf(stderr, "cannot set position while searching\n");
exit(1);
}
@@ -298,20 +302,18 @@
if (!strcmp(arg, "setoption")) {moonfish_setoption(&info.thread_count);
- if (info.searching == 1) {- printf("info string warning: option will only take effect next search request\n");- }
+ if (info.searching) printf("info string warning: option will only take effect next search request\n");continue;
}
if (!strcmp(arg, "stop")) {- if (info.searching) {- moonfish_stop(info.root);
+ moonfish_stop(info.root);
+ if (info.has_thread) {+ info.has_thread = 0;
if (thrd_join(info.thread, NULL) != thrd_success) {fprintf(stderr, "could not join thread\n");
exit(1);
}
- info.searching = 0;
}
continue;
}
@@ -324,7 +326,7 @@
}
#ifndef moonfish_no_threads
- if (info.searching) {+ if (info.has_thread) {moonfish_stop(info.root);
if (thrd_join(info.thread, NULL) != thrd_success) {fprintf(stderr, "could not join thread\n");
--- a/moonfish.h
+++ b/moonfish.h
@@ -204,5 +204,6 @@
/* requests to stop searching the given state (from a different thread) */
void moonfish_stop(struct moonfish_root *root);
+void moonfish_unstop(struct moonfish_root *root);
#endif
--- a/search.c
+++ b/search.c
@@ -53,7 +53,7 @@
struct moonfish_root {struct moonfish_node node;
struct moonfish_chess chess;
- int stop;
+ _Atomic int stop;
};
struct moonfish_data {@@ -201,6 +201,7 @@
for (i = 0 ; i < node->count ; i++) {if (node->children[i].ignored) continue;
+ if (node->children[i].count == -1) continue;
confidence = moonfish_confidence(node->children + i);
if (confidence > max_confidence) {next = node->children + i;
@@ -208,6 +209,8 @@
}
}
+ if (next == NULL) continue;
+
node = next;
moonfish_node_chess(node, chess);
}
@@ -342,7 +345,6 @@
if (options->our_time >= 0 && time > options->our_time / 16) time = options->our_time / 16;
time -= time / 32 + 125;
- root->stop = 0;
data.root = root;
data.time = time;
data.time0 = moonfish_clock();
@@ -437,6 +439,7 @@
exit(1);
}
+ root->stop = 0;
moonfish_node(&root->node);
moonfish_chess(&root->chess);
@@ -454,6 +457,11 @@
void moonfish_stop(struct moonfish_root *root)
{root->stop = 1;
+}
+
+void moonfish_unstop(struct moonfish_root *root)
+{+ root->stop = 0;
}
#endif
--
⑨