Using smol executor

This commit is contained in:
vandomej 2021-10-23 13:27:03 -07:00
parent 433fd04836
commit 3b89c8299b
3 changed files with 48 additions and 32 deletions

View file

@ -25,5 +25,7 @@ anyhow = "1.0"
rand = "0.8.4" rand = "0.8.4"
log = "0.4.14" log = "0.4.14"
env_logger = "0.9.0" env_logger = "0.9.0"
tokio = { version = "1.12.0", features = ["full"] } futures = "0.3.17"
futures = "0.3.17" smol = "1.2.5"
num_cpus = "1.13.0"
easy-parallel = "3.1.0"

View file

@ -18,32 +18,51 @@ use test_state::TestState;
/// ///
/// Use the -h, --h, or --help flag to see usage syntax. /// Use the -h, --h, or --help flag to see usage syntax.
/// TODO /// TODO
#[tokio::main] fn main() -> anyhow::Result<()> {
async fn main() -> anyhow::Result<()> {
env_logger::init(); env_logger::init();
info!("Starting"); info!("Starting");
let now = Instant::now(); let now = Instant::now();
// Command line arguments are parsed with the clap crate. And this program uses // Obtainning number of threads to use
// the yaml method with clap. let num_threads = num_cpus::get().max(1);
let yaml = load_yaml!("../../cli.yml"); let ex = smol::Executor::new();
let matches = App::from_yaml(yaml).get_matches(); let (signal, shutdown) = smol::channel::unbounded::<()>();
// Checking that the first argument <DIRECTORY> is a valid directory // Create an executor thread pool.
let file_path = matches.value_of(gemla::constants::args::FILE).unwrap(); let (_, result): (
let mut gemla = log_error(Gemla::<TestState>::new( Vec<Result<(), smol::channel::RecvError>>,
&PathBuf::from(file_path), Result<(), gemla::error::Error>,
GemlaConfig { ) = easy_parallel::Parallel::new()
generations_per_node: 1, .each(0..num_threads, |_| {
overwrite: false, smol::future::block_on(ex.run(shutdown.recv()))
}, })
))?; .finish(|| {
smol::block_on(async {
drop(signal);
log_error(gemla.simulate(100).await)?; // Command line arguments are parsed with the clap crate. And this program uses
// the yaml method with clap.
let yaml = load_yaml!("../../cli.yml");
let matches = App::from_yaml(yaml).get_matches();
// let mut f = std::fs::File::create("./test")?; // Checking that the first argument <DIRECTORY> is a valid directory
// write!(f, "{}", serde_json::to_string(&gemla.data.readonly().0)?)?; let file_path = matches.value_of(gemla::constants::args::FILE).unwrap();
let mut gemla = log_error(Gemla::<TestState>::new(
&PathBuf::from(file_path),
GemlaConfig {
generations_per_node: 1,
overwrite: false,
},
))?;
log_error(gemla.simulate(100).await)?;
Ok(())
})
});
result?;
info!("Finished in {:?}", now.elapsed()); info!("Finished in {:?}", now.elapsed());

View file

@ -91,12 +91,8 @@ where
if let Some(node) = node_to_process { if let Some(node) = node_to_process {
trace!("Adding node to process list {}", node.get_id()); trace!("Adding node to process list {}", node.get_id());
// if self.threads.len() > 5 { self.threads
// self.join_threads().await?; .insert(node.get_id(), Box::pin(Gemla::process_node(node)));
// } else {
self.threads
.insert(node.get_id(), Box::pin(Gemla::process_node(node)));
// }
} else { } else {
trace!("No node found to process, joining threads"); trace!("No node found to process, joining threads");
@ -110,13 +106,13 @@ where
async fn join_threads(&mut self) -> Result<(), Error> { async fn join_threads(&mut self) -> Result<(), Error> {
if self.threads.len() > 0 { if self.threads.len() > 0 {
trace!("Joining threads for nodes {:?}", self.threads.keys()); trace!("Joining threads for nodes {:?}", self.threads.keys());
let results = futures::future::join_all(self.threads.values_mut()).await; let results = futures::future::join_all(self.threads.values_mut()).await;
let reduced_results: Result<Vec<GeneticNodeWrapper<T>>, Error> = let reduced_results: Result<Vec<GeneticNodeWrapper<T>>, Error> =
results.into_iter().collect(); results.into_iter().collect();
self.threads.clear(); self.threads.clear();
reduced_results.and_then(|r| { reduced_results.and_then(|r| {
if !self if !self
.data .data
@ -124,13 +120,12 @@ where
{ {
warn!("Unable to find nodes to replace in tree") warn!("Unable to find nodes to replace in tree")
} }
self.data self.data
.mutate(|(d, _)| Gemla::merge_completed_nodes(d.as_mut().unwrap()))??; .mutate(|(d, _)| Gemla::merge_completed_nodes(d.as_mut().unwrap()))??;
Ok(()) Ok(())
})?; })?;
} }
Ok(()) Ok(())