Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel queries #292

Merged
merged 22 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor into modules
  • Loading branch information
GrantMoyer committed Sep 7, 2020
commit 66bdc4a34c0217584ac2712adbfe22a35b834ba1
84 changes: 84 additions & 0 deletions crates/bevy_tasks/src/iter/adapters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use crate::iter::ParallelIterator;
use crate::TaskPool;

pub struct Chain<T, U> {
pub(crate) left: T,
pub(crate) right: U,
pub(crate) left_in_progress: bool,
}

impl<B, T, U> ParallelIterator<B> for Chain<T, U>
where
B: Iterator + Send,
T: ParallelIterator<B, Item = B::Item>,
U: ParallelIterator<B, Item = T::Item>,
{
type Item = T::Item;

fn next_batch(&mut self) -> Option<B> {
if self.left_in_progress {
match self.left.next_batch() {
b @ Some(_) => return b,
None => self.left_in_progress = false,
}
}
self.right.next_batch()
}

fn task_pool(&self) -> &TaskPool {
if self.left_in_progress {
self.left.task_pool()
} else {
self.right.task_pool()
}
}
}

pub struct Zip<B1, B2, T, U> {
pub(crate) left: T,
pub(crate) left_batch: Option<B1>,
pub(crate) right: U,
pub(crate) right_batch: Option<B2>,
}

impl<B1, B2, T, U> ParallelIterator<std::iter::Zip<B1, B2>> for Zip<B1, B2, T, U>
where
B1: Iterator + Send,
B2: Iterator + Send,
T: ParallelIterator<B1, Item = B1::Item>,
U: ParallelIterator<B2, Item = B2::Item>,
{
type Item = (T::Item, U::Item);

/// Note: Zip::next_batch() nessesarily reduces the batch size to 1
fn next_batch(&mut self) -> Option<std::iter::Zip<B1, B2>> {
unimplemented!()
}

// TODO: not sure what to do with this
fn task_pool(&self) -> &TaskPool {
self.left.task_pool()
}
}

pub struct Map<P, F> {
pub(crate) iter: P,
pub(crate) f: F,
}

impl<B, U, T, F> ParallelIterator<std::iter::Map<B, F>> for Map<U, F>
where
B: Iterator + Send,
U: ParallelIterator<B, Item = B::Item>,
F: FnMut(U::Item) -> T + Send + Clone,
{
type Item = T;

fn next_batch(&mut self) -> Option<std::iter::Map<B, F>> {
self.iter.next_batch().map(|b| b.map(self.f.clone()))
}

fn task_pool(&self) -> &TaskPool {
self.iter.task_pool()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::TaskPool;

mod adapters;
pub use adapters::*;

/// ParallelIterator closely emulates the std::iter::Iterator
/// interface. However, it uses bevy_task to compute batches in parallel.
pub trait ParallelIterator<B>
Expand Down Expand Up @@ -100,84 +103,3 @@ where
});
}
}

pub struct Chain<T, U> {
left: T,
right: U,
left_in_progress: bool,
}

impl<B, T, U> ParallelIterator<B> for Chain<T, U>
where
B: Iterator + Send,
T: ParallelIterator<B, Item = B::Item>,
U: ParallelIterator<B, Item = T::Item>,
{
type Item = T::Item;

fn next_batch(&mut self) -> Option<B> {
if self.left_in_progress {
match self.left.next_batch() {
b @ Some(_) => return b,
None => self.left_in_progress = false,
}
}
self.right.next_batch()
}

fn task_pool(&self) -> &TaskPool {
if self.left_in_progress {
self.left.task_pool()
} else {
self.right.task_pool()
}
}
}

pub struct Zip<B1, B2, T, U> {
left: T,
left_batch: Option<B1>,
right: U,
right_batch: Option<B2>,
}

impl<B1, B2, T, U> ParallelIterator<std::iter::Zip<B1, B2>> for Zip<B1, B2, T, U>
where
B1: Iterator + Send,
B2: Iterator + Send,
T: ParallelIterator<B1, Item = B1::Item>,
U: ParallelIterator<B2, Item = B2::Item>,
{
type Item = (T::Item, U::Item);

fn next_batch(&mut self) -> Option<std::iter::Zip<B1, B2>> {
unimplemented!()
}

// TODO: not sure what to do with this
fn task_pool(&self) -> &TaskPool {
self.left.task_pool()
}
}

pub struct Map<P, F> {
iter: P,
f: F,
}

impl<B, U, T, F> ParallelIterator<std::iter::Map<B, F>> for Map<U, F>
where
B: Iterator + Send,
U: ParallelIterator<B, Item = B::Item>,
F: FnMut(U::Item) -> T + Send + Clone,
{
type Item = T;

fn next_batch(&mut self) -> Option<std::iter::Map<B, F>> {
self.iter.next_batch().map(|b| b.map(self.f.clone()))
}

fn task_pool(&self) -> &TaskPool {
self.iter.task_pool()
}
}