Repository navigation
Support zero-column mutable matrix iterators #1462
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
Open
Alex Razumov (arrayka)
wants to merge
7
commits into
main
Choose a base branch
from
u/arrayka/zero_column_iterators
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+259
−57
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7881beb
fix(utils): support zero-column mutable iterators
1f96dda
docs(utils): clarify zero-column iterator semantics
b8d1c07
test(utils): benchmark parallel mutable row iteration
055e0a5
Removed "before" bench
9d819f4
required-features = ["rayon"]
95d3864
revert(utils): restore par_rows_mut baseline benchmark
4f25b43
refactor(utils): unify mutable parallel iterators
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,81 @@ | ||
| /* | ||
| * Copyright (c) Microsoft Corporation. | ||
| * Licensed under the MIT license. | ||
| */ | ||
|
|
||
| use std::{hint::black_box, time::Duration}; | ||
|
|
||
| use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; | ||
| use diskann_utils::views::rowmajor::{Matrix, MatrixMut, Owned}; | ||
| use rayon::prelude::{IndexedParallelIterator, ParallelIterator, ParallelSliceMut}; | ||
|
|
||
| const NCOLS: usize = 100; | ||
|
|
||
| fn par_rows_mut_baseline<M>( | ||
| matrix: &mut M, | ||
| ) -> impl IndexedParallelIterator<Item = &mut [M::Element]> | ||
| where | ||
| M: MatrixMut, | ||
| M::Element: Send, | ||
| { | ||
| let ncols = matrix.ncols(); | ||
| assert!( | ||
| ncols != 0 || matrix.nrows() == 0, | ||
| "`MatrixMut::par_rows_mut` does not support matrices with rows and zero columns" | ||
| ); | ||
| matrix.as_mut_slice().par_chunks_exact_mut(ncols.max(1)) | ||
| } | ||
|
|
||
| fn update_rows<'a, I>(rows: I) | ||
| where | ||
| I: ParallelIterator<Item = &'a mut [u32]>, | ||
| { | ||
| rows.for_each(|row| { | ||
| let (first, rest) = row.split_first_mut().unwrap(); | ||
| *first = first.wrapping_add(1); | ||
|
|
||
| let mut previous = *first; | ||
| for value in rest { | ||
| *value = value.wrapping_add(previous).wrapping_add(1); | ||
| previous = *value; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| fn benchmark_shape(c: &mut Criterion, nrows: usize) { | ||
| let mut group = c.benchmark_group(format!("par_rows_mut/{nrows}x{NCOLS}")); | ||
| group.throughput(Throughput::Elements((nrows * NCOLS) as u64)); | ||
|
|
||
| group.bench_function(BenchmarkId::new("baseline", nrows), |b| { | ||
| let mut matrix = Owned::from_element(nrows, NCOLS, 0u32); | ||
| b.iter(|| { | ||
| update_rows(par_rows_mut_baseline(black_box(&mut matrix))); | ||
| black_box(matrix.as_slice()); | ||
| }); | ||
| }); | ||
|
|
||
| group.bench_function(BenchmarkId::new("current", nrows), |b| { | ||
| let mut matrix = Owned::from_element(nrows, NCOLS, 0u32); | ||
| b.iter(|| { | ||
| update_rows(black_box(&mut matrix).par_rows_mut()); | ||
| black_box(matrix.as_slice()); | ||
| }); | ||
| }); | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| fn benchmark_par_rows_mut(c: &mut Criterion) { | ||
| benchmark_shape(c, 5); | ||
| benchmark_shape(c, 1_000_000); | ||
| } | ||
|
|
||
| criterion_group! { | ||
| name = benches; | ||
| config = Criterion::default() | ||
| .sample_size(10) | ||
| .warm_up_time(Duration::from_secs(2)) | ||
| .measurement_time(Duration::from_secs(5)); | ||
| targets = benchmark_par_rows_mut | ||
| } | ||
| 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
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
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.
Uh oh!
There was an error while loading. Please reload this page.