-
Notifications
You must be signed in to change notification settings - Fork 10
simd: apply packing for tree leaves #5
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1a23b5c
simd: apply packing for tree leaves
tcoratger 825a2ec
rm useless default compute_tree_leaves
tcoratger 8b4f5e8
mv simd utils to a specific file
tcoratger 1b7f546
bench
tcoratger efe2d0c
fix bench
tcoratger 7fd3cc6
fix Angus comments
tcoratger be15969
add unit tests against compute_tree_leaves_naive
tcoratger e0040f1
clippy
tcoratger 68c7234
mv simd_utils to root
tcoratger 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
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| pub mod message_hash; | ||
| pub mod prf; | ||
| pub mod simd_utils; | ||
| pub mod tweak_hash; | ||
| pub mod tweak_hash_tree; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| use core::array; | ||
|
|
||
| use p3_field::PackedValue; | ||
|
|
||
| use crate::{F, PackedF}; | ||
|
|
||
| /// Packs scalar arrays into SIMD-friendly vertical layout. | ||
| /// | ||
| /// Transposes from horizontal layout `[[F; N]; WIDTH]` to vertical layout `[PackedF; N]`. | ||
| /// | ||
| /// Input layout (horizontal): each row is one complete array | ||
| /// ```text | ||
| /// data[0] = [a0, a1, a2, ..., aN] | ||
| /// data[1] = [b0, b1, b2, ..., bN] | ||
| /// data[2] = [c0, c1, c2, ..., cN] | ||
| /// ... | ||
| /// ``` | ||
| /// | ||
| /// Output layout (vertical): each PackedF holds one element from each array | ||
| /// ```text | ||
| /// result[0] = PackedF([a0, b0, c0, ...]) // All first elements | ||
| /// result[1] = PackedF([a1, b1, c1, ...]) // All second elements | ||
| /// result[2] = PackedF([a2, b2, c2, ...]) // All third elements | ||
| /// ... | ||
| /// ``` | ||
| /// | ||
| /// This vertical packing enables efficient SIMD operations where a single instruction | ||
| /// processes the same element position across multiple arrays simultaneously. | ||
| #[inline] | ||
| pub fn pack_array<const N: usize>(data: &[[F; N]]) -> [PackedF; N] { | ||
| array::from_fn(|i| PackedF::from_fn(|j| data[j][i])) | ||
| } | ||
|
|
||
| /// Unpacks SIMD vertical layout back into scalar arrays. | ||
| /// | ||
| /// Transposes from vertical layout `[PackedF; N]` to horizontal layout `[[F; N]; WIDTH]`. | ||
| /// | ||
| /// This is the inverse operation of `pack_array`. The output buffer must be preallocated | ||
| /// with size `[WIDTH][N]` where `WIDTH = PackedF::WIDTH`. | ||
| /// | ||
| /// Input layout (vertical): each PackedF holds one element from each array | ||
| /// ```text | ||
| /// packed_data[0] = PackedF([a0, b0, c0, ...]) | ||
| /// packed_data[1] = PackedF([a1, b1, c1, ...]) | ||
| /// packed_data[2] = PackedF([a2, b2, c2, ...]) | ||
| /// ... | ||
| /// ``` | ||
| /// | ||
| /// Output layout (horizontal): each row is one complete array | ||
| /// ```text | ||
| /// output[0] = [a0, a1, a2, ..., aN] | ||
| /// output[1] = [b0, b1, b2, ..., bN] | ||
| /// output[2] = [c0, c1, c2, ..., cN] | ||
| /// ... | ||
| /// ``` | ||
| #[inline] | ||
| pub fn unpack_array<const N: usize>(packed_data: &[PackedF; N], output: &mut [[F; N]]) { | ||
| for (i, data) in packed_data.iter().enumerate().take(N) { | ||
| let unpacked_v = data.as_slice(); | ||
| for j in 0..PackedF::WIDTH { | ||
| output[j][i] = unpacked_v[j]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use p3_field::PrimeCharacteristicRing; | ||
| use proptest::prelude::*; | ||
| use rand::Rng; | ||
|
|
||
| #[test] | ||
| fn test_pack_array_simple() { | ||
| // Test with N=2 (2 field elements per array) | ||
| // Create WIDTH arrays of [F; 2] | ||
| let data: [[F; 2]; PackedF::WIDTH] = | ||
| array::from_fn(|i| [F::from_u64(i as u64), F::from_u64((i + 100) as u64)]); | ||
|
|
||
| let packed = pack_array(&data); | ||
|
|
||
| // Check that packed[0] contains all first elements | ||
| for (lane, &expected) in data.iter().enumerate() { | ||
| assert_eq!(packed[0].as_slice()[lane], expected[0]); | ||
| } | ||
|
|
||
| // Check that packed[1] contains all second elements | ||
| for (lane, &expected) in data.iter().enumerate() { | ||
| assert_eq!(packed[1].as_slice()[lane], expected[1]); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_unpack_array_simple() { | ||
| // Create packed data | ||
| let packed: [PackedF; 2] = [ | ||
| PackedF::from_fn(|i| F::from_u64(i as u64)), | ||
| PackedF::from_fn(|i| F::from_u64((i + 100) as u64)), | ||
| ]; | ||
|
|
||
| // Unpack | ||
| let mut output = [[F::ZERO; 2]; PackedF::WIDTH]; | ||
| unpack_array(&packed, &mut output); | ||
|
|
||
| // Verify | ||
| for (lane, arr) in output.iter().enumerate() { | ||
| assert_eq!(arr[0], F::from_u64(lane as u64)); | ||
| assert_eq!(arr[1], F::from_u64((lane + 100) as u64)); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_pack_preserves_element_order() { | ||
| // Create data where each array has sequential values | ||
| let data: [[F; 3]; PackedF::WIDTH] = array::from_fn(|i| { | ||
| [ | ||
| F::from_u64((i * 3) as u64), | ||
| F::from_u64((i * 3 + 1) as u64), | ||
| F::from_u64((i * 3 + 2) as u64), | ||
| ] | ||
| }); | ||
|
|
||
| let packed = pack_array(&data); | ||
|
|
||
| // Verify the packing structure | ||
| // packed[0] should contain: [0, 3, 6, 9, ...] | ||
| // packed[1] should contain: [1, 4, 7, 10, ...] | ||
| // packed[2] should contain: [2, 5, 8, 11, ...] | ||
| for (element_idx, p) in packed.iter().enumerate() { | ||
| for lane in 0..PackedF::WIDTH { | ||
| let expected = F::from_u64((lane * 3 + element_idx) as u64); | ||
| assert_eq!(p.as_slice()[lane], expected); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_unpack_preserves_element_order() { | ||
| // Create packed data with known pattern | ||
| let packed: [PackedF; 3] = [ | ||
| PackedF::from_fn(|i| F::from_u64((i * 3) as u64)), | ||
| PackedF::from_fn(|i| F::from_u64((i * 3 + 1) as u64)), | ||
| PackedF::from_fn(|i| F::from_u64((i * 3 + 2) as u64)), | ||
| ]; | ||
|
|
||
| let mut output = [[F::ZERO; 3]; PackedF::WIDTH]; | ||
| unpack_array(&packed, &mut output); | ||
|
|
||
| // Verify each array has sequential values | ||
| for (lane, arr) in output.iter().enumerate() { | ||
| assert_eq!(arr[0], F::from_u64((lane * 3) as u64)); | ||
| assert_eq!(arr[1], F::from_u64((lane * 3 + 1) as u64)); | ||
| assert_eq!(arr[2], F::from_u64((lane * 3 + 2) as u64)); | ||
| } | ||
| } | ||
|
|
||
| proptest! { | ||
| #[test] | ||
| fn proptest_pack_unpack_roundtrip( | ||
| _seed in any::<u64>() | ||
| ) { | ||
| let mut rng = rand::rng(); | ||
|
|
||
| // Generate random data with N=10 | ||
| let original: [[F; 10]; PackedF::WIDTH] = array::from_fn(|_| { | ||
| array::from_fn(|_| rng.random()) | ||
| }); | ||
|
|
||
| // Pack and unpack | ||
| let packed = pack_array(&original); | ||
| let mut unpacked = [[F::ZERO; 10]; PackedF::WIDTH]; | ||
| unpack_array(&packed, &mut unpacked); | ||
|
|
||
| // Verify roundtrip | ||
| prop_assert_eq!(original, unpacked); | ||
| } | ||
| } | ||
| } |
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.