1 /* This is a master for the "distributed" engine. It receives connections
2 * from slave machines, sends them gtp commands, then aggregates the
3 * results. It can also act as a proxy for the logs of all slave machines.
4 * The slave machines must run with engine "uct" (not "distributed").
5 * The master sends the pachi-genmoves gtp command to each slave,
6 * gets as replies a list of candidate moves, their number of playouts
7 * and their value. The master then picks the most popular move. */
9 /* The master trusts the majority of slaves for time control:
10 * it picks the move when half the slaves have replied, except
11 * when the allowed time is already passed. In this case the
12 * master picks among the available replies, or waits for just
13 * one reply if there is none yet. */
15 /* This first version does not send tree updates between slaves,
16 * but it has fault tolerance. If a slave is out of sync, the master
17 * sends it the whole command history. */
19 /* Pass me arguments like a=b,c=d,...
20 * Supported arguments:
21 * slave_port=SLAVE_PORT slaves connect to this port; this parameter is mandatory.
22 * max_slaves=MAX_SLAVES default 100
23 * slaves_quit=0|1 quit gtp command also sent to slaves, default false.
24 * proxy_port=PROXY_PORT slaves optionally send their logs to this port.
25 * Warning: with proxy_port, the master stderr mixes the logs of all
26 * machines but you can separate them again:
27 * slave logs: sed -n '/< .*:/s/.*< /< /p' logfile
28 * master logs: perl -0777 -pe 's/<[ <].*:.*\n//g' logfile
31 /* A configuration without proxy would have one master run on masterhost as:
32 * zzgo -e distributed slave_port=1234
33 * and N slaves running as:
34 * zzgo -e uct -g masterhost:1234 slave
36 * zzgo -e distributed slave_port=1234,proxy_port=1235
37 * zzgo -e uct -g masterhost:1234 -l masterhost:1235 slave
38 * If the master itself runs on a machine other than that running gogui,
39 * gogui-twogtp, kgsGtp or cgosGtp, it can redirect its gtp port:
40 * zzgo -e distributed -g 10000 slave_port=1234,proxy_port=1235
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <arpa/inet.h>
68 #include "distributed/distributed.h"
70 /* Internal engine state. */
76 struct move my_last_move
;
77 struct move_stats my_last_stats
;
80 #define get_value(value, color) \
81 ((color) == S_BLACK ? (value) : 1 - (value))
83 /* Max size for one reply or slave log. */
86 /* Max size of all gtp commands for one game */
87 #define CMDS_SIZE (40*MAX_GAMELEN)
89 /* All gtp commands for current game separated by \n */
90 char gtp_cmds
[CMDS_SIZE
];
92 /* Latest gtp command sent to slaves. */
95 /* Number of active slave machines working for this master. */
96 int active_slaves
= 0;
98 /* Number of replies to last gtp command already received. */
101 /* All replies to latest gtp command are in gtp_replies[0..reply_count-1]. */
104 /* Mutex protecting gtp_cmds, gtp_cmd, active_slaves, reply_count & gtp_replies */
105 pthread_mutex_t slave_lock
= PTHREAD_MUTEX_INITIALIZER
;
107 /* Condition signaled when a new gtp command is available. */
108 static pthread_cond_t cmd_cond
= PTHREAD_COND_INITIALIZER
;
110 /* Condition signaled when reply_count increases. */
111 static pthread_cond_t reply_cond
= PTHREAD_COND_INITIALIZER
;
113 /* Mutex protecting stderr. Must not be held at same time as slave_lock. */
114 pthread_mutex_t log_lock
= PTHREAD_MUTEX_INITIALIZER
;
116 /* Absolute time when this program was started.
117 * For debugging only. */
120 /* Write the time, client address, prefix, and string s to stderr atomically.
121 * s should end with a \n */
123 logline(struct in_addr
*client
, char *prefix
, char *s
)
125 double now
= time_now();
126 char addr
[INET_ADDRSTRLEN
];
128 inet_ntop(AF_INET
, client
, addr
, sizeof(addr
));
132 pthread_mutex_lock(&log_lock
);
133 fprintf(stderr
, "%s%15s %9.3f: %s", prefix
, addr
, now
- start_time
, s
);
134 pthread_mutex_unlock(&log_lock
);
137 /* Thread opening a connection on the given socket and copying input
138 * from there to stderr. */
140 proxy_thread(void *arg
)
142 int proxy_sock
= (long)arg
;
143 assert(proxy_sock
>= 0);
145 struct in_addr client
;
146 int conn
= open_server_connection(proxy_sock
, &client
);
147 FILE *f
= fdopen(conn
, "r");
149 while (fgets(buf
, BSIZE
, f
)) {
150 logline(&client
, "< ", buf
);
156 /* Main loop of a slave thread.
157 * Send the current command to the slave machine and wait for a reply.
158 * Resend the whole command history if the slave machine is out of sync.
159 * Returns when the connection with the slave machine is cut.
160 * slave_lock is held on both entry and exit of this function. */
162 slave_loop(FILE *f
, struct in_addr client
, char *buf
, bool resend
)
164 char *to_send
= gtp_cmd
;
168 while (cmd_id
== reply_id
&& !resend
) {
169 // Wait for a new gtp command.
170 pthread_cond_wait(&cmd_cond
, &slave_lock
);
172 cmd_id
= atoi(gtp_cmd
);
176 /* Command available, send it to slave machine.
177 * If slave was out of sync, send all the history. */
178 assert(to_send
&& gtp_cmd
);
179 strncpy(buf
, to_send
, CMDS_SIZE
);
180 cmd_id
= atoi(gtp_cmd
);
182 pthread_mutex_unlock(&slave_lock
);
184 logline(&client
, ">>", buf
);
188 /* Read the reply, which always ends with \n\n
189 * The slave machine sends "=id reply" or "?id reply"
190 * with id == cmd_id if it is in sync. */
194 while (fgets(line
, buf
+ CMDS_SIZE
- line
, f
) && *line
!= '\n') {
196 logline(&client
, "<<", line
);
197 if (reply_id
< 0 && (*line
== '=' || *line
== '?') && isdigit(line
[1]))
198 reply_id
= atoi(line
+1);
199 line
+= strlen(line
);
202 pthread_mutex_lock(&slave_lock
);
203 if (*line
!= '\n') return;
204 // Make sure we are still in sync:
205 cmd_id
= atoi(gtp_cmd
);
206 if (reply_id
== cmd_id
&& *buf
== '=') {
208 gtp_replies
[reply_count
++] = buf
;
209 pthread_cond_signal(&reply_cond
);
211 /* The slave was out of sync or had an incorrect board.
212 * Send the whole command history without wait.
213 * The slave will send a single reply with the
214 * id of the last command. */
218 logline(&client
, "? ", "Resending all history\n");
223 /* Thread sending gtp commands to one slave machine, and
224 * reading replies. If a slave machine dies, this thread waits
225 * for a connection from another slave. */
227 slave_thread(void *arg
)
229 int slave_sock
= (long)arg
;
230 assert(slave_sock
>= 0);
231 char slave_buf
[CMDS_SIZE
];
235 /* Wait for a connection from any slave. */
236 struct in_addr client
;
237 int conn
= open_server_connection(slave_sock
, &client
);
239 FILE *f
= fdopen(conn
, "r+");
241 logline(&client
, "= ", "new slave\n");
243 /* Minimal check of the slave identity. */
245 if (!fgets(slave_buf
, sizeof(slave_buf
), f
)
246 || strncasecmp(slave_buf
, "= Pachi", 7)
247 || !fgets(slave_buf
, sizeof(slave_buf
), f
)
248 || strcmp(slave_buf
, "\n")) {
249 logline(&client
, "? ", "bad slave\n");
254 pthread_mutex_lock(&slave_lock
);
256 slave_loop(f
, client
, slave_buf
, resend
);
258 assert(active_slaves
> 0);
260 pthread_mutex_unlock(&slave_lock
);
264 logline(&client
, "= ", "lost slave\n");
269 /* Create a new gtp command for all slaves. The slave lock is held
270 * upon entry and upon return, so the command will actually be
271 * sent when the lock is released. The last command is overwritten
272 * if gtp_cmd points to a non-empty string. cmd is a single word;
273 * args has all arguments and is empty or has a trailing \n */
275 update_cmd(struct board
*b
, char *cmd
, char *args
)
278 /* To make sure the slaves are in sync, we ignore the original id
279 * and use the board number plus some random bits as gtp id.
280 * Make sure the new command has a new id otherwise slaves
282 static int gtp_id
= -1;
284 int moves
= is_reset(cmd
) ? 0 : b
->moves
;
286 /* fast_random() is 16-bit only so the multiplication can't overflow. */
287 id
= force_reply(moves
+ fast_random(65535) * DIST_GAMELEN
);
288 } while (id
== gtp_id
);
290 snprintf(gtp_cmd
, gtp_cmds
+ CMDS_SIZE
- gtp_cmd
, "%d %s %s",
291 id
, cmd
, *args
? args
: "\n");
295 /* Wait for slave replies until we get at least 50% of the
296 * slaves or the given absolute time (if non zero) is passed.
297 * If we get 50% of the slaves, we wait another 0.5s to get
298 * as many slaves as possible while not wasting time waiting
299 * for stuck or dead slaves.
300 * The replies are returned in gtp_replies[0..reply_count-1]
301 * slave_lock is held on entry and on return. */
303 get_replies(double time_limit
)
305 #define EXTRA_TIME 0.5
306 while (reply_count
== 0 || reply_count
< active_slaves
) {
307 if (time_limit
&& reply_count
> 0) {
310 ts
.tv_nsec
= (int)(modf(time_limit
, &sec
)*1000000000.0);
311 ts
.tv_sec
= (int)sec
;
312 pthread_cond_timedwait(&reply_cond
, &slave_lock
, &ts
);
314 pthread_cond_wait(&reply_cond
, &slave_lock
);
316 if (reply_count
== 0) continue;
317 if (reply_count
>= active_slaves
) break;
318 double now
= time_now();
319 if (time_limit
&& now
>= time_limit
) break;
320 if (reply_count
>= active_slaves
/ 2
321 && (!time_limit
|| now
+ EXTRA_TIME
< time_limit
))
322 time_limit
= now
+ EXTRA_TIME
;
324 assert(reply_count
> 0);
327 /* Dispatch a new gtp command to all slaves.
328 * The slave lock must not be held upon entry and is released upon return.
329 * args is empty or ends with '\n' */
330 static enum parse_code
331 distributed_notify(struct engine
*e
, struct board
*b
, int id
, char *cmd
, char *args
, char **reply
)
333 struct distributed
*dist
= e
->data
;
335 if ((!strcasecmp(cmd
, "quit") && !dist
->slaves_quit
)
336 || !strcasecmp(cmd
, "uct_genbook")
337 || !strcasecmp(cmd
, "uct_dumpbook")
338 || !strcasecmp(cmd
, "kgs-chat"))
341 pthread_mutex_lock(&slave_lock
);
343 // Clear the history when a new game starts:
344 if (!gtp_cmd
|| is_gamestart(cmd
)) {
347 /* Preserve command history for new slaves.
348 * To indicate that the slave should only reply to
349 * the last command we force the id of previous
350 * commands to be just the move number. */
351 int id
= prevent_reply(atoi(gtp_cmd
));
352 int len
= strspn(gtp_cmd
, "0123456789");
354 snprintf(buf
, sizeof(buf
), "%0*d", len
, id
);
355 memcpy(gtp_cmd
, buf
, len
);
357 gtp_cmd
+= strlen(gtp_cmd
);
360 if (!strcasecmp(cmd
, "genmove")) {
361 cmd
= "pachi-genmoves";
362 } else if (!strcasecmp(cmd
, "kgs-genmove_cleanup")) {
363 cmd
= "pachi-genmoves_cleanup";
364 } else if (!strcasecmp(cmd
, "final_score")) {
365 cmd
= "final_status_list";
368 // Let the slaves send the new gtp command:
369 update_cmd(b
, cmd
, args
);
370 pthread_cond_broadcast(&cmd_cond
);
372 /* Wait for replies here except for specific commands
373 * handled by the engine later. If we don't wait, we run
374 * the risk of getting out of sync with most slaves and
375 * sending complete command history too frequently. */
376 if (strcasecmp(cmd
, "pachi-genmoves")
377 && strcasecmp(cmd
, "pachi-genmoves_cleanup")
378 && strcasecmp(cmd
, "final_status_list"))
381 pthread_mutex_unlock(&slave_lock
);
385 /* pachi-genmoves returns a line "=id total_playouts threads[ reserved]" then a list of lines
386 * "coord playouts value". Keep this function in sync with uct_notify().
387 * Return the move with most playouts, its average value, and stats for debugging.
388 * slave_lock is held on entry and on return. */
390 select_best_move(struct board
*b
, struct move_stats
*best_stats
,
391 int *total_playouts
, int *total_threads
)
393 assert(reply_count
> 0);
395 /* +2 for pass and resign. */
396 struct move_stats
*stats
= alloca((board_size2(b
)+2) * sizeof(struct move_stats
));
397 memset(stats
, 0, (board_size2(b
)+2) * sizeof(*stats
));
400 coord_t best_move
= pass
;
401 int best_playouts
= -1;
402 *total_playouts
= *total_threads
= 0;
404 for (int reply
= 0; reply
< reply_count
; reply
++) {
405 char *r
= gtp_replies
[reply
];
406 int id
, playouts
, threads
;
407 if (sscanf(r
, "=%d %d %d", &id
, &playouts
, &threads
) != 3) continue;
408 *total_playouts
+= playouts
;
409 *total_threads
+= threads
;
410 // Skip the rest of the firt line if any (allow future extensions)
415 while (r
&& sscanf(++r
, "%63s %d %f", move
, &s
.playouts
, &s
.value
) == 3) {
416 coord_t
*c
= str2coord(move
, board_size(b
));
417 stats_add_result(&stats
[*c
], s
.value
, s
.playouts
);
418 if (stats
[*c
].playouts
> best_playouts
) {
419 best_playouts
= stats
[*c
].playouts
;
426 *best_stats
= stats
[best_move
];
430 /* Time control is mostly done by the slaves, so we use default values here. */
431 #define FUSEKI_END 20
432 #define YOSE_START 40
435 distributed_genmove(struct engine
*e
, struct board
*b
, struct time_info
*ti
, enum stone color
, bool pass_all_alive
)
437 struct distributed
*dist
= e
->data
;
438 double start
= time_now();
440 /* If we do not have time constraints we just wait for
441 * slaves to reply as they have been configured by default. */
443 if (ti
->period
!= TT_NULL
&& ti
->dim
== TD_WALLTIME
) {
444 struct time_stop stop
;
445 time_stop_conditions(ti
, b
, FUSEKI_END
, YOSE_START
, &stop
);
446 time_limit
= ti
->len
.t
.timer_start
+ stop
.worst
.time
;
449 pthread_mutex_lock(&slave_lock
);
450 get_replies(time_limit
);
451 int replies
= reply_count
;
453 int playouts
, threads
;
454 dist
->my_last_move
.color
= color
;
455 dist
->my_last_move
.coord
= select_best_move(b
, &dist
->my_last_stats
, &playouts
, &threads
);
457 /* Tell the slaves to commit to the selected move, overwriting
458 * the last "pachi-genmoves" in the command history. */
460 char *coord
= coord2str(dist
->my_last_move
.coord
, b
);
461 snprintf(args
, sizeof(args
), "%s %s\n", stone2str(color
), coord
);
462 update_cmd(b
, "play", args
);
463 pthread_cond_broadcast(&cmd_cond
);
464 pthread_mutex_unlock(&slave_lock
);
468 enum stone color
= dist
->my_last_move
.color
;
469 double time
= time_now() - start
+ 0.000001; /* avoid divide by zero */
470 snprintf(buf
, sizeof(buf
),
471 "GLOBAL WINNER is %s %s with score %1.4f (%d/%d games)\n"
472 "genmove in %0.2fs (%d games/s, %d games/s/slave, %d games/s/thread)\n",
473 stone2str(color
), coord
, get_value(dist
->my_last_stats
.value
, color
),
474 dist
->my_last_stats
.playouts
, playouts
, time
,
475 (int)(playouts
/time
), (int)(playouts
/time
/replies
),
476 (int)(playouts
/time
/threads
));
477 logline(NULL
, "*** ", buf
);
480 return coord_copy(dist
->my_last_move
.coord
);
484 distributed_chat(struct engine
*e
, struct board
*b
, char *cmd
)
486 struct distributed
*dist
= e
->data
;
487 static char reply
[BSIZE
];
489 cmd
+= strspn(cmd
, " \n\t");
490 if (!strncasecmp(cmd
, "winrate", 7)) {
491 enum stone color
= dist
->my_last_move
.color
;
492 snprintf(reply
, BSIZE
, "In %d playouts at %d machines, %s %s can win with %.2f%% probability.",
493 dist
->my_last_stats
.playouts
, active_slaves
, stone2str(color
),
494 coord2sstr(dist
->my_last_move
.coord
, b
),
495 100 * get_value(dist
->my_last_stats
.value
, color
));
502 scmp(const void *p1
, const void *p2
)
504 return strcasecmp(*(char * const *)p1
, *(char * const *)p2
);
508 distributed_dead_group_list(struct engine
*e
, struct board
*b
, struct move_queue
*mq
)
510 pthread_mutex_lock(&slave_lock
);
513 /* Find the most popular reply. */
514 qsort(gtp_replies
, reply_count
, sizeof(char *), scmp
);
518 for (int reply
= 1; reply
< reply_count
; reply
++) {
519 if (!strcmp(gtp_replies
[reply
], gtp_replies
[reply
-1])) {
524 if (count
> best_count
) {
530 /* Pick the first move of each line as group. */
531 char *dead
= gtp_replies
[best_reply
];
532 dead
= strchr(dead
, ' '); // skip "id "
533 while (dead
&& *++dead
!= '\n') {
534 coord_t
*c
= str2coord(dead
, board_size(b
));
537 dead
= strchr(dead
, '\n');
539 pthread_mutex_unlock(&slave_lock
);
542 static struct distributed
*
543 distributed_state_init(char *arg
, struct board
*b
)
545 struct distributed
*dist
= calloc(1, sizeof(struct distributed
));
547 dist
->max_slaves
= 100;
549 char *optspec
, *next
= arg
;
552 next
+= strcspn(next
, ",");
553 if (*next
) { *next
++ = 0; } else { *next
= 0; }
555 char *optname
= optspec
;
556 char *optval
= strchr(optspec
, '=');
557 if (optval
) *optval
++ = 0;
559 if (!strcasecmp(optname
, "slave_port") && optval
) {
560 dist
->slave_port
= strdup(optval
);
561 } else if (!strcasecmp(optname
, "proxy_port") && optval
) {
562 dist
->proxy_port
= strdup(optval
);
563 } else if (!strcasecmp(optname
, "max_slaves") && optval
) {
564 dist
->max_slaves
= atoi(optval
);
565 } else if (!strcasecmp(optname
, "slaves_quit")) {
566 dist
->slaves_quit
= !optval
|| atoi(optval
);
568 fprintf(stderr
, "distributed: Invalid engine argument %s or missing value\n", optname
);
573 gtp_replies
= calloc(dist
->max_slaves
, sizeof(char *));
575 if (!dist
->slave_port
) {
576 fprintf(stderr
, "distributed: missing slave_port\n");
579 int slave_sock
= port_listen(dist
->slave_port
, dist
->max_slaves
);
581 for (int id
= 0; id
< dist
->max_slaves
; id
++) {
582 pthread_create(&thread
, NULL
, slave_thread
, (void *)(long)slave_sock
);
585 if (dist
->proxy_port
) {
586 int proxy_sock
= port_listen(dist
->proxy_port
, dist
->max_slaves
);
587 for (int id
= 0; id
< dist
->max_slaves
; id
++) {
588 pthread_create(&thread
, NULL
, proxy_thread
, (void *)(long)proxy_sock
);
595 engine_distributed_init(char *arg
, struct board
*b
)
597 start_time
= time_now();
598 struct distributed
*dist
= distributed_state_init(arg
, b
);
599 struct engine
*e
= calloc(1, sizeof(struct engine
));
600 e
->name
= "Distributed Engine";
601 e
->comment
= "I'm playing the distributed engine. When I'm losing, I will resign, "
602 "if I think I win, I play until you pass. "
603 "Anyone can send me 'winrate' in private chat to get my assessment of the position.";
604 e
->notify
= distributed_notify
;
605 e
->genmove
= distributed_genmove
;
606 e
->dead_group_list
= distributed_dead_group_list
;
607 e
->chat
= distributed_chat
;
609 // Keep the threads and the open socket connections:
610 e
->keep_on_clear
= true;