Add the multi_threads algorithm
This commit is contained in:
parent
7fc15efb1a
commit
2038cb93ee
4 changed files with 13 additions and 1 deletions
|
@ -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
|
- `single thread`: No multi-threading or any optimization of any kind. Used as
|
||||||
a reference.
|
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
|
- `lpt`: Manual multi-threading. Tasks are sorted so the shortest ones are
|
||||||
executed first. This is the [longest-processing-time-first algorithm][lpt].
|
executed first. This is the [longest-processing-time-first algorithm][lpt].
|
||||||
- `rayon`: Multi-threading provided by the `rayon` crate, but no optimization
|
- `rayon`: Multi-threading provided by the `rayon` crate, but no optimization
|
||||||
|
|
|
@ -6,6 +6,7 @@ use rust_job_scheduling::Task;
|
||||||
const TEST_DATA: &[(&str, &[Task])] = &[("10 tasks", T_10), ("500 tasks", T_500)];
|
const TEST_DATA: &[(&str, &[Task])] = &[("10 tasks", T_10), ("500 tasks", T_500)];
|
||||||
const TEST_FUNCS: &[(&str, &dyn Fn(&[Task]))] = &[
|
const TEST_FUNCS: &[(&str, &dyn Fn(&[Task]))] = &[
|
||||||
("single thread", &single_thread),
|
("single thread", &single_thread),
|
||||||
|
("multi threads", &multi_threads),
|
||||||
("lpt", &lpt),
|
("lpt", &lpt),
|
||||||
("rayon", &rayon),
|
("rayon", &rayon),
|
||||||
("rayon sorted", &rayon_sorted),
|
("rayon sorted", &rayon_sorted),
|
||||||
|
|
|
@ -2,6 +2,6 @@ mod lpt;
|
||||||
mod rayon;
|
mod rayon;
|
||||||
mod single_thread;
|
mod single_thread;
|
||||||
|
|
||||||
pub use lpt::lpt;
|
pub use lpt::*;
|
||||||
pub use rayon::*;
|
pub use rayon::*;
|
||||||
pub use single_thread::single_thread;
|
pub use single_thread::single_thread;
|
||||||
|
|
|
@ -5,7 +5,16 @@ use std::thread;
|
||||||
pub fn lpt(data: &[Task]) {
|
pub fn lpt(data: &[Task]) {
|
||||||
let mut data = data.to_vec();
|
let mut data = data.to_vec();
|
||||||
data.sort();
|
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<Task>) {
|
||||||
let data_mt = Arc::new(Mutex::new(data));
|
let data_mt = Arc::new(Mutex::new(data));
|
||||||
let nb_threads = thread::available_parallelism()
|
let nb_threads = thread::available_parallelism()
|
||||||
.map(|n| n.get())
|
.map(|n| n.get())
|
||||||
|
|
Loading…
Reference in a new issue