-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Parallel queries #292
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c04ada8
Partial ParallelIterator interface
GrantMoyer 66bdc4a
Refactor into modules
GrantMoyer 15119a0
Take task pool as an argument
GrantMoyer 31c0c67
Cleanup code style and use statements
GrantMoyer 9ba35a1
Remove zip method implementation for now
GrantMoyer 4251572
Add Iterator method implementations
GrantMoyer 2525d00
Fix doc typos
GrantMoyer e7c1d21
Implement ParallelIterator for Query
GrantMoyer 089f59e
Unimplement Iterator for BatchedIter to prevent method name collision
GrantMoyer bccdb47
Export ParallelIterator in prelude
GrantMoyer 54b0907
Add parallel query example
GrantMoyer 8e8fd3a
Add basic ParallelIterator benchmarks
GrantMoyer 25a50a5
Rename BatchedIter to ParIter
GrantMoyer 028f94d
Make more practical parallel query example
GrantMoyer 751bd3a
Remove unnessesary impls for Send + Sync
GrantMoyer 709728f
Remove find method for now
GrantMoyer 632604c
Remove unessesary calls to predicate from position()
GrantMoyer 9958389
Add documentation about when to use or not use ParallelIterator
GrantMoyer f795532
Take a &TaskPool for future parallelization
GrantMoyer b134391
Document ParallelIterator methods
GrantMoyer 43ce660
Typo
GrantMoyer cdfe1cd
Reuse material
GrantMoyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
use bevy_tasks::{ParallelIterator, TaskPoolBuilder}; | ||
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
|
||
struct ParChunks<'a, T>(std::slice::Chunks<'a, T>); | ||
impl<'a, T> ParallelIterator<std::slice::Iter<'a, T>> for ParChunks<'a, T> | ||
where | ||
T: 'a + Send + Sync, | ||
{ | ||
type Item = &'a T; | ||
|
||
fn next_batch(&mut self) -> Option<std::slice::Iter<'a, T>> { | ||
self.0.next().map(|s| s.iter()) | ||
} | ||
} | ||
|
||
struct ParChunksMut<'a, T>(std::slice::ChunksMut<'a, T>); | ||
impl<'a, T> ParallelIterator<std::slice::IterMut<'a, T>> for ParChunksMut<'a, T> | ||
where | ||
T: 'a + Send + Sync, | ||
{ | ||
type Item = &'a mut T; | ||
|
||
fn next_batch(&mut self) -> Option<std::slice::IterMut<'a, T>> { | ||
self.0.next().map(|s| s.iter_mut()) | ||
} | ||
} | ||
|
||
fn bench_overhead(c: &mut Criterion) { | ||
fn noop(_: &mut usize) {}; | ||
|
||
let mut v = (0..10000).collect::<Vec<usize>>(); | ||
c.bench_function("overhead_iter", |b| { | ||
b.iter(|| { | ||
v.iter_mut().for_each(noop); | ||
}) | ||
}); | ||
|
||
let mut v = (0..10000).collect::<Vec<usize>>(); | ||
let mut group = c.benchmark_group("overhead_par_iter"); | ||
for thread_count in &[1, 2, 4, 8, 16, 32] { | ||
let pool = TaskPoolBuilder::new().num_threads(*thread_count).build(); | ||
group.bench_with_input( | ||
BenchmarkId::new("threads", thread_count), | ||
thread_count, | ||
|b, _| { | ||
b.iter(|| { | ||
ParChunksMut(v.chunks_mut(100)).for_each(&pool, noop); | ||
}) | ||
}, | ||
); | ||
} | ||
group.finish(); | ||
} | ||
|
||
fn bench_for_each(c: &mut Criterion) { | ||
fn busy_work(n: usize) { | ||
let mut i = n; | ||
while i > 0 { | ||
i = black_box(i - 1); | ||
} | ||
} | ||
|
||
let mut v = (0..10000).collect::<Vec<usize>>(); | ||
c.bench_function("for_each_iter", |b| { | ||
b.iter(|| { | ||
v.iter_mut().for_each(|x| { | ||
busy_work(10000); | ||
*x *= *x; | ||
}); | ||
}) | ||
}); | ||
|
||
let mut v = (0..10000).collect::<Vec<usize>>(); | ||
let mut group = c.benchmark_group("for_each_par_iter"); | ||
for thread_count in &[1, 2, 4, 8, 16, 32] { | ||
let pool = TaskPoolBuilder::new().num_threads(*thread_count).build(); | ||
group.bench_with_input( | ||
BenchmarkId::new("threads", thread_count), | ||
thread_count, | ||
|b, _| { | ||
b.iter(|| { | ||
ParChunksMut(v.chunks_mut(100)).for_each(&pool, |x| { | ||
busy_work(10000); | ||
*x *= *x; | ||
}); | ||
}) | ||
}, | ||
); | ||
} | ||
group.finish(); | ||
} | ||
|
||
fn bench_many_maps(c: &mut Criterion) { | ||
fn busy_doubles(mut x: usize, n: usize) -> usize { | ||
for _ in 0..n { | ||
x = black_box(x.wrapping_mul(2)); | ||
} | ||
x | ||
} | ||
|
||
let v = (0..10000).collect::<Vec<usize>>(); | ||
c.bench_function("many_maps_iter", |b| { | ||
b.iter(|| { | ||
v.iter() | ||
.map(|x| busy_doubles(*x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.for_each(drop); | ||
}) | ||
}); | ||
|
||
let v = (0..10000).collect::<Vec<usize>>(); | ||
let mut group = c.benchmark_group("many_maps_par_iter"); | ||
for thread_count in &[1, 2, 4, 8, 16, 32] { | ||
let pool = TaskPoolBuilder::new().num_threads(*thread_count).build(); | ||
group.bench_with_input( | ||
BenchmarkId::new("threads", thread_count), | ||
thread_count, | ||
|b, _| { | ||
b.iter(|| { | ||
ParChunks(v.chunks(100)) | ||
.map(|x| busy_doubles(*x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.map(|x| busy_doubles(x, 1000)) | ||
.for_each(&pool, drop); | ||
}) | ||
}, | ||
); | ||
} | ||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, bench_overhead, bench_for_each, bench_many_maps); | ||
criterion_main!(benches); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[inline] would make this more consistent with some of the other functions in this file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its worth pointing out that inline sometimes regresses performance. iterators are especially weird in that respect. its worth testing perf for every inline/non-inline decision made.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't profiled inline vs not inline here, so I'd prefer to let the compiler make the decision for now.