From 85acc0c4af091b8869af4ee78bc73cb61a840903 Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Tue, 10 Mar 2020 22:27:22 +0100 Subject: [PATCH] initial commit --- .github/workflows/rust.yml | 19 ++++ .gitignore | 3 + Cargo.toml | 18 ++++ LICENSE | 21 ++++ README.md | 22 ++++ src/lib.rs | 212 +++++++++++++++++++++++++++++++++++++ src/tests.rs | 169 +++++++++++++++++++++++++++++ 7 files changed, 464 insertions(+) create mode 100644 .github/workflows/rust.yml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 src/lib.rs create mode 100644 src/tests.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..6738b0b --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,19 @@ +name: Rust + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2de3917 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +Cargo.lock +/.idea diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c2da882 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "dynqueue" +version = "0.1.2" +authors = ["Harald Hoyer "] +edition = "2018" + +license = "MIT" +documentation = "https://docs.rs/dynqueue" +homepage = "https://github.com/haraldh/dynqueue" +repository = "https://github.com/haraldh/dynqueue" +description = "Dynamically extendable Rayon parallel iterator" +readme = "README.md" + +keywords = [ "parallel", "performance", "thread", "join", "concurrency"] +categories = [ "concurrency" ] + +[dependencies] +rayon = "1.3.0" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..97542ec --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Harald Hoyer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..f044844 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# DynQueue - dynamically extendable Rayon parallel iterator + +A `DynQueue` can be iterated with `into_par_iter` producing `(DynQueueHandle, T)` elements. +With the `DynQueueHandle` a new `T` can be inserted in the `DynQueue`, +which is currently iterated over. + +```rust +use dynqueue::DynQueue; + +use rayon::iter::IntoParallelIterator as _; +use rayon::iter::ParallelIterator as _; + +fn main() { + let mut result = DynQueue::new(vec![1, 2, 3]) + .into_par_iter() + .map(|(handle, value)| { if value == 2 { handle.enqueue(4) }; value }) + .collect::>(); + result.sort(); + + assert_eq!(result, vec![1, 2, 3, 4]); +} +``` diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..c59b075 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,212 @@ +//! DynQueue - dynamically extendable Rayon parallel iterator +//! +//! A `DynQueue` can be iterated with `into_par_iter` producing `(DynQueueHandle, T)` elements. +//! With the `DynQueueHandle` a new `T` can be inserted in the `DynQueue`, +//! which is currently iterated over. +//! +//! # Example +//! +//! ``` +//! use dynqueue::{DynQueue, DynQueueHandle}; +//! +//! use rayon::iter::IntoParallelIterator as _; +//! use rayon::iter::ParallelIterator as _; +//! +//! let mut result = DynQueue::new(vec![1, 2, 3]) +//! .into_par_iter() +//! .map(|(handle, value)| { if value == 2 { handle.enqueue(4) }; value }) +//! .collect::>(); +//! result.sort(); +//! +//! assert_eq!(result, vec![1, 2, 3, 4]); +//! ``` +//! +//! # Panics +//! +//! The `DynQueueHandle` shall not outlive the `DynQueue` iterator +//! +//! ```should_panic +//! use dynqueue::{DynQueue, DynQueueHandle}; +//! +//! use rayon::iter::IntoParallelIterator as _; +//! use rayon::iter::ParallelIterator as _; +//! +//! static mut STALE_HANDLE : Option>> = None; +//! +//! pub fn test_func() -> Vec { +//! DynQueue::new(vec![1u8, 2u8, 3u8]) +//! .into_par_iter() +//! .map(|(handle, value)| unsafe { STALE_HANDLE.replace(handle); value }) +//! .collect::>() +//! } +//! // test_func() panics +//! let result = test_func(); +//! unsafe { STALE_HANDLE.as_ref().unwrap().enqueue(4); } +//! ``` + +#![deny(clippy::all)] +#![deny(missing_docs)] + +use rayon::iter::plumbing::{ + bridge_unindexed, Consumer, Folder, UnindexedConsumer, UnindexedProducer, +}; +use std::collections::VecDeque; +use std::marker::PhantomData; +use std::sync::{Arc, RwLock}; + +#[cfg(test)] +mod tests; + +/// Everything implementing `Queue` can be handled by DynQueue +pub trait Queue +where + Self: Sized, +{ + /// push an element in the queue + fn push(&mut self, v: T); + + /// pop an element from the queue + fn pop(&mut self) -> Option; + + /// number of elements in the queue + fn len(&self) -> usize; + + /// split off `size` elements + fn split_off(&mut self, size: usize) -> Self; +} + +impl Queue for Vec { + #[inline(always)] + fn push(&mut self, v: T) { + Vec::push(self, v) + } + + #[inline(always)] + fn pop(&mut self) -> Option { + Vec::pop(self) + } + + #[inline(always)] + fn len(&self) -> usize { + Vec::len(self) + } + + #[inline(always)] + fn split_off(&mut self, size: usize) -> Self { + Vec::split_off(self, size) + } +} + +impl Queue for VecDeque { + #[inline(always)] + fn push(&mut self, v: T) { + VecDeque::push_back(self, v) + } + + #[inline(always)] + fn pop(&mut self) -> Option { + VecDeque::pop_front(self) + } + + #[inline(always)] + fn len(&self) -> usize { + VecDeque::len(self) + } + + #[inline(always)] + fn split_off(&mut self, size: usize) -> Self { + VecDeque::split_off(self, size) + } +} + +// PhantomData should prevent `DynQueueInner` to outlive the original `DynQueue` +// but does not always. +struct DynQueueInner<'a, T, U: Queue>(std::sync::RwLock, PhantomData<&'a T>); + +/// The `DynQueueHandle` returned by the iterator in addition to `T` +pub struct DynQueueHandle<'a, T, U: Queue>(Arc>); + +impl<'a, T, U: Queue> DynQueueHandle<'a, T, U> { + /// Enqueue `T` in the `DynQueue`, which is currently iterated. + #[inline] + pub fn enqueue(&self, job: T) { + (self.0).0.write().unwrap().push(job) + } +} + +/// The `DynQueue` which can be parallel iterated over +pub struct DynQueue<'a, T, U: Queue>(Arc>); + +impl<'a, T, U: Queue> DynQueue<'a, T, U> { + /// Create a new `DynQueue` from a `Vec` + #[inline] + pub fn new(lifo: U) -> Self { + Self(Arc::new(DynQueueInner(RwLock::new(lifo), PhantomData))) + } +} + +impl<'a, T, U> UnindexedProducer for DynQueue<'a, T, U> +where + T: Send + Sync, + U: Queue + Send + Sync, +{ + type Item = (DynQueueHandle<'a, T, U>, T); + + fn split(self) -> (Self, Option) { + let len = { + let q = (self.0).0.read().unwrap(); + q.len() + }; + if len >= 2 { + let new_q = { + let mut q = (self.0).0.write().unwrap(); + let split_off = q.split_off(len / 2); + DynQueue::new(split_off) + }; + (self, Some(new_q)) + } else { + (self, None) + } + } + + fn fold_with(self, folder: F) -> F + where + F: Folder, + { + let mut folder = folder; + loop { + let ret = { + let mut q = (self.0).0.write().unwrap(); + q.pop() + }; + + if let Some(v) = ret { + folder = folder.consume((DynQueueHandle(self.0.clone()), v)); + + if folder.full() { + break; + } + } else { + // Self shall have the only reference + assert_eq!(Arc::strong_count(&self.0), 1, "Stale Handle"); + break; + } + } + folder + } +} + +impl<'a, T, U> rayon::iter::ParallelIterator for DynQueue<'a, T, U> +where + T: Send + Sync, + U: Queue + Send + Sync, +{ + type Item = (DynQueueHandle<'a, T, U>, T); + + fn drive_unindexed(self, consumer: C) -> >::Result + where + C: UnindexedConsumer, + { + bridge_unindexed(self, consumer) + } +} diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..05be6ce --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,169 @@ +use crate::{DynQueue, DynQueueHandle, Queue}; +use std::collections::VecDeque; + +const SLEEP_MS: u64 = 10; + +#[inline] +fn handle_queue>(t: (DynQueueHandle, u64)) -> u64 { + let (h, v) = t; + + if v % 2 == 0 { + h.enqueue(11); + } + if v % 3 == 0 { + h.enqueue(11); + } + if v % 4 == 0 { + h.enqueue(11); + } + if v == 11 { + h.enqueue(5); + h.enqueue(17); + } + v +} + +#[inline] +fn get_input() -> Vec { + vec![ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, + ] +} + +#[inline] +fn get_expected() -> Vec { + vec![ + 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, + 8, 9, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, + 11, 11, 11, 12, 13, 14, 15, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, + 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 19, 20, 21, + ] +} + +#[test] +fn dynqueue_iter_test_const_sleep() { + use rayon::iter::IntoParallelIterator as _; + use rayon::iter::ParallelIterator as _; + use std::time::Duration; + let expected = get_expected(); + + let med = expected.iter().sum::() / expected.iter().count() as u64; + + let jq = DynQueue::new(get_input()); + let now = std::time::Instant::now(); + + let mut res = jq + .into_par_iter() + .map(handle_queue) + .map(|v| { + std::thread::sleep(Duration::from_millis(SLEEP_MS * med)); + v + }) + .collect::>(); + eprintln!("elapsed = {:#?}", now.elapsed()); + res.sort(); + assert_eq!(res, expected); + eprintln!( + "instead of = {}ms", + res.iter().count() * med as usize * SLEEP_MS as usize + ); +} + +#[test] +fn dynqueue_iter_test_const_sleep_vecdeque() { + use rayon::iter::IntoParallelIterator as _; + use rayon::iter::ParallelIterator as _; + use std::time::Duration; + let expected = get_expected(); + + let med = expected.iter().sum::() / expected.iter().count() as u64; + + let jq = DynQueue::new(VecDeque::from(get_input())); + let now = std::time::Instant::now(); + + let mut res = jq + .into_par_iter() + .map(handle_queue) + .map(|v| { + std::thread::sleep(Duration::from_millis(SLEEP_MS * med)); + v + }) + .collect::>(); + eprintln!("elapsed = {:#?}", now.elapsed()); + res.sort(); + assert_eq!(res, expected); + eprintln!( + "instead of = {}ms", + res.iter().count() * med as usize * SLEEP_MS as usize + ); +} + +#[test] +fn dynqueue_iter_test_sleep_v() { + use rayon::iter::IntoParallelIterator as _; + use rayon::iter::ParallelIterator as _; + use std::time::Duration; + + let jq = DynQueue::new(get_input()); + + let now = std::time::Instant::now(); + + let mut res = jq + .into_par_iter() + .map(handle_queue) + .map(|v| { + std::thread::sleep(Duration::from_millis(SLEEP_MS * v)); + v + }) + .collect::>(); + eprintln!("elapsed = {:#?}", now.elapsed()); + res.sort(); + assert_eq!(res, get_expected()); + eprintln!("instead of = {}ms", res.iter().sum::() * SLEEP_MS); +} + +#[test] +fn dynqueue_iter_test_sleep_inv_v() { + use rayon::iter::IntoParallelIterator as _; + use rayon::iter::ParallelIterator as _; + use std::time::Duration; + + let jq = DynQueue::new(get_input()); + + let now = std::time::Instant::now(); + + let mut res = jq + .into_par_iter() + .map(handle_queue) + .map(|v| { + std::thread::sleep(Duration::from_millis(SLEEP_MS * (22 - v))); + v + }) + .collect::>(); + eprintln!("elapsed = {:#?}", now.elapsed()); + res.sort(); + assert_eq!(res, get_expected()); + eprintln!( + "instead of = {}ms", + (res.iter().count() as u64 * 22 - res.iter().sum::()) * SLEEP_MS + ); +} + +#[test] +fn par_iter_test() { + use rayon::iter::IntoParallelIterator as _; + use rayon::iter::ParallelIterator as _; + use std::time::Duration; + + let now = std::time::Instant::now(); + + let res = get_expected() + .into_par_iter() + .map(|v| { + std::thread::sleep(Duration::from_millis(SLEEP_MS * v as u64)); + v + }) + .collect::>(); + eprintln!("elapsed = {:#?}", now.elapsed()); + eprintln!("instead of = {}ms", res.iter().sum::() * SLEEP_MS); +}