From 2038cb93eeb076be8f5db4ae438a43033e34c1a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Wed, 30 Oct 2024 23:39:03 +0100 Subject: [PATCH] Add the multi_threads algorithm --- README.md | 2 ++ benches/job_scheduling.rs | 1 + src/algorithm.rs | 2 +- src/algorithm/lpt.rs | 9 +++++++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a2becb7..9d9152f 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,8 @@ algorithms in Rust and benchmark them. Let the battle begin. - `single thread`: No multi-threading or any optimization of any kind. Used as a reference. +- `multi threads`: Manual multi-threading but no optimization of any kind. Used + as a reference. - `lpt`: Manual multi-threading. Tasks are sorted so the shortest ones are executed first. This is the [longest-processing-time-first algorithm][lpt]. - `rayon`: Multi-threading provided by the `rayon` crate, but no optimization diff --git a/benches/job_scheduling.rs b/benches/job_scheduling.rs index 8d8ef53..2566dd7 100644 --- a/benches/job_scheduling.rs +++ b/benches/job_scheduling.rs @@ -6,6 +6,7 @@ use rust_job_scheduling::Task; const TEST_DATA: &[(&str, &[Task])] = &[("10 tasks", T_10), ("500 tasks", T_500)]; const TEST_FUNCS: &[(&str, &dyn Fn(&[Task]))] = &[ ("single thread", &single_thread), + ("multi threads", &multi_threads), ("lpt", &lpt), ("rayon", &rayon), ("rayon sorted", &rayon_sorted), diff --git a/src/algorithm.rs b/src/algorithm.rs index e46e616..0ef1550 100644 --- a/src/algorithm.rs +++ b/src/algorithm.rs @@ -2,6 +2,6 @@ mod lpt; mod rayon; mod single_thread; -pub use lpt::lpt; +pub use lpt::*; pub use rayon::*; pub use single_thread::single_thread; diff --git a/src/algorithm/lpt.rs b/src/algorithm/lpt.rs index 237d493..1467e5d 100644 --- a/src/algorithm/lpt.rs +++ b/src/algorithm/lpt.rs @@ -5,7 +5,16 @@ use std::thread; pub fn lpt(data: &[Task]) { let mut data = data.to_vec(); data.sort(); + run_multi(data); +} +pub fn multi_threads(data: &[Task]) { + let data = data.to_vec(); + run_multi(data); +} + +#[inline(always)] +fn run_multi(data: Vec) { let data_mt = Arc::new(Mutex::new(data)); let nb_threads = thread::available_parallelism() .map(|n| n.get())