-
Notifications
You must be signed in to change notification settings - Fork 51
Shared counters optimization #101
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
Closed
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2c08ee0
Shared counters optimization #11
jimvdl d5aaaef
Missing safety warning
jimvdl a4ef493
Shared counters
jimvdl d982e4d
Minor changes
jimvdl 72286fa
Fixed invalid drop of cells
jimvdl d4e8587
Run cargo fmt
jimvdl 2a4a28f
Fixed incorrect drop of old counter cells after resize
jimvdl 7b2e23f
Moved counter cell logic into its own module
jimvdl 4b3d0ed
Minor doc fixes
jimvdl d7de300
Ran cargo fmt
jimvdl 5882f58
Impl Send + Sync for LongAdder
jimvdl 577874e
Removed unnecessary Send and Sync for LongAdder
jimvdl 52d1cfe
Refactored LongAdder to ConcurrentCounter
jimvdl 8be45ce
Merge branch 'master' of https://github.com/jonhoo/flurry into shared…
jimvdl d72fed6
merged master, minor clippy adjustments
jimvdl 166dca0
Applied review changes
jimvdl a7e3aaf
Applied some review changes
jimvdl caa9ae3
Merge branch 'master' of https://github.com/jonhoo/flurry into shared…
jimvdl 68eda45
Better cell selection, reverted resize hint change
jimvdl e398d4f
Fixed an issue where base wasn't retried correctly every iteration
jimvdl 906672e
Merge branch 'master' into shared-counters-optimization
jimvdl 5221009
Merge branch 'master' of https://github.com/jonhoo/flurry into shared…
jimvdl d8343f9
Initial benchmarking approach
jimvdl 1bd51f5
Merge branch 'shared-counters-optimization' of https://github.com/jim…
jimvdl 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 hidden or 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 hidden or 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,124 @@ | ||
| use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; | ||
| use rayon; | ||
| use rayon::prelude::*; | ||
| use std::sync::atomic::{AtomicIsize, Ordering}; | ||
|
|
||
| const ITER: isize = 32 * 1024; | ||
|
|
||
| #[derive(Debug)] | ||
| struct ConcurrentCounter { | ||
| base: AtomicIsize, | ||
| cells: Vec<AtomicIsize>, | ||
| } | ||
|
|
||
| impl ConcurrentCounter { | ||
| fn new() -> Self { | ||
| Self { | ||
| base: AtomicIsize::new(0), | ||
| cells: (0..num_cpus::get()) | ||
| .into_iter() | ||
| .map(|_| AtomicIsize::new(0)) | ||
| .collect(), | ||
| } | ||
| } | ||
|
|
||
| fn add(&self, value: isize) { | ||
| let mut base = self.base.load(Ordering::SeqCst); | ||
| let mut index = base + value; | ||
|
|
||
| loop { | ||
| match self.base.compare_exchange( | ||
| base, | ||
| base + value, | ||
| Ordering::SeqCst, | ||
| Ordering::Relaxed, | ||
| ) { | ||
| Ok(_) => break, | ||
| Err(b) => base = b, | ||
| } | ||
|
|
||
| let c = &self.cells[index as usize % self.cells.len()]; | ||
| let cv = c.load(Ordering::SeqCst); | ||
| index += cv; | ||
|
|
||
| if c.compare_exchange(cv, cv + value, Ordering::SeqCst, Ordering::Relaxed) | ||
| .is_ok() | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn sum(&self, ordering: Ordering) -> isize { | ||
| let sum: isize = self.cells.iter().map(|c| c.load(ordering)).sum(); | ||
|
|
||
| self.base.load(ordering) + sum | ||
| } | ||
| } | ||
|
|
||
| fn atomic_counter(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group("atomic_counter"); | ||
| group.throughput(Throughput::Elements(ITER as u64)); | ||
| let max = num_cpus::get(); | ||
|
|
||
| for threads in 1..=max { | ||
| group.bench_with_input( | ||
| BenchmarkId::from_parameter(threads), | ||
| &threads, | ||
| |b, &threads| { | ||
| let pool = rayon::ThreadPoolBuilder::new() | ||
| .num_threads(threads) | ||
| .build() | ||
| .unwrap(); | ||
| pool.install(|| { | ||
| b.iter(|| { | ||
| let counter = AtomicIsize::new(0); | ||
| (0..ITER).into_par_iter().for_each(|_| { | ||
| counter.fetch_add(1, Ordering::Relaxed); | ||
| }); | ||
| assert_eq!(ITER, counter.load(Ordering::Relaxed)); | ||
| }) | ||
| }); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| fn concurrent_counter(c: &mut Criterion) { | ||
| let mut group = c.benchmark_group("concurrent_counter"); | ||
| group.throughput(Throughput::Elements(ITER as u64)); | ||
| let max = num_cpus::get(); | ||
|
|
||
| for threads in 1..=max { | ||
| group.bench_with_input( | ||
| BenchmarkId::from_parameter(threads), | ||
| &threads, | ||
| |b, &threads| { | ||
| let pool = rayon::ThreadPoolBuilder::new() | ||
| .num_threads(threads) | ||
| .build() | ||
| .unwrap(); | ||
| pool.install(|| { | ||
| b.iter(|| { | ||
| let counter = ConcurrentCounter::new(); | ||
| (0..ITER).into_par_iter().for_each(|_| { | ||
| counter.add(1); | ||
| }); | ||
| assert_eq!(ITER, counter.sum(Ordering::Relaxed)); | ||
| }) | ||
| }); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| criterion_group!( | ||
| benches, | ||
| atomic_counter, | ||
| concurrent_counter | ||
| ); | ||
| criterion_main!(benches); |
This file contains hidden or 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,53 @@ | ||
| use std::sync::atomic::{AtomicIsize, Ordering}; | ||
|
|
||
| // TODO: finish Java CounterCell port, this is only a bare minimum implementation. | ||
| #[derive(Debug)] | ||
| pub(crate) struct ConcurrentCounter { | ||
| base: AtomicIsize, | ||
| cells: Vec<AtomicIsize>, | ||
| } | ||
|
|
||
| impl ConcurrentCounter { | ||
| pub(crate) fn new() -> Self { | ||
| Self { | ||
| base: AtomicIsize::new(0), | ||
| cells: (0..crate::map::num_cpus()) | ||
| .into_iter() | ||
| .map(|_| AtomicIsize::new(0)) | ||
| .collect(), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn add(&self, value: isize) { | ||
| let mut base = self.base.load(Ordering::SeqCst); | ||
| let mut index = base + value; | ||
|
|
||
| loop { | ||
| match self.base.compare_exchange( | ||
| base, | ||
| base + value, | ||
| Ordering::SeqCst, | ||
| Ordering::Relaxed, | ||
| ) { | ||
| Ok(_) => break, | ||
| Err(b) => base = b, | ||
| } | ||
|
|
||
| let c = &self.cells[index as usize % self.cells.len()]; | ||
| let cv = c.load(Ordering::SeqCst); | ||
| index += cv; | ||
|
|
||
| if c.compare_exchange(cv, cv + value, Ordering::SeqCst, Ordering::Relaxed) | ||
| .is_ok() | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn sum(&self, ordering: Ordering) -> isize { | ||
| let sum: isize = self.cells.iter().map(|c| c.load(ordering)).sum(); | ||
|
|
||
| self.base.load(ordering) + sum | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.