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

Stacksize #287

Merged
merged 3 commits into from
May 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/RubiChess.h
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,15 @@ typedef struct {
extern NnueType NnueReady;


struct NnueNetwork {
alignas(64) clipped_t input[NnueFtOutputdims];
int32_t hidden1_values[32];
int32_t hidden2_values[32];
clipped_t hidden1_clipped[32];
clipped_t hidden2_clipped[32];
int32_t out_value;
};

class NnueLayer
{
public:
Expand Down Expand Up @@ -1189,8 +1198,8 @@ enum MoveSelector_State { INITSTATE, HASHMOVESTATE, TACTICALINITSTATE, TACTICALS

class MoveSelector
{
chessposition *pos;
public:
chessposition *pos;
int state;
chessmovelist* captures;
chessmovelist* quiets;
Expand All @@ -1201,8 +1210,6 @@ class MoveSelector
int legalmovenum;
bool onlyGoodCaptures;
int16_t *cmptr[CMPLIES];

public:
void SetPreferredMoves(chessposition *p); // for quiescence move selector
void SetPreferredMoves(chessposition *p, uint16_t hshm, uint32_t kllm1, uint32_t kllm2, uint32_t counter, int excludemove);
uint32_t next();
Expand Down Expand Up @@ -1371,7 +1378,12 @@ class chessposition
#ifdef NNUE
NnueAccumulator accumulator[MAXDEPTH];
DirtyPiece dirtypiece[MAXDEPTH];
NnueNetwork network;
#endif
uint32_t quietMoves[MAXDEPTH][MAXMOVELISTLENGTH];
uint32_t tacticalMoves[MAXDEPTH][MAXMOVELISTLENGTH];
MoveSelector moveSelector[MAXDEPTH];
MoveSelector extensionMoveSelector[MAXDEPTH];
bool w2m();
void BitboardSet(int index, PieceCode p);
void BitboardClear(int index, PieceCode p);
Expand Down
3 changes: 1 addition & 2 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2295,15 +2295,14 @@ int chessposition::getBestPossibleCapture()
void MoveSelector::SetPreferredMoves(chessposition *p)
{
pos = p;
hashmove = 0;
killermove1 = killermove2 = 0;
if (!p->isCheckbb)
{
onlyGoodCaptures = true;
state = TACTICALINITSTATE;
}
else
{
onlyGoodCaptures = false;
state = EVASIONINITSTATE;
pos->getCmptr(&cmptr[0]);
}
Expand Down
11 changes: 0 additions & 11 deletions src/nnue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,22 +349,11 @@ template <NnueType Nt> void chessposition::Transform(clipped_t *output)
}


struct NnueNetwork {
alignas(64) clipped_t input[NnueFtOutputdims];
int32_t hidden1_values[32];
int32_t hidden2_values[32];
clipped_t hidden1_clipped[32];
clipped_t hidden2_clipped[32];
int32_t out_value;
};

eval NnueValueScale = 47;


template <NnueType Nt> int chessposition::NnueGetEval()
{
NnueNetwork network;

Transform<Nt>(network.input);
NnueHd1->Propagate(network.input, network.hidden1_values);
NnueCl1->Propagate(network.hidden1_values, network.hidden1_clipped);
Expand Down
62 changes: 30 additions & 32 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,16 @@ int chessposition::getQuiescence(int alpha, int beta, int depth)

prepareStack();

MoveSelector ms = {};
ms.SetPreferredMoves(this);
MoveSelector* ms = &moveSelector[ply];
memset(ms, 0, sizeof(MoveSelector));
ms->SetPreferredMoves(this);
STATISTICSINC(qs_loop_n);

uint32_t bestcode = 0;
int eval_type = HASHALPHA;
uint32_t mc;

while ((mc = ms.next()))
while ((mc = ms->next()))
{
if (Pt != NoPrune && !myIsCheck && staticeval + materialvalue[GETCAPTURE(mc) >> 1] + sps.deltapruningmargin <= alpha)
{
Expand All @@ -328,7 +329,7 @@ int chessposition::getQuiescence(int alpha, int beta, int depth)
continue;

STATISTICSINC(qs_moves);
ms.legalmovenum++;
ms->legalmovenum++;
score = -getQuiescence<Pt>(-beta, -alpha, depth - 1);
unplayMove(mc);
if (score > bestscore)
Expand Down Expand Up @@ -358,7 +359,7 @@ int chessposition::getQuiescence(int alpha, int beta, int depth)
copyPositionTuneSet(&targetpts, &myev[0], &this->pts, &this->ev[0]);
#endif

if (myIsCheck && !ms.legalmovenum)
if (myIsCheck && !ms->legalmovenum)
// It's a mate
return SCOREBLACKWINS + ply;

Expand Down Expand Up @@ -645,18 +646,17 @@ int chessposition::alphabeta(int alpha, int beta, int depth)
// Reset killers for child ply
killer[ply + 1][0] = killer[ply + 1][1] = 0;

MoveSelector ms = {};
ms.SetPreferredMoves(this, hashmovecode, killer[ply][0], killer[ply][1], counter, excludeMove);
MoveSelector* ms = (excludeMove ? &extensionMoveSelector[ply] : &moveSelector[ply]);
memset(ms, 0, sizeof(MoveSelector));
ms->SetPreferredMoves(this, hashmovecode, killer[ply][0], killer[ply][1], counter, excludeMove);
STATISTICSINC(moves_loop_n);

int legalMoves = 0;
int quietsPlayed = 0;
uint32_t quietMoves[MAXMOVELISTLENGTH];
int tacticalPlayed = 0;
uint32_t tacticalMoves[MAXMOVELISTLENGTH];
while ((mc = ms.next()))
while ((mc = ms->next()))
{
ms.legalmovenum++;
ms->legalmovenum++;
#ifdef SDEBUG
bool isDebugMove = (debugMove.code == mc);
SDEBUGDO(isDebugMove, pvmovenum[ply] = legalMoves + 1;);
Expand All @@ -675,7 +675,7 @@ int chessposition::alphabeta(int alpha, int beta, int depth)
&& quietsPlayed > lmptable[positionImproved][depth])
{
// Proceed to next moveselector state manually to save some time
ms.state++;
ms->state++;
STATISTICSINC(moves_pruned_lmp);
SDEBUGDO(isDebugMove, pvaborttype[ply] = PVA_LMPRUNED;);
continue;
Expand All @@ -695,15 +695,15 @@ int chessposition::alphabeta(int alpha, int beta, int depth)
&& !isCheckbb
&& depth <= sps.seeprunemaxdepth
&& bestscore > (Pt == Prune ? NOSCORE : -SCORETBWININMAXPLY)
&& ms.state >= QUIETSTATE
&& ms->state >= QUIETSTATE
&& !see(mc, sps.seeprunemarginperdepth * depth * (ISTACTICAL(mc) ? depth : sps.seeprunequietfactor)))
{
STATISTICSINC(moves_pruned_badsee);
SDEBUGDO(isDebugMove, pvaborttype[ply] = PVA_SEEPRUNED;);
continue;
}

int stats = !ISTACTICAL(mc) ? getHistory(mc, ms.cmptr) : getTacticalHst(mc);
int stats = !ISTACTICAL(mc) ? getHistory(mc, ms->cmptr) : getTacticalHst(mc);
int extendMove = 0;
int pc = GETPIECE(mc);
int to = GETCORRECTTO(mc);
Expand Down Expand Up @@ -746,9 +746,9 @@ int chessposition::alphabeta(int alpha, int beta, int depth)
STATISTICSINC(extend_endgame);
extendMove = 1;
}
else if(!ISTACTICAL(mc) && ms.cmptr[0] && ms.cmptr[1])
else if(!ISTACTICAL(mc) && ms->cmptr[0] && ms->cmptr[1])
{
if (ms.cmptr[0][pc * 64 + to] > he_threshold && ms.cmptr[1][pc * 64 + to] > he_threshold)
if (ms->cmptr[0][pc * 64 + to] > he_threshold && ms->cmptr[1][pc * 64 + to] > he_threshold)
{
STATISTICSINC(extend_history);
extendMove = 1;
Expand Down Expand Up @@ -806,8 +806,8 @@ int chessposition::alphabeta(int alpha, int beta, int depth)
// Prune moves with bad counter move history
if (Pt != MatePrune
&& !ISTACTICAL(mc) && effectiveDepth < 4
&& ms.cmptr[0] && ms.cmptr[0][pc * 64 + to] < 0
&& ms.cmptr[1] && ms.cmptr[1][pc * 64 + to] < 0)
&& ms->cmptr[0] && ms->cmptr[0][pc * 64 + to] < 0
&& ms->cmptr[1] && ms->cmptr[1][pc * 64 + to] < 0)
{
SDEBUGDO(isDebugMove, pvaborttype[ply] = PVA_BADHISTORYPRUNED;);
continue;
Expand All @@ -820,7 +820,7 @@ int chessposition::alphabeta(int alpha, int beta, int depth)
SDEBUGDO(isDebugMove, debugMovePlayed = true;);
STATISTICSINC(moves_played[(bool)ISTACTICAL(mc)]);

LegalMoves[ply] = ms.legalmovenum;
LegalMoves[ply] = ms->legalmovenum;
SDEBUGDO(isDebugMove, pvadditionalinfo[ply-1] = ""; );

if (reduction)
Expand Down Expand Up @@ -872,9 +872,9 @@ int chessposition::alphabeta(int alpha, int beta, int depth)
{
if (!ISTACTICAL(mc))
{
updateHistory(mc, ms.cmptr, depth * depth);
updateHistory(mc, ms->cmptr, depth * depth);
for (int i = 0; i < quietsPlayed; i++)
updateHistory(quietMoves[i], ms.cmptr, -(depth * depth));
updateHistory(quietMoves[mstop][i], ms->cmptr, -(depth * depth));

// Killermove
if (killer[ply][0] != mc)
Expand All @@ -891,7 +891,7 @@ int chessposition::alphabeta(int alpha, int beta, int depth)
{
updateTacticalHst(mc, depth * depth);
for (int i = 0; i < tacticalPlayed; i++)
updateTacticalHst(tacticalMoves[i], -(depth * depth));
updateTacticalHst(tacticalMoves[mstop][i], -(depth * depth));
}

STATISTICSINC(moves_fail_high);
Expand All @@ -911,9 +911,9 @@ int chessposition::alphabeta(int alpha, int beta, int depth)
}

if (!ISTACTICAL(mc))
quietMoves[quietsPlayed++] = mc;
quietMoves[mstop][quietsPlayed++] = mc;
else
tacticalMoves[tacticalPlayed++] = mc;
tacticalMoves[mstop][tacticalPlayed++] = mc;
}

if (legalMoves == 0)
Expand Down Expand Up @@ -1037,12 +1037,10 @@ int chessposition::rootsearch(int alpha, int beta, int depth, int inWindowLast,
staticevalstack[mstop] = staticeval;

int quietsPlayed = 0;
uint32_t quietMoves[MAXMOVELISTLENGTH];
int tacticalPlayed = 0;
uint32_t tacticalMoves[MAXMOVELISTLENGTH];

// FIXME: Dummy move selector for now; only used to pass null cmptr to updateHistory
MoveSelector ms = {};
MoveSelector *ms = &moveSelector[0];

for (int i = 0; i < rootmovelist.length; i++)
{
Expand Down Expand Up @@ -1105,9 +1103,9 @@ int chessposition::rootsearch(int alpha, int beta, int depth, int inWindowLast,
}

if (!ISTACTICAL(m->code))
quietMoves[quietsPlayed++] = m->code;
quietMoves[0][quietsPlayed++] = m->code;
else
tacticalMoves[tacticalPlayed++] = m->code;
tacticalMoves[0][tacticalPlayed++] = m->code;

if ((isMultiPV && score <= bestmovescore[lastmoveindex])
|| (!isMultiPV && score <= bestscore))
Expand Down Expand Up @@ -1183,9 +1181,9 @@ int chessposition::rootsearch(int alpha, int beta, int depth, int inWindowLast,
// Killermove
if (!ISTACTICAL(m->code))
{
updateHistory(m->code, ms.cmptr, depth * depth);
updateHistory(m->code, ms->cmptr, depth * depth);
for (int q = 0; q < quietsPlayed - 1; q++)
updateHistory(quietMoves[q], ms.cmptr, -(depth * depth));
updateHistory(quietMoves[0][q], ms->cmptr, -(depth * depth));

if (killer[0][0] != m->code)
{
Expand All @@ -1197,7 +1195,7 @@ int chessposition::rootsearch(int alpha, int beta, int depth, int inWindowLast,
{
updateTacticalHst(m->code, depth * depth);
for (int t = 0; t < tacticalPlayed - 1; t++)
updateTacticalHst(tacticalMoves[t], -(depth * depth));
updateTacticalHst(tacticalMoves[0][t], -(depth * depth));

}
tp.addHash(hash, beta, staticeval, HASHBETA, effectiveDepth, (uint16_t)m->code);
Expand Down
36 changes: 18 additions & 18 deletions src/tbprobe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,12 @@ static int probe_ab(int alpha, int beta, int *success, chessposition *pos)

// Generate (at least) all legal captures including (under)promotions.
// It is OK to generate more, as long as they are filtered out below.
chessmovelist movelist;
chessmovelist *movelist = &pos->captureslist[pos->ply];
pos->prepareStack();
movelist.length = CreateMovelist<ALL>(pos, &movelist.move[0]);
for (int i = 0; i < movelist.length; i++)
movelist->length = CreateMovelist<ALL>(pos, &movelist->move[0]);
for (int i = 0; i < movelist->length; i++)
{
uint32_t mc = movelist.move[i].code;
uint32_t mc = movelist->move[i].code;
if (ISCAPTURE(mc) || ISPROMOTION(mc) || pos->isCheckbb)
{
if (pos->playMove(mc))
Expand Down Expand Up @@ -382,16 +382,16 @@ int probe_wdl(int *success, chessposition *pos)
int i;

// Generate (at least) all legal captures including (under)promotions.
chessmovelist movelist;
chessmovelist *movelist = &pos->captureslist[pos->ply];
pos->prepareStack();
movelist.length = CreateMovelist<ALL>(pos, &movelist.move[0]);
movelist->length = CreateMovelist<ALL>(pos, &movelist->move[0]);

// We do capture resolution, letting best_cap keep track of the best
// capture without ep rights and letting best_ep keep track of still
// better ep captures if they exist.
for (i = 0; i < movelist.length; i++)
for (i = 0; i < movelist->length; i++)
{
uint32_t mc = movelist.move[i].code;
uint32_t mc = movelist->move[i].code;
if (ISCAPTURE(mc))
{
if (pos->playMove(mc))
Expand Down Expand Up @@ -447,9 +447,9 @@ int probe_wdl(int *success, chessposition *pos)
// Now handle the stalemate case.
if (best_ep > -3 && v == 0) {
// Check for stalemate in the position with ep captures.
for (i = 0; i < movelist.length; i++)
for (i = 0; i < movelist->length; i++)
{
uint32_t mc = movelist.move[i].code;
uint32_t mc = movelist->move[i].code;
if (ISEPCAPTURE(mc))
continue;

Expand All @@ -460,7 +460,7 @@ int probe_wdl(int *success, chessposition *pos)
}
}

if (i == movelist.length) { // Stalemate detected.
if (i == movelist->length) { // Stalemate detected.
*success = 2;
return best_ep;
}
Expand Down Expand Up @@ -504,7 +504,7 @@ static int wdl_to_dtz[] = {
//
int probe_dtz(int *success, chessposition *pos)
{
chessmovelist movelist;
chessmovelist *movelist = &pos->captureslist[pos->ply];

int wdl = probe_wdl(success, pos);
if (*success == 0)
Expand All @@ -523,11 +523,11 @@ int probe_dtz(int *success, chessposition *pos)
// Generate at least all legal non-capturing pawn moves
// including non-capturing promotions.
pos->prepareStack();
movelist.length = CreateMovelist<ALL>(pos, &movelist.move[0]);
movelist->length = CreateMovelist<ALL>(pos, &movelist->move[0]);

for (int i = 0; i < movelist.length; i++)
for (int i = 0; i < movelist->length; i++)
{
uint32_t mc = movelist.move[i].code;
uint32_t mc = movelist->move[i].code;
if (ISCAPTURE(mc) || (GETPIECE(mc) >> 1) != PAWN)
continue;

Expand Down Expand Up @@ -567,14 +567,14 @@ int probe_dtz(int *success, chessposition *pos)
// In case of mate, this will cause -1 to be returned.
best = wdl_to_dtz[wdl + 2];
pos->prepareStack();
movelist.length = CreateMovelist<ALL>(pos, &movelist.move[0]);
movelist->length = CreateMovelist<ALL>(pos, &movelist->move[0]);
}
for (int i = 0; i < movelist.length; i++)
for (int i = 0; i < movelist->length; i++)
{
// We can skip pawn moves and captures.
// If wdl > 0, we already caught them. If wdl < 0, the initial value
// of best already takes account of them.
uint32_t mc = movelist.move[i].code;
uint32_t mc = movelist->move[i].code;
if (ISCAPTURE(mc) || (GETPIECE(mc) >> 1) == PAWN)
continue;

Expand Down