Skip to content

Commit

Permalink
Merge pull request #45 from primenumber/limit-cpus
Browse files Browse the repository at this point in the history
Limit number of CPUs
  • Loading branch information
primenumber authored May 4, 2024
2 parents 31f09be + 9c0b88d commit 5ce34d2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn search(board: Board, think_time_limit: u128, solve_obj: &mut SolveObj, sub_so
solve_obj.cache_gen += 1;
if board.empty().count_ones() <= 18 {
let mut solve_obj = solve_obj.clone();
solve_with_move(board, &mut solve_obj, &sub_solver.clone())
solve_with_move(board, &mut solve_obj, &sub_solver.clone(), None)
} else {
let start = Instant::now();
let timer = Timer {
Expand Down
4 changes: 3 additions & 1 deletion src/engine/midgame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,14 @@ pub fn simplified_abdada(
(alpha, beta): (i8, i8),
passed: bool,
depth: i8,
num_threads: Option<usize>,
) -> (i8, Option<Hand>, SolveStat) {
thread::scope(|s| {
let mut handles = Vec::new();
let cs_hash = Arc::new(DashSet::new());
let finished = Arc::new(AtomicBool::new(false));
for _ in 0..num_cpus::get() {
let num_threads = num_threads.unwrap_or(num_cpus::get());
for _ in 0..num_threads {
let solve_obj = solve_obj.clone();
let cs_hash = cs_hash.clone();
let finished = finished.clone();
Expand Down
23 changes: 20 additions & 3 deletions src/engine/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,24 +272,33 @@ pub fn move_ordering_impl(solve_obj: &mut SolveObj, board: Board, _old_best: Opt
}
}

// num_threads: number of searching threads, use number of cpus when None
pub fn solve(
solve_obj: &mut SolveObj,
_worker_urls: &[String],
board: Board,
(alpha, beta): (i8, i8),
passed: bool,
depth: i8,
num_threads: Option<usize>,
) -> (i8, Option<Hand>, SolveStat) {
simplified_abdada(solve_obj, board, (alpha, beta), passed, depth)
simplified_abdada(solve_obj, board, (alpha, beta), passed, depth, num_threads)
}

pub fn solve_with_move(board: Board, solve_obj: &mut SolveObj, _sub_solver: &Arc<SubSolver>) -> Hand {
// num_threads: number of searching threads, use number of cpus when None
pub fn solve_with_move(
board: Board,
solve_obj: &mut SolveObj,
_sub_solver: &Arc<SubSolver>,
num_threads: Option<usize>,
) -> Hand {
if let Some(best) = simplified_abdada(
solve_obj,
board,
(-(BOARD_SIZE as i8), BOARD_SIZE as i8),
false,
0,
num_threads,
)
.1
{
Expand All @@ -299,7 +308,15 @@ pub fn solve_with_move(board: Board, solve_obj: &mut SolveObj, _sub_solver: &Arc
let mut result = -65;
for pos in board.mobility() {
let next = board.play(pos).unwrap();
let res = -simplified_abdada(solve_obj, next, (-(BOARD_SIZE as i8), -result), false, 0).0;
let res = -simplified_abdada(
solve_obj,
next,
(-(BOARD_SIZE as i8), -result),
false,
0,
num_threads,
)
.0;
if res > result {
result = res;
best_pos = Some(pos);
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn solve_ffo(name: &str, index: &mut usize, solve_obj: &mut SolveObj, workers: &
(-(BOARD_SIZE as i8), BOARD_SIZE as i8),
false,
0,
None,
);
let end = start.elapsed();
let milli_seconds = end.as_millis() + 1; // ceil up, avoid zero-division
Expand Down
8 changes: 4 additions & 4 deletions src/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn play(matches: &ArgMatches) -> Board {
best
} else {
let mut solve_obj = solve_obj.clone();
solve_with_move(board.board, &mut solve_obj, &sub_solver)
solve_with_move(board.board, &mut solve_obj, &sub_solver, None)
};
solve_obj.cache_gen += 1;
best
Expand Down Expand Up @@ -120,7 +120,7 @@ pub fn self_play(matches: &ArgMatches) -> Board {
best
} else {
let mut solve_obj = solve_obj.clone();
solve_with_move(board.board, &mut solve_obj, &sub_solver)
solve_with_move(board.board, &mut solve_obj, &sub_solver, None)
};
solve_obj.cache_gen += 1;
let hand = best;
Expand Down Expand Up @@ -173,7 +173,7 @@ fn self_play_worker(mut solve_obj: SolveObj, sub_solver: Arc<SubSolver>, initial
best
} else {
let mut obj = solve_obj.clone();
solve_with_move(board.board, &mut obj, &sub_solver)
solve_with_move(board.board, &mut obj, &sub_solver, Some(1))
};
solve_obj.cache_gen += 1;
let hand = best;
Expand Down Expand Up @@ -349,7 +349,7 @@ pub fn codingame(_matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>
best
} else {
let mut solve_obj = solve_obj.clone();
solve_with_move(board.board, &mut solve_obj, &sub_solver)
solve_with_move(board.board, &mut solve_obj, &sub_solver, None)
};
solve_obj.cache_gen += 1;
match best {
Expand Down

0 comments on commit 5ce34d2

Please sign in to comment.