-
Notifications
You must be signed in to change notification settings - Fork 95
Add Mask::count() method to count true elements #490
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
GrigoryEvko
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
GrigoryEvko:feat/mask-find-first
base: master
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.
+396
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,307 @@ | ||
| //! Comprehensive benchmarks for Mask::count() performance analysis | ||
| //! | ||
| //! This benchmark suite tests: | ||
| //! - Different mask sizes (2, 4, 8, 16, 32, 64 elements) | ||
| //! - Different densities (0%, 25%, 50%, 75%, 100% true) | ||
| //! - Comparison with manual iteration baseline | ||
| //! - Cache behavior and instruction-level performance | ||
|
|
||
| #![feature(portable_simd)] | ||
| #![feature(test)] | ||
|
|
||
| extern crate test; | ||
| use cmp::SimdPartialOrd; | ||
| use core_simd::simd::*; | ||
| use test::{Bencher, black_box}; | ||
|
|
||
| // ============================================================================ | ||
| // Mask Size: 2 elements (i64) | ||
| // ============================================================================ | ||
|
|
||
| #[bench] | ||
| fn mask2_count_0pct(b: &mut Bencher) { | ||
| let mask = mask64x2::splat(false); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask2_count_50pct(b: &mut Bencher) { | ||
| let mask = mask64x2::from_array([true, false]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask2_count_100pct(b: &mut Bencher) { | ||
| let mask = mask64x2::splat(true); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Mask Size: 4 elements (i32) | ||
| // ============================================================================ | ||
|
|
||
| #[bench] | ||
| fn mask4_count_0pct(b: &mut Bencher) { | ||
| let mask = mask32x4::splat(false); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask4_count_25pct(b: &mut Bencher) { | ||
| let mask = mask32x4::from_array([true, false, false, false]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask4_count_50pct(b: &mut Bencher) { | ||
| let mask = mask32x4::from_array([true, false, true, false]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask4_count_75pct(b: &mut Bencher) { | ||
| let mask = mask32x4::from_array([true, true, true, false]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask4_count_100pct(b: &mut Bencher) { | ||
| let mask = mask32x4::splat(true); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| // Baseline: manual iteration for mask4 | ||
| #[bench] | ||
| fn mask4_count_manual_50pct(b: &mut Bencher) { | ||
| let mask = mask32x4::from_array([true, false, true, false]); | ||
| b.iter(|| { | ||
| let m = black_box(mask); | ||
| let mut count = 0; | ||
| for i in 0..4 { | ||
| if m.test(i) { | ||
| count += 1; | ||
| } | ||
| } | ||
| black_box(count) | ||
| }); | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Mask Size: 8 elements (i32) | ||
| // ============================================================================ | ||
|
|
||
| #[bench] | ||
| fn mask8_count_0pct(b: &mut Bencher) { | ||
| let mask = mask32x8::splat(false); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask8_count_25pct(b: &mut Bencher) { | ||
| let mask = mask32x8::from_array([true, false, false, false, true, false, false, false]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask8_count_50pct(b: &mut Bencher) { | ||
| let mask = mask32x8::from_array([true, false, true, false, true, false, true, false]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask8_count_75pct(b: &mut Bencher) { | ||
| let mask = mask32x8::from_array([true, true, true, false, true, true, true, false]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask8_count_100pct(b: &mut Bencher) { | ||
| let mask = mask32x8::splat(true); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| // Baseline: manual iteration for mask8 | ||
| #[bench] | ||
| fn mask8_count_manual_50pct(b: &mut Bencher) { | ||
| let mask = mask32x8::from_array([true, false, true, false, true, false, true, false]); | ||
| b.iter(|| { | ||
| let m = black_box(mask); | ||
| let mut count = 0; | ||
| for i in 0..8 { | ||
| if m.test(i) { | ||
| count += 1; | ||
| } | ||
| } | ||
| black_box(count) | ||
| }); | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Mask Size: 16 elements (i32) | ||
| // ============================================================================ | ||
|
|
||
| #[bench] | ||
| fn mask16_count_0pct(b: &mut Bencher) { | ||
| let mask = mask32x16::splat(false); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask16_count_25pct(b: &mut Bencher) { | ||
| let mask = mask32x16::from_array([ | ||
| true, false, false, false, true, false, false, false, true, false, false, false, true, | ||
| false, false, false, | ||
| ]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask16_count_50pct(b: &mut Bencher) { | ||
| let mask = mask32x16::from_array([ | ||
| true, false, true, false, true, false, true, false, true, false, true, false, true, false, | ||
| true, false, | ||
| ]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask16_count_75pct(b: &mut Bencher) { | ||
| let mask = mask32x16::from_array([ | ||
| true, true, true, false, true, true, true, false, true, true, true, false, true, true, | ||
| true, false, | ||
| ]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask16_count_100pct(b: &mut Bencher) { | ||
| let mask = mask32x16::splat(true); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| // Baseline: manual iteration for mask16 | ||
| #[bench] | ||
| fn mask16_count_manual_50pct(b: &mut Bencher) { | ||
| let mask = mask32x16::from_array([ | ||
| true, false, true, false, true, false, true, false, true, false, true, false, true, false, | ||
| true, false, | ||
| ]); | ||
| b.iter(|| { | ||
| let m = black_box(mask); | ||
| let mut count = 0; | ||
| for i in 0..16 { | ||
| if m.test(i) { | ||
| count += 1; | ||
| } | ||
| } | ||
| black_box(count) | ||
| }); | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Real-world scenario: filtering based on comparison | ||
| // ============================================================================ | ||
|
|
||
| #[bench] | ||
| fn real_world_filter_count_f32x8(b: &mut Bencher) { | ||
| let data = f32x8::from_array([1.0, 5.5, 3.2, 7.8, 2.1, 9.5, 4.3, 6.7]); | ||
| let threshold = f32x8::splat(5.0); | ||
|
|
||
| b.iter(|| { | ||
| let d = black_box(data); | ||
| let t = black_box(threshold); | ||
| let mask = d.simd_gt(t); | ||
| black_box(mask.count()) | ||
| }); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn real_world_filter_count_f32x16(b: &mut Bencher) { | ||
| let data = f32x16::from_array([ | ||
| 1.0, 5.5, 3.2, 7.8, 2.1, 9.5, 4.3, 6.7, 1.5, 5.2, 3.8, 7.1, 2.9, 9.2, 4.8, 6.1, | ||
| ]); | ||
| let threshold = f32x16::splat(5.0); | ||
|
|
||
| b.iter(|| { | ||
| let d = black_box(data); | ||
| let t = black_box(threshold); | ||
| let mask = d.simd_gt(t); | ||
| black_box(mask.count()) | ||
| }); | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Stress test: multiple counts in tight loop | ||
| // ============================================================================ | ||
|
|
||
| #[bench] | ||
| fn stress_multiple_counts_mask8(b: &mut Bencher) { | ||
| let masks = [ | ||
| mask32x8::from_array([true, false, true, false, true, false, true, false]), | ||
| mask32x8::from_array([false, true, false, true, false, true, false, true]), | ||
| mask32x8::from_array([true, true, false, false, true, true, false, false]), | ||
| mask32x8::from_array([false, false, true, true, false, false, true, true]), | ||
| ]; | ||
|
|
||
| b.iter(|| { | ||
| let ms = black_box(&masks); | ||
| let total = ms[0].count() + ms[1].count() + ms[2].count() + ms[3].count(); | ||
| black_box(total) | ||
| }); | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Cache behavior test: alternating access pattern | ||
| // ============================================================================ | ||
|
|
||
| #[bench] | ||
| fn cache_alternating_access(b: &mut Bencher) { | ||
| let mask1 = mask32x8::from_array([true, false, true, false, true, false, true, false]); | ||
| let mask2 = mask32x8::from_array([false, true, false, true, false, true, false, true]); | ||
|
|
||
| b.iter(|| { | ||
| let m1 = black_box(mask1); | ||
| let m2 = black_box(mask2); | ||
| black_box(m1.count() + m2.count()) | ||
| }); | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Test different element types (i64 vs i32) | ||
| // ============================================================================ | ||
|
|
||
| #[bench] | ||
| fn mask4_i64_count_50pct(b: &mut Bencher) { | ||
| let mask = mask64x4::from_array([true, false, true, false]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn mask8_i64_count_50pct(b: &mut Bencher) { | ||
| let mask = mask64x8::from_array([true, false, true, false, true, false, true, false]); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Edge cases | ||
| // ============================================================================ | ||
|
|
||
| #[bench] | ||
| fn edge_case_all_false_mask16(b: &mut Bencher) { | ||
| let mask = mask32x16::splat(false); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn edge_case_all_true_mask16(b: &mut Bencher) { | ||
| let mask = mask32x16::splat(true); | ||
| b.iter(|| black_box(mask).count()); | ||
| } | ||
|
|
||
| #[bench] | ||
| fn edge_case_single_true_mask16(b: &mut Bencher) { | ||
| let mut arr = [false; 16]; | ||
| arr[7] = true; | ||
| let mask = mask32x16::from_array(arr); | ||
| b.iter(|| black_box(mask).count()); | ||
| } |
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,35 @@ | ||
| //! Demonstrates `Mask::count()` to count matching elements. | ||
|
|
||
| #![feature(portable_simd)] | ||
| use cmp::SimdPartialOrd; | ||
| use core_simd::simd::*; | ||
|
|
||
| fn main() { | ||
| // Count elements above threshold | ||
| let data = [1.0, 5.0, 3.0, 7.0, 2.0, 9.0, 4.0, 6.0]; | ||
| let values = f32x8::from_array(data); | ||
| let threshold = f32x8::splat(5.0); | ||
| let mask = values.simd_gt(threshold); | ||
| println!("Values above 5.0: {}", mask.count()); | ||
|
|
||
| // Use count() to pre-allocate for filtering | ||
| let chunks = data.chunks_exact(8); | ||
| let mut total = 0; | ||
| for chunk in chunks.clone() { | ||
| let v = f32x8::from_slice(chunk); | ||
| total += v.simd_gt(f32x8::splat(5.0)).count(); | ||
| } | ||
|
|
||
| let mut results = Vec::with_capacity(total); | ||
| for chunk in chunks { | ||
| let v = f32x8::from_slice(chunk); | ||
| let m = v.simd_gt(f32x8::splat(5.0)); | ||
| for (i, &val) in chunk.iter().enumerate() { | ||
| if m.test(i) { | ||
| results.push(val); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| println!("Filtered: {:?}", results); | ||
| } |
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.
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 have two concerns with this implementation:
to_bitmaskwill truncate to the first 64 bitsto_bitmaskis pretty slow on some architectures. On those architectures, I wonder if something like(mask.to_int() >> (size_of<T>() * 8 - 1)).reduce_sum()would work better