Skip to content

Commit

Permalink
📦 NEW: Pass full ttable between searches
Browse files Browse the repository at this point in the history
  • Loading branch information
ianagbip1oti committed Dec 14, 2023
1 parent ca310c0 commit f477d20
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
12 changes: 8 additions & 4 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::options::{get_num_threads, get_policy_temperature, is_chess960};
use crate::search_tree::{MoveEdge, SearchTree};
use crate::state::State;
use crate::tablebase;
use crate::transposition_table::{LRAllocator, TranspositionTable};
use crate::transposition_table::{LRAllocator, LRTable};
use crate::uci::{read_stdin, Tokens};

const DEFAULT_MOVE_TIME_SECS: u64 = 10;
Expand Down Expand Up @@ -93,12 +93,16 @@ pub struct Search {
}

impl Search {
pub fn new(state: State, prev_table: TranspositionTable) -> Self {
let search_tree = SearchTree::new(state, TranspositionTable::empty(), prev_table);
pub fn new(state: State, table: LRTable) -> Self {
let search_tree = SearchTree::new(state, table);
Self { search_tree }
}

pub fn table(self) -> TranspositionTable {
pub fn reset_table(&mut self) {
self.search_tree.reset_table();
}

pub fn table(self) -> LRTable {
self.search_tree.table()
}

Expand Down
19 changes: 10 additions & 9 deletions src/search_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ where
}

impl SearchTree {
pub fn new(
state: State,
current_table: TranspositionTable,
previous_table: TranspositionTable,
) -> Self {
pub fn new(state: State, table: LRTable) -> Self {
let tb_hits = 0.into();

let root_table = TranspositionTable::for_root();
Expand All @@ -217,7 +213,7 @@ impl SearchTree {
)
.expect("Unable to create root node");

previous_table.lookup_into(&state, &mut root_node);
table.lookup_into_from_all(&state, &mut root_node);

Self {
root_state: state,
Expand All @@ -226,7 +222,7 @@ impl SearchTree {
cpuct_root: get_cpuct_root(),
policy_t: get_policy_temperature(),
root_table,
ttable: LRTable::new(current_table, previous_table),
ttable: table,
num_nodes: 1.into(),
playouts: 0.into(),
max_depth: 0.into(),
Expand All @@ -235,12 +231,17 @@ impl SearchTree {
}
}

pub fn reset_table(&mut self) {
self.ttable = LRTable::empty();
self.root_node.clear_children_links();
}

fn flip_tables(&self) {
self.ttable.flip_tables();
}

pub fn table(self) -> TranspositionTable {
self.ttable.table()
pub fn table(self) -> LRTable {
self.ttable
}

pub fn num_nodes(&self) -> usize {
Expand Down
29 changes: 14 additions & 15 deletions src/transposition_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ impl TranspositionTable {
Self { table, arena }
}

pub fn is_empty(&self) -> bool {
self.table.is_empty()
}

pub fn arena(&self) -> &Arena {
&self.arena
}
Expand Down Expand Up @@ -67,7 +63,7 @@ impl TranspositionTable {
.map(|v| unsafe { &*v.load(Ordering::Relaxed) })
}

pub fn lookup_into(&self, state: &State, dest: &mut PositionNode) {
pub fn lookup_into(&self, state: &State, dest: &mut PositionNode) -> bool {
if let Some(src) = self.lookup(state) {
dest.set_flag(src.flag());

Expand All @@ -77,6 +73,9 @@ impl TranspositionTable {
for i in 0..lhs.len().min(rhs.len()) {
lhs[i].replace(&rhs[i]);
}
true
} else {
false
}
}
}
Expand All @@ -98,6 +97,10 @@ impl LRTable {
}
}

pub fn empty() -> Self {
Self::new(TranspositionTable::empty(), TranspositionTable::empty())
}

pub fn is_arena_full(&self) -> bool {
self.current_table().arena().full()
}
Expand All @@ -114,6 +117,12 @@ impl LRTable {
self.previous_table().lookup_into(state, dest);
}

pub fn lookup_into_from_all(&self, state: &State, dest: &mut PositionNode) {
if !self.current_table().lookup_into(state, dest) {
self.previous_table().lookup_into(state, dest);
}
}

pub fn is_left_current(&self) -> bool {
self.is_left_current.load(Ordering::Relaxed)
}
Expand Down Expand Up @@ -142,16 +151,6 @@ impl LRTable {
);
}

pub fn table(self) -> TranspositionTable {
if self.left.is_empty() {
self.right
} else if self.right.is_empty() || self.is_left_current() {
self.left
} else {
self.right
}
}

pub fn flip_lock(&self) -> &Mutex<()> {
&self.flip_lock
}
Expand Down
15 changes: 9 additions & 6 deletions src/uci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::search::Search;
use crate::search_tree::print_size_list;
use crate::state::State;
use crate::tablebase::set_tablebase_directory;
use crate::transposition_table::TranspositionTable;
use crate::transposition_table::LRTable;

pub type Tokens<'a> = SplitWhitespace<'a>;

Expand All @@ -18,7 +18,7 @@ const ENGINE_AUTHOR: &str = "Princess Lana";
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");

pub fn main() {
let mut search = Search::new(State::default(), TranspositionTable::empty());
let mut search = Search::new(State::default(), LRTable::empty());

let mut next_line: Option<String> = None;

Expand All @@ -38,11 +38,11 @@ pub fn main() {
let option = UciOption::parse(tokens);

if let Some(opt) = option {
opt.set();
opt.set(&mut search);
}
}
"ucinewgame" => {
search = Search::new(State::default(), TranspositionTable::empty());
search = Search::new(State::default(), LRTable::empty());
}
"position" => {
if let Some(state) = State::from_tokens(tokens) {
Expand Down Expand Up @@ -129,15 +129,18 @@ impl UciOption {
&self.value
}

pub fn set(&self) {
pub fn set(&self, search: &mut Search) {
match self.name().as_str() {
"syzygypath" => {
if let Some(path) = self.value() {
set_tablebase_directory(path);
}
}
"threads" => self.set_option(set_num_threads),
"hash" => self.set_option(set_hash_size_mb),
"hash" => {
self.set_option(set_hash_size_mb);
search.reset_table();
}
"cpuct" => self.set_option(set_cpuct),
"cpuctroot" => self.set_option(set_cpuct_root),
"cvisitsselection" => self.set_option(set_cvisits_selection),
Expand Down

0 comments on commit f477d20

Please sign in to comment.