5 Commits

Author SHA1 Message Date
7e65fbc205 change some bit macros, fix pst_init bug (thx gcc!), TT mask 64 bits 2024-09-04 20:07:19 +02:00
cf77a78aa6 Makefile: Fix dev/debug flags (issue with -Og & -g) 2024-09-04 20:01:18 +02:00
7ae6604e10 Makefile: add dependency on brlib file 2024-08-08 08:43:14 +02:00
f7a6e582ed movegen: add pos_gen_legal() to simplify code 2024-08-05 08:40:03 +02:00
e1debcc3ae create (unused) thread.[ch] files.
I am still unsure if I will go to thread or process...
2024-07-31 07:30:20 +02:00
10 changed files with 178 additions and 28 deletions

View File

@@ -39,7 +39,9 @@ OBJ := $(addprefix $(OBJDIR)/,$(SRC_FN:.c=.o))
TSTSRC := $(wildcard $(TSTDIR)/*.c)
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 := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d))
@@ -102,7 +104,7 @@ endif
# if no version, use last commit and date.
# else, if last commit != last tag commit, add commit and date to version number
ifeq ($(VERSION),)
VERSION := $(build)-$(COMMIT)-$(DATE)
VERSION := $(build)-git.$(COMMIT)-$(DATE)
else ifneq ($(COMMIT), $(TAG_COMMIT))
VERSION := $(VERSION)-next-$(build)-$(COMMIT)-$(DATE)
endif
@@ -157,22 +159,22 @@ ifeq ($(build),release)
#CFLAGS += -g
#CFLAGS += -ginline-points # inlined funcs debug info
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)
CFLAGS += -Og
CFLAGS += -g # symbols (gdb, perf, etc.)
CFLAGS += -O1
CFLAGS += -ggdb # symbols (gdb, perf, etc.)
CFLAGS += -ginline-points # inlined funcs debug info
#CFLAGS += -pg # gprof
# Next one may be useful for valgrind (when invalid instructions)
#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)
CFLAGS += -Og
CFLAGS += -g # symbols (gdb, perf, etc.)
CFLAGS += -ginline-points # inlined funcs debug info
CFLAGS += -O0 # -Og hides some variables
CFLAGS += -ggdb3 # symbols (including macro)
#CFLAGS += -ginline-points # inlined funcs debug info
# for gprof
#CFLAGS += -pg
# Next one may be useful for valgrind (when invalid instructions)
@@ -344,6 +346,7 @@ brlib:
$(info calling with build=$(build))
$(MAKE) -e -C $(BRLIB) lib-static
unexport build
##################################### brchess binaries
.PHONY: targets cleanbin cleanbindir
@@ -355,9 +358,9 @@ cleanbin:
cleanbindir:
$(call rmdir,$(BINDIR),binaries)
$(TARGET): libs $(OBJ) | $(BINDIR)
$(TARGET): $(LIBFILE) $(OBJ) | $(BINDIR) libs
@echo linking $@.
$(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@
$(CC) $(ALL_LDFLAGS) $(OBJ) -o $@
##################################### pre-processed (.i) and assembler (.s) output
.PHONY: cleanasmcpp
@@ -415,7 +418,7 @@ TEST += movedo-test perft-test tt-test
PIECE_OBJS := piece.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 \
eval.o eval-defs.o eval-simple.o
eval.o eval-defs.o eval-simple.o hist.o
BB_OBJS := $(FEN_OBJS)
MOVEGEN_OBJS := $(BB_OBJS) move-gen.o
ATTACK_OBJS := $(MOVEGEN_OBJS)
@@ -490,6 +493,7 @@ wtf:
@#echo LIBSRC=$(LIBSRC)
zob:
echo $(LIBFILE)
@#$(CC) $(LDFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(LIBS) src/util.c -o util
##################################### End of multi-targets

View File

@@ -16,9 +16,10 @@
#include <brlib.h> /* brlib types */
#define ONE 1ul
#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 GAMESIZE 1024 /* max game size (512 moves) */

View File

@@ -256,8 +256,7 @@ static const struct pst {
* A1 ..... H1
*/
[PAWN] = {
{ /* H1 H8 */
/* midgame */
{ /* midgame */
+0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
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 end_pc = piece_endval(pt);
for (square_t sq = 0; sq < SQUARE_NB; ++sq) {
eval_t mid_pst = pst->val[MIDGAME][pt][sq];
eval_t end_pst = pst->val[ENDGAME][pt][sq];
eval_t mid_pst = pst->val[pt][MIDGAME][sq];
eval_t end_pst = pst->val[pt][ENDGAME][sq];
pst_mg[BLACK][pt][sq] = (mid_pc * wmat + mid_pst * wpst) / 100;
pst_eg[BLACK][pt][sq] = (end_pc * wmat + end_pst * wpst) / 100;

View File

@@ -202,7 +202,7 @@ int tt_create(s32 sizemb)
hash_tt.bytes = hash_tt.nbuckets * sizeof(bucket_t);
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);
@@ -295,6 +295,7 @@ hentry_t *tt_probe_perft(const hkey_t key, const u16 depth)
/* find key in buckets */
for (i = 0; i < ENTRIES_PER_BUCKET; ++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) {
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()
{
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.mask, hash_tt.nkeys);
} else {

View File

@@ -76,15 +76,15 @@ typedef struct {
/* memory size in bytes/mb */
size_t bytes;
u32 mb;
u32 mb;
/* size in buckets/keys */
size_t nbuckets;
size_t nkeys; /* nbuckets * NBUCKETS */
/* internal representation */
u32 nbits; /* #buckets in bits, power of 2 */
u32 mask; /* nbuckets - 1, key mask */
u32 nbits; /* #buckets in bits, power of 2 */
u64 mask; /* nbuckets - 1, bucket mask */
/* stats - unsure about usage */
//size_t used_buckets;
@@ -106,8 +106,8 @@ typedef struct {
extern hkey_t zobrist_pieces[16][64];
extern hkey_t zobrist_castling[4 * 4 + 1];
extern hkey_t zobrist_turn; /* for black, XOR each ply */
extern hkey_t zobrist_ep[9]; /* 0-7: ep file, 8: SQUARE_NONE */
extern hkey_t zobrist_turn; /* for black, XOR each ply */
extern hkey_t zobrist_ep[9]; /* 0-7: ep file, 8: SQUARE_NONE */
extern hasht_t hash_tt; /* main transposition table */

View File

@@ -480,3 +480,18 @@ finish:
return movelist;
//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));
}

View File

@@ -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_gen_pseudo(pos_t *pos, movelist_t *movelist);
movelist_t *pos_gen_legal(pos_t *pos, movelist_t *movelist);
#endif /* MOVEGEN_H */

70
src/thread.c Normal file
View 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
View 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 */

View File

@@ -198,6 +198,10 @@ struct fentest {
/*****************************************************
* 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,
"https://www.talkchess.com/forum3/viewtopic.php?f=7&t=71379",
"8/6kR/8/8/8/bq6/1rqqqqqq/K1nqnbrq b - - 0 1"