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

Support UCI ponder #232

Merged
merged 3 commits into from Jul 19, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/search.c
Expand Up @@ -63,6 +63,10 @@ void InitPruningAndReductionTables() {
}
}

INLINE int StopSearch(SearchParams* params) {
return params->timeset && GetTimeMS() - params->start > min(params->alloc, params->max) && !params->pondering;
}

void* UCISearch(void* arg) {
SearchArgs* args = (SearchArgs*)arg;

Expand All @@ -80,8 +84,14 @@ void* UCISearch(void* arg) {
void BestMove(Board* board, SearchParams* params, ThreadData* threads, SearchResults* results) {
Move bestMove;
if ((bestMove = TBRootProbe(board))) {
while (params->pondering)
asm("");

printf("bestmove %s\n", MoveToStr(bestMove));
} else if ((bestMove = ProbeNoob(board))) {
while (params->pondering)
asm("");

printf("bestmove %s\n", MoveToStr(bestMove));
} else {
pthread_t pthreads[threads->count];
Expand All @@ -100,7 +110,14 @@ void BestMove(Board* board, SearchParams* params, ThreadData* threads, SearchRes
for (int i = 1; i < threads->count; i++)
pthread_join(pthreads[i], NULL);

printf("bestmove %s\n", MoveToStr(results->bestMoves[results->depth]));
while (params->pondering)
asm("");

printf("bestmove %s", MoveToStr(results->bestMoves[results->depth]));
if (results->ponderMoves[results->depth])
printf(" ponder %s", MoveToStr(results->ponderMoves[results->depth]));

printf("\n");
}
}

Expand Down Expand Up @@ -204,6 +221,7 @@ void* Search(void* arg) {
results->depth = depth;
results->scores[depth] = thread->scores[0];
results->bestMoves[depth] = thread->bestMoves[0];
results->ponderMoves[depth] = thread->pvs[0].count > 1 ? thread->pvs[0].moves[1] : NULL_MOVE;

if (!mainThread || depth < 5 || !params->timeset)
continue;
Expand Down Expand Up @@ -257,8 +275,7 @@ int Negamax(int alpha, int beta, int depth, ThreadData* thread, PV* pv) {
// Either mainthread has ended us OR we've run out of time
// this second check is more expensive and done only every 1024 nodes
// 1Mnps ~1ms
if (params->stopped ||
(!(data->nodes & 1023) && params->timeset && GetTimeMS() - params->start > min(params->alloc, params->max)))
if (params->stopped || (!(data->nodes & 1023) && StopSearch(params)))
longjmp(thread->exit, 1);

if (!isRoot) {
Expand Down Expand Up @@ -610,8 +627,7 @@ int Quiesce(int alpha, int beta, ThreadData* thread, PV* pv) {
// Either mainthread has ended us OR we've run out of time
// this second check is more expensive and done only every 1024 nodes
// 1Mnps ~1ms
if (params->stopped ||
(!(data->nodes & 1023) && params->timeset && GetTimeMS() - params->start > min(params->alloc, params->max)))
if (params->stopped || (!(data->nodes & 1023) && StopSearch(params)))
longjmp(thread->exit, 1);

// draw check
Expand Down
2 changes: 1 addition & 1 deletion src/search.h
Expand Up @@ -47,7 +47,7 @@ int Quiesce(int alpha, int beta, ThreadData* thread, PV* pv);

void PrintInfo(PV* pv, int score, ThreadData* thread, int alpha, int beta, int multiPV);
void PrintPV(PV* pv);

int MoveSearchedByMultiPV(ThreadData* thread, Move move);
int MoveSearchable(SearchParams* params, Move move);

Expand Down
2 changes: 2 additions & 0 deletions src/types.h
Expand Up @@ -202,6 +202,7 @@ typedef struct {
int movesToGo;
int stopped;
int quit;
int pondering;
int multiPV;
int searchMoves;
SimpleMoveList searchable;
Expand All @@ -211,6 +212,7 @@ typedef struct {
int depth;
Score scores[MAX_SEARCH_PLY];
Move bestMoves[MAX_SEARCH_PLY];
Move ponderMoves[MAX_SEARCH_PLY];
} SearchResults;

typedef struct {
Expand Down
8 changes: 8 additions & 0 deletions src/uci.c
Expand Up @@ -64,6 +64,7 @@ void ParseGo(char* in, SearchParams* params, Board* board, ThreadData* threads)
params->stopped = 0;
params->quit = 0;
params->multiPV = MULTI_PV;
params->pondering = 0;
params->searchMoves = 0;
params->searchable.count = 0;

Expand Down Expand Up @@ -97,6 +98,9 @@ void ParseGo(char* in, SearchParams* params, Board* board, ThreadData* threads)
if ((ptrChar = strstr(in, "depth")))
depth = min(MAX_SEARCH_PLY - 1, atoi(ptrChar + 6));

if ((ptrChar = strstr(in, "ponder")))
params->pondering = 1;

if ((ptrChar = strstr(in, "searchmoves"))) {
params->searchMoves = 1;

Expand Down Expand Up @@ -243,12 +247,16 @@ void UCILoop() {
} else if (!strncmp(in, "go", 2)) {
ParseGo(in, &searchParameters, &board, threads);
} else if (!strncmp(in, "stop", 4)) {
searchParameters.pondering = 0;
searchParameters.stopped = 1;
} else if (!strncmp(in, "quit", 4)) {
searchParameters.pondering = 0;
searchParameters.quit = 1;
break;
} else if (!strncmp(in, "uci", 3)) {
PrintUCIOptions();
} else if (!strncmp(in, "ponderhit", 9)) {
searchParameters.pondering = 0;
} else if (!strncmp(in, "board", 5)) {
PrintBoard(&board);
} else if (!strncmp(in, "eval", 4)) {
Expand Down