multithreading
This commit is contained in:
parent
c368e8da60
commit
7e6c057127
@ -3,8 +3,10 @@ use crate::{
|
|||||||
repr::{Board, CoordPair, Piece, Winner},
|
repr::{Board, CoordPair, Piece, Winner},
|
||||||
};
|
};
|
||||||
use allocative::Allocative;
|
use allocative::Allocative;
|
||||||
use indicatif::{ProgressIterator, ProgressStyle};
|
use indicatif::{ParallelProgressIterator, ProgressStyle};
|
||||||
use std::{collections::HashMap, hash::BuildHasherDefault, ops::ControlFlow};
|
use rayon::iter::IntoParallelIterator;
|
||||||
|
use rayon::prelude::*;
|
||||||
|
use std::{collections::HashMap, hash::BuildHasherDefault};
|
||||||
|
|
||||||
#[derive(Allocative)]
|
#[derive(Allocative)]
|
||||||
pub struct FutureMoves {
|
pub struct FutureMoves {
|
||||||
@ -158,29 +160,29 @@ impl FutureMoves {
|
|||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let cf = self
|
self.leaf_moves()
|
||||||
.leaf_moves()
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|&i| self.depth_of(i) == self.current_depth)
|
.filter(|&i| self.depth_of(i) == self.current_depth)
|
||||||
.collect::<Vec<usize>>()
|
.collect::<Vec<usize>>()
|
||||||
.into_iter()
|
.into_par_iter()
|
||||||
.progress_with_style(ProgressStyle::with_template(pstyle_inner).unwrap())
|
.progress_with_style(ProgressStyle::with_template(pstyle_inner).unwrap())
|
||||||
.try_for_each(|node_idx| {
|
.map(|parent_idx| (parent_idx, self.generate_children_raw(parent_idx)))
|
||||||
self.generate_children(node_idx);
|
.collect::<Vec<(usize, Vec<Move>)>>()
|
||||||
|
.into_iter()
|
||||||
|
.for_each(|(parent_idx, moves)| {
|
||||||
|
let start_idx = self.arena.len();
|
||||||
|
self.arena.extend(moves);
|
||||||
|
|
||||||
if self.arena_len() >= self.config.max_arena_size {
|
let new_indices = start_idx..self.arena.len();
|
||||||
ControlFlow::Break(())
|
self.arena[parent_idx].children.extend(new_indices);
|
||||||
} else {
|
|
||||||
ControlFlow::Continue(())
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self.prune_bad_children();
|
self.prune_bad_children();
|
||||||
|
|
||||||
if cf.is_break() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
self.current_depth += 1;
|
self.current_depth += 1;
|
||||||
|
|
||||||
|
if self.arena.len() >= self.config.max_arena_size {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,10 +202,7 @@ impl FutureMoves {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates children for a parent (`parent_idx`)
|
fn generate_children_raw(&self, parent_idx: usize) -> Vec<Move> {
|
||||||
/// Completely unchecked, the caller should be the one who tests to make sure child generation
|
|
||||||
/// hasn't already been tried on a parent
|
|
||||||
fn generate_children(&mut self, parent_idx: usize) {
|
|
||||||
let parent = &self.arena[parent_idx];
|
let parent = &self.arena[parent_idx];
|
||||||
|
|
||||||
let new_color = !parent.color;
|
let new_color = !parent.color;
|
||||||
@ -230,14 +229,11 @@ impl FutureMoves {
|
|||||||
new.push(Move::new(None, parent_board, new_color, self.agent_color));
|
new.push(Move::new(None, parent_board, new_color, self.agent_color));
|
||||||
}
|
}
|
||||||
|
|
||||||
let start_idx = self.arena.len();
|
for m in new.iter_mut() {
|
||||||
self.arena.extend(new);
|
m.parent = Some(parent_idx);
|
||||||
|
|
||||||
let new_indices = start_idx..self.arena.len();
|
|
||||||
|
|
||||||
for child_idx in new_indices {
|
|
||||||
self.set_parent_child(parent_idx, child_idx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
new
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given an index from `self.arena`, what depth is it at? 0-indexed
|
/// Given an index from `self.arena`, what depth is it at? 0-indexed
|
||||||
|
|||||||
@ -32,7 +32,7 @@ fn main() {
|
|||||||
min_arena_depth: 14,
|
min_arena_depth: 14,
|
||||||
top_k_children: 2,
|
top_k_children: 2,
|
||||||
up_to_minus: 10,
|
up_to_minus: 10,
|
||||||
max_arena_size: 100_000_000,
|
max_arena_size: 400_000_000,
|
||||||
do_prune: true,
|
do_prune: true,
|
||||||
print: true,
|
print: true,
|
||||||
children_eval_method: Default::default(),
|
children_eval_method: Default::default(),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user