Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #708

Merged
merged 3 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Weiss binary
weiss

# Chess position files
*.epd
*.pgn
Expand Down
4 changes: 2 additions & 2 deletions src/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "psqt.h"


bool chess960 = false;
bool Chess960 = false;

uint8_t SqDistance[64][64];

Expand Down Expand Up @@ -170,7 +170,7 @@ static void AddPiece(Position *pos, const Square sq, const Piece piece) {
static void InitCastlingRight(Position *pos, Color color, int file) {

if ( FileOf(kingSq(color)) != FILE_E
|| (file != FILE_A && file != FILE_H)) chess960 = true;
|| (file != FILE_A && file != FILE_H)) Chess960 = true;

Square kFrom = kingSq(color);
Square rFrom = MakeSquare(RelativeRank(color, RANK_1), file);
Expand Down
2 changes: 1 addition & 1 deletion src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ typedef struct Position {
} Position;


extern bool chess960;
extern bool Chess960;

extern const int NonPawn[PIECE_NB];

Expand Down
4 changes: 2 additions & 2 deletions src/endgame.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "endgame.h"


Endgame endgameTable[ENDGAME_TABLE_SIZE] = { 0 };
Endgame EndgameTable[ENDGAME_TABLE_SIZE] = { 0 };


// Generates a material key from a string like "KRPkr"
Expand All @@ -50,7 +50,7 @@ static void AddEndgame(const char *white, const char *black, SpecializedEval ef)

Key key = GenMaterialKey(white, black);

Endgame *eg = &endgameTable[EndgameIndex(key)];
Endgame *eg = &EndgameTable[EndgameIndex(key)];

if (eg->evalFunc != NULL) {
puts("Collision in endgame table.");
Expand Down
2 changes: 1 addition & 1 deletion src/endgame.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef struct Endgame {
} Endgame;


extern Endgame endgameTable[ENDGAME_TABLE_SIZE];
extern Endgame EndgameTable[ENDGAME_TABLE_SIZE];


INLINE int EndgameIndex(Key materialKey) {
Expand Down
2 changes: 1 addition & 1 deletion src/evaluate.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ static int ScaleFactor(const Position *pos, const int eval) {
// Calculate a static evaluation of a position
int EvalPosition(const Position *pos, PawnCache pc) {

Endgame *eg = &endgameTable[EndgameIndex(pos->materialKey)];
Endgame *eg = &EndgameTable[EndgameIndex(pos->materialKey)];

if (eg->key == pos->materialKey && eg->evalFunc != NULL)
return eg->evalFunc(pos, sideToMove);
Expand Down
2 changes: 1 addition & 1 deletion src/makemove.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static void MovePiece(Position *pos, const Square from, const Square to, const b
const PieceType pt = PieceTypeOf(piece);

assert(ValidPiece(piece));
assert(pieceOn(to) == EMPTY || (chess960 && pt == ROOK));
assert(pieceOn(to) == EMPTY || (Chess960 && pt == ROOK));

// Hash out piece on old square, in on new square
if (hash)
Expand Down
4 changes: 2 additions & 2 deletions src/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ char *MoveToStr(const Move move) {
moveStr[4] = "\0.nbrq"[PieceTypeOf(promotion(move))];

// Encode castling as KxR for chess 960
if (chess960 && moveIsCastle(move)) {
if (Chess960 && moveIsCastle(move)) {
int color = ColorOf(piece(move)) == WHITE ? WHITE_CASTLE : BLACK_CASTLE;
int side = FileOf(toSq(move)) == FILE_G ? OO : OOO;
SqToStr(RookSquare[color & side], moveStr + 2);
Expand Down Expand Up @@ -109,7 +109,7 @@ Move ParseMove(const char *str, const Position *pos) {
: pt == PAWN && str[0] != str[2] && !pieceOn(to) ? FLAG_ENPAS
: 0;

if (chess960 && pt == KING && pieceOn(to) == MakePiece(sideToMove, ROOK)) {
if (Chess960 && pt == KING && pieceOn(to) == MakePiece(sideToMove, ROOK)) {
to = RelativeSquare(sideToMove, to > from ? G1 : C1);
flag = FLAG_CASTLE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/move.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ INLINE bool CastleLegal(const Position *pos, Square to) {
if (SqAttacked(pos, PopLsb(&kingPath), !color))
return false;

return !chess960 || !(Attackers(pos, to, pieceBB(ALL) ^ BB(RookSquare[castle])) & colorBB(!color));
return !Chess960 || !(Attackers(pos, to, pieceBB(ALL) ^ BB(RookSquare[castle])) & colorBB(!color));
}

bool MoveIsPseudoLegal(const Position *pos, Move move);
Expand Down
10 changes: 5 additions & 5 deletions src/noobprobe/noobprobe.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@
#include "../query/query.h"


bool noobbook;
bool NoobBook;
int failedQueries;
int noobLimit;
int NoobLimit;


// Probes noobpwnftw's Chess Cloud Database
bool ProbeNoob(Position *pos) {

// Stop querying after 3 failures or at the specified depth
if ( !noobbook
if ( !NoobBook
|| failedQueries >= 3
|| (Limits.timelimit && Limits.maxUsage < 2000)
|| (noobLimit && pos->gameMoves > noobLimit))
|| (NoobLimit && pos->gameMoves > NoobLimit))
return false;

puts("info string NoobBook: Querying chessdb.cn for a move...");
Expand All @@ -51,7 +51,7 @@ bool ProbeNoob(Position *pos) {
if (strstr(response, "move") != response)
return failedQueries++, false;

threads->rootMoves[0].move = ParseMove(&response[5], pos);
Threads->rootMoves[0].move = ParseMove(&response[5], pos);

puts("info string NoobBook: Move received");

Expand Down
4 changes: 2 additions & 2 deletions src/noobprobe/noobprobe.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
#include "../types.h"


extern bool noobbook;
extern bool NoobBook;
extern int NoobLimit;
extern int failedQueries;
extern int noobLimit;


bool ProbeNoob(Position *pos);
2 changes: 1 addition & 1 deletion src/onlinesyzygy/onlinesyzygy.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "onlinesyzygy.h"


bool onlineSyzygy = false;
bool OnlineSyzygy = false;


// Probes lichess syzygy
Expand Down
2 changes: 1 addition & 1 deletion src/onlinesyzygy/onlinesyzygy.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "../types.h"


extern bool onlineSyzygy;
extern bool OnlineSyzygy;


bool QueryRoot(const Position *pos, unsigned *wdl, unsigned *dtz, Move *move);
4 changes: 2 additions & 2 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ void *SearchPosition(void *pos) {

// Start helper threads and begin searching
StartHelpers(IterativeDeepening);
IterativeDeepening(&threads[0]);
IterativeDeepening(&Threads[0]);

conclusion:

Expand All @@ -710,7 +710,7 @@ void *SearchPosition(void *pos) {
WaitForHelpers();

// Print conclusion
PrintConclusion(threads);
PrintConclusion(Threads);

SEARCH_STOPPED = true;
Wake();
Expand Down
4 changes: 2 additions & 2 deletions src/syzygy.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static bool SyzygyMove(const Position *pos) {
bool success =
pos->castlingRights ? false
: pieces <= TB_LARGEST ? ProbeRoot(pos, &move, &wdl, &dtz)
: onlineSyzygy && pieces <= 7 ? QueryRoot(pos, &move, &wdl, &dtz)
: OnlineSyzygy && pieces <= 7 ? QueryRoot(pos, &move, &wdl, &dtz)
: false;

if (!success) return false;
Expand All @@ -117,7 +117,7 @@ static bool SyzygyMove(const Position *pos) {
fflush(stdout);

// Set move to be printed as conclusion
threads->rootMoves[0].move = move;
Threads->rootMoves[0].move = move;

return true;
}
14 changes: 7 additions & 7 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ void Benchmark(int argc, char **argv) {
// Collect results
BenchResult *r = &results[i];
r->elapsed = TimeSince(Limits.start);
r->nodes = TotalNodes(threads);
r->score = threads->rootMoves[0].score;
r->best = threads->rootMoves[0].move;
r->nodes = TotalNodes();
r->score = Threads->rootMoves[0].score;
r->best = Threads->rootMoves[0].move;

totalElapsed += r->elapsed;
totalNodes += r->nodes;

ClearTT(threads);
ClearTT();
}

puts("======================================================");
Expand Down Expand Up @@ -179,13 +179,13 @@ void Perft(char *str) {
char *fen = strtok(NULL, "\0") ?: default_fen;

Depth depth = d ? atoi(d) : 5;
ParseFen(fen, &threads->pos);
ParseFen(fen, &Threads->pos);

printf("\nPerft starting:\nDepth : %d\nFEN : %s\n", depth, fen);
fflush(stdout);

const TimePoint start = Now();
uint64_t leafNodes = RecursivePerft(&threads->pos, depth);
uint64_t leafNodes = RecursivePerft(&Threads->pos, depth);
const TimePoint elapsed = TimeSince(start) + 1;

printf("\nPerft complete:"
Expand All @@ -197,7 +197,7 @@ void Perft(char *str) {
}

void PrintEval(Position *pos) {
printf("%d\n", EvalPositionWhitePov(pos, threads->pawnCache));
printf("%d\n", EvalPositionWhitePov(pos, Threads->pawnCache));
fflush(stdout);
}
#endif
44 changes: 22 additions & 22 deletions src/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "threads.h"


Thread *threads;
Thread *Threads;
static pthread_t *pthreads;

// Used for letting the main thread sleep without using cpu
Expand All @@ -38,16 +38,16 @@ static pthread_cond_t sleepCondition = PTHREAD_COND_INITIALIZER;
// Allocates memory for thread structs
void InitThreads(int count) {

if (threads) free(threads);
if (Threads) free(Threads);
if (pthreads) free(pthreads);

threads = calloc(count, sizeof(Thread));
Threads = calloc(count, sizeof(Thread));
pthreads = calloc(count, sizeof(pthread_t));

// Each thread knows its own index and total thread count
for (int i = 0; i < count; ++i)
threads[i].index = i,
threads[i].count = count;
Threads[i].index = i,
Threads[i].count = count;
}

// Sorts all rootmoves searched by multiPV
Expand All @@ -70,24 +70,24 @@ void SortRootMoves(Thread *thread, int multiPV) {
// Tallies the nodes searched by all threads
uint64_t TotalNodes() {
uint64_t total = 0;
for (int i = 0; i < threads->count; ++i)
total += threads[i].pos.nodes;
for (int i = 0; i < Threads->count; ++i)
total += Threads[i].pos.nodes;
return total;
}

// Tallies the tbhits of all threads
uint64_t TotalTBHits() {
uint64_t total = 0;
for (int i = 0; i < threads->count; ++i)
total += threads[i].tbhits;
for (int i = 0; i < Threads->count; ++i)
total += Threads[i].tbhits;
return total;
}

// Setup threads for a new search
void PrepareSearch(Position *pos, Move searchmoves[]) {
int rootMoveCount = LegalMoveCount(pos, searchmoves);

for (Thread *t = threads; t < threads + threads->count; ++t) {
for (Thread *t = Threads; t < Threads + Threads->count; ++t) {
memset(t, 0, offsetof(Thread, pos));
memcpy(&t->pos, pos, sizeof(Position));
t->rootMoveCount = rootMoveCount;
Expand All @@ -109,33 +109,33 @@ static bool helpersActive = false;
// Start helper threads running the provided function
void StartHelpers(void *(*func)(void *)) {
helpersActive = true;
for (int i = 1; i < threads->count; ++i)
pthread_create(&pthreads[i], NULL, func, &threads[i]);
for (int i = 1; i < Threads->count; ++i)
pthread_create(&pthreads[i], NULL, func, &Threads[i]);
}

// Wait for helper threads to finish
void WaitForHelpers() {
if (!helpersActive) return;
for (int i = 1; i < threads->count; ++i)
for (int i = 1; i < Threads->count; ++i)
pthread_join(pthreads[i], NULL);
helpersActive = false;
}

// Reset all data that isn't reset each turn
void ResetThreads() {
for (int i = 0; i < threads->count; ++i)
memset(threads[i].pawnCache, 0, sizeof(PawnCache)),
memset(threads[i].history, 0, sizeof(threads[i].history)),
memset(threads[i].pawnHistory, 0, sizeof(threads[i].pawnHistory)),
memset(threads[i].captureHistory, 0, sizeof(threads[i].captureHistory)),
memset(threads[i].continuation, 0, sizeof(threads[i].continuation));
for (int i = 0; i < Threads->count; ++i)
memset(Threads[i].pawnCache, 0, sizeof(PawnCache)),
memset(Threads[i].history, 0, sizeof(Threads[i].history)),
memset(Threads[i].pawnHistory, 0, sizeof(Threads[i].pawnHistory)),
memset(Threads[i].captureHistory, 0, sizeof(Threads[i].captureHistory)),
memset(Threads[i].continuation, 0, sizeof(Threads[i].continuation));
}

// Run the given function once in each thread
void RunWithAllThreads(void *(*func)(void *)) {
for (int i = 0; i < threads->count; ++i)
pthread_create(&pthreads[i], NULL, func, &threads[i]);
for (int i = 0; i < threads->count; ++i)
for (int i = 0; i < Threads->count; ++i)
pthread_create(&pthreads[i], NULL, func, &Threads[i]);
for (int i = 0; i < Threads->count; ++i)
pthread_join(pthreads[i], NULL);
}

Expand Down
2 changes: 1 addition & 1 deletion src/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ typedef struct Thread {
} Thread;


extern Thread *threads;
extern Thread *Threads;


void InitThreads(int threadCount);
Expand Down
12 changes: 6 additions & 6 deletions src/uci.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ static void SetOption(char *str) {
else if (OptionNameIs("Threads" )) InitThreads(IntValue);
else if (OptionNameIs("SyzygyPath" )) tb_init(optionValue);
else if (OptionNameIs("MultiPV" )) Limits.multiPV = IntValue;
else if (OptionNameIs("NoobBookLimit")) noobLimit = IntValue;
else if (OptionNameIs("NoobBook" )) noobbook = BooleanValue;
else if (OptionNameIs("UCI_Chess960" )) chess960 = BooleanValue;
else if (OptionNameIs("OnlineSyzygy" )) onlineSyzygy = BooleanValue;
else if (OptionNameIs("NoobBookLimit")) NoobLimit = IntValue;
else if (OptionNameIs("NoobBook" )) NoobBook = BooleanValue;
else if (OptionNameIs("UCI_Chess960" )) Chess960 = BooleanValue;
else if (OptionNameIs("OnlineSyzygy" )) OnlineSyzygy = BooleanValue;
else puts("info string No such option.");

fflush(stdout);
Expand Down Expand Up @@ -219,8 +219,8 @@ void PrintThinking(const Thread *thread, int alpha, int beta) {
const Position *pos = &thread->pos;

TimePoint elapsed = TimeSince(Limits.start);
uint64_t nodes = TotalNodes(thread);
uint64_t tbhits = TotalTBHits(thread);
uint64_t nodes = TotalNodes();
uint64_t tbhits = TotalTBHits();
int hashFull = HashFull();
int nps = (int)(1000 * nodes / (elapsed + 1));

Expand Down