Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
primenumber committed Nov 27, 2023
1 parent 6746cbd commit ac3a728
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
25 changes: 12 additions & 13 deletions src/engine/midgame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use futures::StreamExt;
use num_cpus;
use std::cmp::max;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::sync::{Arc, Mutex};
use std::thread;

struct YBWCContext {
Expand Down Expand Up @@ -239,7 +239,7 @@ fn simplified_abdada_young(
res: &mut i8,
best: &mut Option<Hand>,
depth: i8,
cs_hash: &Arc<RwLock<HashMap<Board, usize>>>,
cs_hash: &Arc<Mutex<HashMap<Board, usize>>>,
) -> Option<(i8, Option<Hand>, SolveStat)> {
if defer_search(next, cs_hash) {
deffered.push((pos, next));
Expand Down Expand Up @@ -279,7 +279,7 @@ fn simplified_abdada_body(
(mut alpha, beta): (i8, i8),
passed: bool,
depth: i8,
cs_hash: &Arc<RwLock<HashMap<Board, usize>>>,
cs_hash: &Arc<Mutex<HashMap<Board, usize>>>,
) -> (i8, Option<Hand>, SolveStat) {
let v = move_ordering_impl(solve_obj, board, None);
let mut stat = SolveStat::one();
Expand Down Expand Up @@ -365,7 +365,7 @@ fn simplified_abdada_intro(
(mut alpha, mut beta): (i8, i8),
passed: bool,
depth: i8,
cs_hash: &Arc<RwLock<HashMap<Board, usize>>>,
cs_hash: &Arc<Mutex<HashMap<Board, usize>>>,
) -> (i8, Option<Hand>, SolveStat) {
let rem = popcnt(board.empty());
if depth >= solve_obj.params.ybwc_depth_limit || rem < solve_obj.params.ybwc_empties_limit {
Expand Down Expand Up @@ -396,8 +396,8 @@ fn simplified_abdada_intro(
(res, best, stat)
}

fn start_search(board: Board, cs_hash: &Arc<RwLock<HashMap<Board, usize>>>) {
let mut locked_table = cs_hash.write().unwrap();
fn start_search(board: Board, cs_hash: &Arc<Mutex<HashMap<Board, usize>>>) {
let mut locked_table = cs_hash.lock().unwrap();
match locked_table.get_mut(&board) {
Some(nproc) => *nproc += 1,
None => {
Expand All @@ -406,8 +406,8 @@ fn start_search(board: Board, cs_hash: &Arc<RwLock<HashMap<Board, usize>>>) {
}
}

fn finish_search(board: Board, cs_hash: &Arc<RwLock<HashMap<Board, usize>>>) {
let mut locked_table = cs_hash.write().unwrap();
fn finish_search(board: Board, cs_hash: &Arc<Mutex<HashMap<Board, usize>>>) {
let mut locked_table = cs_hash.lock().unwrap();
match locked_table.get_mut(&board) {
Some(nproc) => {
*nproc -= 1;
Expand All @@ -421,22 +421,21 @@ fn finish_search(board: Board, cs_hash: &Arc<RwLock<HashMap<Board, usize>>>) {
}
}

fn defer_search(board: Board, cs_hash: &Arc<RwLock<HashMap<Board, usize>>>) -> bool {
cs_hash.read().unwrap().contains_key(&board)
fn defer_search(board: Board, cs_hash: &Arc<Mutex<HashMap<Board, usize>>>) -> bool {
cs_hash.lock().unwrap().contains_key(&board)
}

pub fn simplified_abdada(
solve_obj: &mut SolveObj,
_sub_solver: &SubSolver,
board: Board,
(alpha, beta): (i8, i8),
passed: bool,
depth: i8,
) -> (i8, Option<Hand>, SolveStat) {
thread::scope(|s| {
let mut handles = Vec::new();
let cs_hash = Arc::new(RwLock::new(HashMap::new()));
for _ in 0..num_cpus::get() {
let cs_hash = Arc::new(Mutex::new(HashMap::new()));
for _ in 0..num_cpus::get_physical() {
let mut solve_obj = solve_obj.clone();
let cs_hash = cs_hash.clone();
handles.push(s.spawn(move || {
Expand Down
5 changes: 2 additions & 3 deletions src/engine/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,13 @@ pub fn move_ordering_impl(solve_obj: &mut SolveObj, board: Board, _old_best: Opt

pub fn solve(
solve_obj: &mut SolveObj,
worker_urls: &[String],
_worker_urls: &[String],
board: Board,
(alpha, beta): (i8, i8),
passed: bool,
depth: i8,
) -> (i8, Option<Hand>, SolveStat) {
let sub_solver = SubSolver::new(worker_urls);
simplified_abdada(solve_obj, &sub_solver, board, (alpha, beta), passed, depth)
simplified_abdada(solve_obj, board, (alpha, beta), passed, depth)
}

pub async fn solve_with_move(board: Board, solve_obj: &mut SolveObj, sub_solver: &Arc<SubSolver>) -> Hand {
Expand Down

0 comments on commit ac3a728

Please sign in to comment.