Compare commits
5 Commits
thread
...
7e65fbc205
Author | SHA1 | Date | |
---|---|---|---|
7e65fbc205 | |||
cf77a78aa6 | |||
7ae6604e10 | |||
f7a6e582ed | |||
e1debcc3ae |
34
Makefile
34
Makefile
@@ -39,7 +39,9 @@ OBJ := $(addprefix $(OBJDIR)/,$(SRC_FN:.c=.o))
|
|||||||
TSTSRC := $(wildcard $(TSTDIR)/*.c)
|
TSTSRC := $(wildcard $(TSTDIR)/*.c)
|
||||||
|
|
||||||
LIB := br_$(shell uname -m) # library name
|
LIB := br_$(shell uname -m) # library name
|
||||||
LIBS := $(strip -l$(LIB))
|
LIB := $(strip $(LIB))
|
||||||
|
LIBFILE := ${BRLIBDIR}/lib$(strip ${LIB}).a
|
||||||
|
LIBS := $(addprefix -l,$(strip $(LIB)))
|
||||||
|
|
||||||
DEP_FN := $(SRC_FN)
|
DEP_FN := $(SRC_FN)
|
||||||
DEP := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d))
|
DEP := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d))
|
||||||
@@ -102,7 +104,7 @@ endif
|
|||||||
# if no version, use last commit and date.
|
# if no version, use last commit and date.
|
||||||
# else, if last commit != last tag commit, add commit and date to version number
|
# else, if last commit != last tag commit, add commit and date to version number
|
||||||
ifeq ($(VERSION),)
|
ifeq ($(VERSION),)
|
||||||
VERSION := $(build)-$(COMMIT)-$(DATE)
|
VERSION := $(build)-git.$(COMMIT)-$(DATE)
|
||||||
else ifneq ($(COMMIT), $(TAG_COMMIT))
|
else ifneq ($(COMMIT), $(TAG_COMMIT))
|
||||||
VERSION := $(VERSION)-next-$(build)-$(COMMIT)-$(DATE)
|
VERSION := $(VERSION)-next-$(build)-$(COMMIT)-$(DATE)
|
||||||
endif
|
endif
|
||||||
@@ -157,22 +159,22 @@ ifeq ($(build),release)
|
|||||||
#CFLAGS += -g
|
#CFLAGS += -g
|
||||||
#CFLAGS += -ginline-points # inlined funcs debug info
|
#CFLAGS += -ginline-points # inlined funcs debug info
|
||||||
LDFLAGS += -flto
|
LDFLAGS += -flto
|
||||||
|
else ifeq ($(build),perf)
|
||||||
|
CFLAGS += -O3
|
||||||
|
CFLAGS += -ggdb0 # symbols (gdb, perf, etc.)
|
||||||
|
CFLAGS += -ginline-points # inlined funcs debug info
|
||||||
|
CFLAGS += -funroll-loops
|
||||||
else ifeq ($(build),dev)
|
else ifeq ($(build),dev)
|
||||||
CFLAGS += -Og
|
CFLAGS += -O1
|
||||||
CFLAGS += -g # symbols (gdb, perf, etc.)
|
CFLAGS += -ggdb # symbols (gdb, perf, etc.)
|
||||||
CFLAGS += -ginline-points # inlined funcs debug info
|
CFLAGS += -ginline-points # inlined funcs debug info
|
||||||
#CFLAGS += -pg # gprof
|
#CFLAGS += -pg # gprof
|
||||||
# Next one may be useful for valgrind (when invalid instructions)
|
# Next one may be useful for valgrind (when invalid instructions)
|
||||||
#CFLAGS += -mno-tbm
|
#CFLAGS += -mno-tbm
|
||||||
else ifeq ($(build),perf)
|
|
||||||
CFLAGS += -O3
|
|
||||||
CFLAGS += -g # symbols (gdb, perf, etc.)
|
|
||||||
CFLAGS += -ginline-points # inlined funcs debug info
|
|
||||||
CFLAGS += -funroll-loops
|
|
||||||
else ifeq ($(build),debug)
|
else ifeq ($(build),debug)
|
||||||
CFLAGS += -Og
|
CFLAGS += -O0 # -Og hides some variables
|
||||||
CFLAGS += -g # symbols (gdb, perf, etc.)
|
CFLAGS += -ggdb3 # symbols (including macro)
|
||||||
CFLAGS += -ginline-points # inlined funcs debug info
|
#CFLAGS += -ginline-points # inlined funcs debug info
|
||||||
# for gprof
|
# for gprof
|
||||||
#CFLAGS += -pg
|
#CFLAGS += -pg
|
||||||
# Next one may be useful for valgrind (when invalid instructions)
|
# Next one may be useful for valgrind (when invalid instructions)
|
||||||
@@ -344,6 +346,7 @@ brlib:
|
|||||||
$(info calling with build=$(build))
|
$(info calling with build=$(build))
|
||||||
$(MAKE) -e -C $(BRLIB) lib-static
|
$(MAKE) -e -C $(BRLIB) lib-static
|
||||||
unexport build
|
unexport build
|
||||||
|
|
||||||
##################################### brchess binaries
|
##################################### brchess binaries
|
||||||
.PHONY: targets cleanbin cleanbindir
|
.PHONY: targets cleanbin cleanbindir
|
||||||
|
|
||||||
@@ -355,9 +358,9 @@ cleanbin:
|
|||||||
cleanbindir:
|
cleanbindir:
|
||||||
$(call rmdir,$(BINDIR),binaries)
|
$(call rmdir,$(BINDIR),binaries)
|
||||||
|
|
||||||
$(TARGET): libs $(OBJ) | $(BINDIR)
|
$(TARGET): $(LIBFILE) $(OBJ) | $(BINDIR) libs
|
||||||
@echo linking $@.
|
@echo linking $@.
|
||||||
$(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@
|
$(CC) $(ALL_LDFLAGS) $(OBJ) -o $@
|
||||||
|
|
||||||
##################################### pre-processed (.i) and assembler (.s) output
|
##################################### pre-processed (.i) and assembler (.s) output
|
||||||
.PHONY: cleanasmcpp
|
.PHONY: cleanasmcpp
|
||||||
@@ -415,7 +418,7 @@ TEST += movedo-test perft-test tt-test
|
|||||||
PIECE_OBJS := piece.o
|
PIECE_OBJS := piece.o
|
||||||
FEN_OBJS := $(PIECE_OBJS) fen.o position.o bitboard.o board.o \
|
FEN_OBJS := $(PIECE_OBJS) fen.o position.o bitboard.o board.o \
|
||||||
hq.o attack.o hash.o init.o util.o alloc.o move.o \
|
hq.o attack.o hash.o init.o util.o alloc.o move.o \
|
||||||
eval.o eval-defs.o eval-simple.o
|
eval.o eval-defs.o eval-simple.o hist.o
|
||||||
BB_OBJS := $(FEN_OBJS)
|
BB_OBJS := $(FEN_OBJS)
|
||||||
MOVEGEN_OBJS := $(BB_OBJS) move-gen.o
|
MOVEGEN_OBJS := $(BB_OBJS) move-gen.o
|
||||||
ATTACK_OBJS := $(MOVEGEN_OBJS)
|
ATTACK_OBJS := $(MOVEGEN_OBJS)
|
||||||
@@ -490,6 +493,7 @@ wtf:
|
|||||||
@#echo LIBSRC=$(LIBSRC)
|
@#echo LIBSRC=$(LIBSRC)
|
||||||
|
|
||||||
zob:
|
zob:
|
||||||
|
echo $(LIBFILE)
|
||||||
@#$(CC) $(LDFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(LIBS) src/util.c -o util
|
@#$(CC) $(LDFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(LIBS) src/util.c -o util
|
||||||
|
|
||||||
##################################### End of multi-targets
|
##################################### End of multi-targets
|
||||||
|
@@ -16,9 +16,10 @@
|
|||||||
|
|
||||||
#include <brlib.h> /* brlib types */
|
#include <brlib.h> /* brlib types */
|
||||||
|
|
||||||
#define ONE 1ul
|
|
||||||
#define U64(const_u64) const_u64##UL
|
#define U64(const_u64) const_u64##UL
|
||||||
#define BIT(i) ( (u64) (ONE << (i)) )
|
#define BIT(i) ( U64(1) << (i) )
|
||||||
|
#define BIT_NONE ( U64(0) )
|
||||||
|
#define BIT_ALL ( ~BIT_NONE )
|
||||||
|
|
||||||
#define BOARDSIZE (8*8)
|
#define BOARDSIZE (8*8)
|
||||||
#define GAMESIZE 1024 /* max game size (512 moves) */
|
#define GAMESIZE 1024 /* max game size (512 moves) */
|
||||||
|
@@ -256,8 +256,7 @@ static const struct pst {
|
|||||||
* A1 ..... H1
|
* A1 ..... H1
|
||||||
*/
|
*/
|
||||||
[PAWN] = {
|
[PAWN] = {
|
||||||
{ /* H1 H8 */
|
{ /* midgame */
|
||||||
/* midgame */
|
|
||||||
+0, 0, 0, 0, 0, 0, 0, 0,
|
+0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
50, 50, 50, 50, 50, 50, 50, 50,
|
50, 50, 50, 50, 50, 50, 50, 50,
|
||||||
10, 10, 20, 30, 30, 20, 10, 10,
|
10, 10, 20, 30, 30, 20, 10, 10,
|
||||||
@@ -575,8 +574,8 @@ void pst_init(int set)
|
|||||||
eval_t mid_pc = piece_midval(pt);
|
eval_t mid_pc = piece_midval(pt);
|
||||||
eval_t end_pc = piece_endval(pt);
|
eval_t end_pc = piece_endval(pt);
|
||||||
for (square_t sq = 0; sq < SQUARE_NB; ++sq) {
|
for (square_t sq = 0; sq < SQUARE_NB; ++sq) {
|
||||||
eval_t mid_pst = pst->val[MIDGAME][pt][sq];
|
eval_t mid_pst = pst->val[pt][MIDGAME][sq];
|
||||||
eval_t end_pst = pst->val[ENDGAME][pt][sq];
|
eval_t end_pst = pst->val[pt][ENDGAME][sq];
|
||||||
|
|
||||||
pst_mg[BLACK][pt][sq] = (mid_pc * wmat + mid_pst * wpst) / 100;
|
pst_mg[BLACK][pt][sq] = (mid_pc * wmat + mid_pst * wpst) / 100;
|
||||||
pst_eg[BLACK][pt][sq] = (end_pc * wmat + end_pst * wpst) / 100;
|
pst_eg[BLACK][pt][sq] = (end_pc * wmat + end_pst * wpst) / 100;
|
||||||
|
@@ -202,7 +202,7 @@ int tt_create(s32 sizemb)
|
|||||||
hash_tt.bytes = hash_tt.nbuckets * sizeof(bucket_t);
|
hash_tt.bytes = hash_tt.nbuckets * sizeof(bucket_t);
|
||||||
hash_tt.mb = hash_tt.bytes / 1024 / 1024;
|
hash_tt.mb = hash_tt.bytes / 1024 / 1024;
|
||||||
|
|
||||||
hash_tt.mask = -1ull >> (64 - nbits);
|
hash_tt.mask = BIT_ALL >> (64 - nbits);
|
||||||
|
|
||||||
hash_tt.keys = safe_alloc_aligned_hugepage(hash_tt.bytes);
|
hash_tt.keys = safe_alloc_aligned_hugepage(hash_tt.bytes);
|
||||||
|
|
||||||
@@ -295,6 +295,7 @@ hentry_t *tt_probe_perft(const hkey_t key, const u16 depth)
|
|||||||
/* find key in buckets */
|
/* find key in buckets */
|
||||||
for (i = 0; i < ENTRIES_PER_BUCKET; ++i) {
|
for (i = 0; i < ENTRIES_PER_BUCKET; ++i) {
|
||||||
entry = bucket->entry + i;
|
entry = bucket->entry + i;
|
||||||
|
bug_on(entry->key && (key & hash_tt.mask) != (entry->key & hash_tt.mask));
|
||||||
if (key == entry->key && HASH_PERFT_DEPTH(entry->data) == depth) {
|
if (key == entry->key && HASH_PERFT_DEPTH(entry->data) == depth) {
|
||||||
hash_tt.hits++;
|
hash_tt.hits++;
|
||||||
/*
|
/*
|
||||||
@@ -390,7 +391,7 @@ hentry_t *tt_store_perft(const hkey_t key, const u16 depth, const u64 nodes)
|
|||||||
void tt_info()
|
void tt_info()
|
||||||
{
|
{
|
||||||
if (hash_tt.keys) {
|
if (hash_tt.keys) {
|
||||||
printf("TT: Mb:%d buckets:%'lu (bits:%u mask:%#x) entries:%'lu\n",
|
printf("TT: Mb:%d buckets:%'lu (bits:%u mask:%#lx) entries:%'lu\n",
|
||||||
hash_tt.mb, hash_tt.nbuckets, hash_tt.nbits,
|
hash_tt.mb, hash_tt.nbuckets, hash_tt.nbits,
|
||||||
hash_tt.mask, hash_tt.nkeys);
|
hash_tt.mask, hash_tt.nkeys);
|
||||||
} else {
|
} else {
|
||||||
|
@@ -84,7 +84,7 @@ typedef struct {
|
|||||||
|
|
||||||
/* internal representation */
|
/* internal representation */
|
||||||
u32 nbits; /* #buckets in bits, power of 2 */
|
u32 nbits; /* #buckets in bits, power of 2 */
|
||||||
u32 mask; /* nbuckets - 1, key mask */
|
u64 mask; /* nbuckets - 1, bucket mask */
|
||||||
|
|
||||||
/* stats - unsure about usage */
|
/* stats - unsure about usage */
|
||||||
//size_t used_buckets;
|
//size_t used_buckets;
|
||||||
|
@@ -480,3 +480,18 @@ finish:
|
|||||||
return movelist;
|
return movelist;
|
||||||
//return movelist->nmoves = moves - movelist->move;
|
//return movelist->nmoves = moves - movelist->move;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* pos_gen_legal() - generate position legal moves
|
||||||
|
* @pos: position
|
||||||
|
* @movelist: &movelist_t array to store moves
|
||||||
|
*
|
||||||
|
* Generate all @pos legal moves for player-to-move.
|
||||||
|
* @movelist is filled with the moves.
|
||||||
|
*
|
||||||
|
* @Return: movelist
|
||||||
|
*/
|
||||||
|
movelist_t *pos_gen_legal(pos_t *pos, movelist_t *movelist)
|
||||||
|
{
|
||||||
|
return pos_legal(pos, pos_gen_pseudo(pos, movelist));
|
||||||
|
}
|
||||||
|
@@ -27,5 +27,6 @@ movelist_t *pos_legal_dup(const pos_t *pos, movelist_t *pseudo, movelist_t *lega
|
|||||||
movelist_t *pos_legal(const pos_t *pos, movelist_t *list);
|
movelist_t *pos_legal(const pos_t *pos, movelist_t *list);
|
||||||
|
|
||||||
movelist_t *pos_gen_pseudo(pos_t *pos, movelist_t *movelist);
|
movelist_t *pos_gen_pseudo(pos_t *pos, movelist_t *movelist);
|
||||||
|
movelist_t *pos_gen_legal(pos_t *pos, movelist_t *movelist);
|
||||||
|
|
||||||
#endif /* MOVEGEN_H */
|
#endif /* MOVEGEN_H */
|
||||||
|
70
src/thread.c
Normal file
70
src/thread.c
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
/* thread.c - thread management.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||||
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
|
* Some rights reserved. See COPYING.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along with this
|
||||||
|
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <brlib.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
#include "thread.h"
|
||||||
|
|
||||||
|
/* Still have to decide: thread or process ?
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
thread_pool_t threadpool;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* thrd_create - initialize thrd.
|
||||||
|
*/
|
||||||
|
int thrd_create(__unused int num)
|
||||||
|
{
|
||||||
|
int fd[2];
|
||||||
|
/* shall we make a communication channel via a pipe or socket ? */
|
||||||
|
int __unused ret = socketpair(AF_LOCAL, SOCK_SEQPACKET, PF_LOCAL, fd);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* thread_init - initialize thread pool.
|
||||||
|
*/
|
||||||
|
int thread_init(int nb)
|
||||||
|
{
|
||||||
|
nb = clamp(nb, MIN_THRDS, MAX_THRDS);
|
||||||
|
|
||||||
|
/* stop unwanted threads, always keep 1 */
|
||||||
|
for (int i = nb + 1; i < threadpool.nb; ++i) {
|
||||||
|
printf("stopping thread %d - status = \n", i);
|
||||||
|
threadpool.thread[i].cmd = THRD_DO_QUIT;
|
||||||
|
}
|
||||||
|
for (int i = threadpool.nb; i < nb; ++i) {
|
||||||
|
printf("creating thread %d - status = \n", i);
|
||||||
|
thrd_create(i);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return nb;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
communication:
|
||||||
|
main thread -> thread
|
||||||
|
commands via memory
|
||||||
|
thread -> main thread
|
||||||
|
status via memory
|
||||||
|
output via pipe/socket
|
||||||
|
thread output will be output/filtered by main thread
|
||||||
|
|
||||||
|
*/
|
55
src/thread.h
Normal file
55
src/thread.h
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
/* thread.h - thread management.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||||
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
|
* Some rights reserved. See COPYING.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along with this
|
||||||
|
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef THREAD_H
|
||||||
|
#define THREAD_H
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#include <brlib.h>
|
||||||
|
|
||||||
|
#include "position.h"
|
||||||
|
|
||||||
|
#define MIN_THRDS 1
|
||||||
|
#define MAX_THRDS 16
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
THRD_DEAD,
|
||||||
|
THRD_IDLE,
|
||||||
|
THRD_WORKING,
|
||||||
|
} thread_status_t;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
/* main thread to subs */
|
||||||
|
THRD_DO_SEARCH,
|
||||||
|
THRD_DO_STOP,
|
||||||
|
THRD_DO_QUIT,
|
||||||
|
} thread_cmd_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int id;
|
||||||
|
thread_status_t status;
|
||||||
|
thread_cmd_t cmd;
|
||||||
|
int fd[2];
|
||||||
|
pos_t pos;
|
||||||
|
} thread_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int nb;
|
||||||
|
thread_t thread[MAX_THRDS + 1];
|
||||||
|
} thread_pool_t;
|
||||||
|
|
||||||
|
int thrd_create(__unused int num);
|
||||||
|
int thread_init(int nb);
|
||||||
|
|
||||||
|
#endif /* THREAD_H */
|
@@ -198,6 +198,10 @@ struct fentest {
|
|||||||
/*****************************************************
|
/*****************************************************
|
||||||
* tests from talkchess *
|
* tests from talkchess *
|
||||||
*****************************************************/
|
*****************************************************/
|
||||||
|
{ __LINE__, FEN | MOVEGEN | MOVEDO | PERFT,
|
||||||
|
"https://talkchess.com/viewtopic.php?t=21343",
|
||||||
|
"8/k7/3p4/p2P1p2/P2P1P2/8/8/K7 w - - 0 1"
|
||||||
|
},
|
||||||
{ __LINE__, FEN | MOVEGEN | MOVEDO | PERFT,
|
{ __LINE__, FEN | MOVEGEN | MOVEDO | PERFT,
|
||||||
"https://www.talkchess.com/forum3/viewtopic.php?f=7&t=71379",
|
"https://www.talkchess.com/forum3/viewtopic.php?f=7&t=71379",
|
||||||
"8/6kR/8/8/8/bq6/1rqqqqqq/K1nqnbrq b - - 0 1"
|
"8/6kR/8/8/8/bq6/1rqqqqqq/K1nqnbrq b - - 0 1"
|
||||||
|
Reference in New Issue
Block a user