Skip to content

Commit

Permalink
Reduce memory consumption of continuation history
Browse files Browse the repository at this point in the history
No functional change for standard chess.
  • Loading branch information
ianfab committed May 19, 2019
1 parent e662cf6 commit 08bbbda
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
14 changes: 9 additions & 5 deletions src/movepick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

#include "movepick.h"

int history_slot(Piece pc) {
return pc == NO_PIECE ? 0 : (type_of(pc) == KING ? PIECE_SLOTS - 1 : type_of(pc) % (PIECE_SLOTS - 1)) + color_of(pc) * PIECE_SLOTS;
}

namespace {

enum Stages {
Expand Down Expand Up @@ -112,10 +116,10 @@ void MovePicker::score() {

else if (Type == QUIETS)
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
+ (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
+ (*continuationHistory[1])[pos.moved_piece(m)][to_sq(m)]
+ (*continuationHistory[3])[pos.moved_piece(m)][to_sq(m)]
+ (*continuationHistory[5])[pos.moved_piece(m)][to_sq(m)] / 2;
+ (*continuationHistory[0])[history_slot(pos.moved_piece(m))][to_sq(m)]
+ (*continuationHistory[1])[history_slot(pos.moved_piece(m))][to_sq(m)]
+ (*continuationHistory[3])[history_slot(pos.moved_piece(m))][to_sq(m)]
+ (*continuationHistory[5])[history_slot(pos.moved_piece(m))][to_sq(m)] / 2;

else // Type == EVASIONS
{
Expand All @@ -124,7 +128,7 @@ void MovePicker::score() {
- Value(type_of(pos.moved_piece(m)));
else
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
+ (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
+ (*continuationHistory[0])[history_slot(pos.moved_piece(m))][to_sq(m)]
- (1 << 28);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ template <typename T, int D, int Size>
struct Stats<T, D, Size> : public std::array<StatsEntry<T, D>, Size> {};

/// In stats table, D=0 means that the template parameter is not used
enum StatsParams { NOT_USED = 0 };
enum StatsParams { NOT_USED = 0, PIECE_SLOTS = 8 };


/// ButterflyHistory records how often quiet moves have been successful or
Expand All @@ -101,8 +101,9 @@ typedef Stats<int16_t, 29952, PIECE_NB, SQUARE_NB> PieceToHistory;
/// ContinuationHistory is the combined history of a given pair of moves, usually
/// the current one given a previous one. The nested history table is based on
/// PieceToHistory instead of ButterflyBoards.
typedef Stats<PieceToHistory, NOT_USED, PIECE_NB, SQUARE_NB> ContinuationHistory;
typedef Stats<PieceToHistory, NOT_USED, 2 * PIECE_SLOTS, SQUARE_NB> ContinuationHistory;

int history_slot(Piece pc);

/// MovePicker class is used to pick one pseudo legal move at a time from the
/// current position. The most important method is next_move(), which returns a
Expand Down
18 changes: 9 additions & 9 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ namespace {
probCutCount++;

ss->currentMove = move;
ss->continuationHistory = &thisThread->continuationHistory[pos.moved_piece(move)][to_sq(move)];
ss->continuationHistory = &thisThread->continuationHistory[history_slot(pos.moved_piece(move))][to_sq(move)];

assert(depth >= 5 * ONE_PLY);

Expand Down Expand Up @@ -998,8 +998,8 @@ namespace {

// Countermoves based pruning (~20 Elo)
if ( lmrDepth < 3 + ((ss-1)->statScore > 0 || (ss-1)->moveCount == 1)
&& (*contHist[0])[movedPiece][to_sq(move)] < CounterMovePruneThreshold
&& (*contHist[1])[movedPiece][to_sq(move)] < CounterMovePruneThreshold)
&& (*contHist[0])[history_slot(movedPiece)][to_sq(move)] < CounterMovePruneThreshold
&& (*contHist[1])[history_slot(movedPiece)][to_sq(move)] < CounterMovePruneThreshold)
continue;

// Futility pruning: parent node (~2 Elo)
Expand Down Expand Up @@ -1031,7 +1031,7 @@ namespace {

// Update the current move (this must be done after singular extension search)
ss->currentMove = move;
ss->continuationHistory = &thisThread->continuationHistory[movedPiece][to_sq(move)];
ss->continuationHistory = &thisThread->continuationHistory[history_slot(movedPiece)][to_sq(move)];

// Step 15. Make the move
pos.do_move(move, st, givesCheck);
Expand Down Expand Up @@ -1075,9 +1075,9 @@ namespace {
r -= 2 * ONE_PLY;

ss->statScore = thisThread->mainHistory[us][from_to(move)]
+ (*contHist[0])[movedPiece][to_sq(move)]
+ (*contHist[1])[movedPiece][to_sq(move)]
+ (*contHist[3])[movedPiece][to_sq(move)]
+ (*contHist[0])[history_slot(movedPiece)][to_sq(move)]
+ (*contHist[1])[history_slot(movedPiece)][to_sq(move)]
+ (*contHist[3])[history_slot(movedPiece)][to_sq(move)]
- 4000;

// Decrease/increase reduction by comparing opponent's stat score (~10 Elo)
Expand Down Expand Up @@ -1420,7 +1420,7 @@ namespace {
}

ss->currentMove = move;
ss->continuationHistory = &thisThread->continuationHistory[pos.moved_piece(move)][to_sq(move)];
ss->continuationHistory = &thisThread->continuationHistory[history_slot(pos.moved_piece(move))][to_sq(move)];

// Make and search the move
pos.do_move(move, st, givesCheck);
Expand Down Expand Up @@ -1507,7 +1507,7 @@ namespace {

for (int i : {1, 2, 4, 6})
if (is_ok((ss-i)->currentMove))
(*(ss-i)->continuationHistory)[pc][to] << bonus;
(*(ss-i)->continuationHistory)[history_slot(pc)][to] << bonus;
}


Expand Down

0 comments on commit 08bbbda

Please sign in to comment.