@@ -9,6 +9,83 @@ use crate::error::{Error, Result};
99use crate :: gemm:: { batched_gemm_f32, batched_gemm_f64} ;
1010use crate :: layout:: { DimMap , Layout , ViewSpec , broadcast_shapes} ;
1111
12+ /// The smallest and largest storage index a layout can reach, or `None` if it has no elements.
13+ fn layout_extent ( l : & Layout ) -> Option < ( isize , isize ) > {
14+ let ( mut lo, mut hi) = ( l. offset ( ) as isize , l. offset ( ) as isize ) ;
15+ for ( & d, & st) in l. shape ( ) . iter ( ) . zip ( l. strides ( ) ) {
16+ if d == 0 {
17+ return None ;
18+ }
19+ let span = ( d as isize - 1 ) * st as isize ;
20+ if span < 0 { lo += span } else { hi += span }
21+ }
22+ Some ( ( lo, hi) )
23+ }
24+
25+ /// Binary kernel for two arbitrary layouts of one shape (broadcast dims have stride 0, views any stride): the leading
26+ /// dimensions are walked row by row (in parallel for large outputs) and the last dimension runs as a tight loop, with
27+ /// slice-based fast paths for the stride pairs that occur in practice. `None` if a layout does not fit its storage.
28+ fn strided_binary < T , F > ( a : & [ T ] , la : & Layout , b : & [ T ] , lb : & Layout , f : F ) -> Option < Vec < T > >
29+ where
30+ T : Copy + Default + Send + Sync + ' static ,
31+ F : Fn ( T , T ) -> T + Sync ,
32+ {
33+ let shape = la. shape ( ) ;
34+ let nd = shape. len ( ) ;
35+ if nd == 0 || shape != lb. shape ( ) {
36+ return None ;
37+ }
38+ let ( alo, ahi) = layout_extent ( la) ?;
39+ let ( blo, bhi) = layout_extent ( lb) ?;
40+ if alo < 0 || blo < 0 || ahi as usize >= a. len ( ) || bhi as usize >= b. len ( ) {
41+ return None ;
42+ }
43+ let cols = shape[ nd - 1 ] ;
44+ let total: usize = shape. iter ( ) . product ( ) ;
45+ let ( sa, sb) = ( la. strides ( ) [ nd - 1 ] as isize , lb. strides ( ) [ nd - 1 ] as isize ) ;
46+ let lead = & shape[ ..nd - 1 ] ;
47+ let ( lsa, lsb) = ( & la. strides ( ) [ ..nd - 1 ] , & lb. strides ( ) [ ..nd - 1 ] ) ;
48+ let ( oa, ob) = ( la. offset ( ) as isize , lb. offset ( ) as isize ) ;
49+ let row = |r : usize , out : & mut [ T ] | {
50+ // storage offsets of the first element of row `r`
51+ let ( mut pa, mut pb, mut rem) = ( oa, ob, r) ;
52+ for d in ( 0 ..lead. len ( ) ) . rev ( ) {
53+ let c = ( rem % lead[ d] ) as isize ;
54+ rem /= lead[ d] ;
55+ pa += c * lsa[ d] as isize ;
56+ pb += c * lsb[ d] as isize ;
57+ }
58+ let ( pa, pb) = ( pa as usize , pb as usize ) ;
59+ match ( sa, sb) {
60+ ( 1 , 1 ) => out. iter_mut ( ) . zip ( & a[ pa..pa + cols] ) . zip ( & b[ pb..pb + cols] ) . for_each ( |( ( o, & x) , & y) | * o = f ( x, y) ) ,
61+ ( 1 , 0 ) => {
62+ let y = b[ pb] ;
63+ out. iter_mut ( ) . zip ( & a[ pa..pa + cols] ) . for_each ( |( o, & x) | * o = f ( x, y) ) ;
64+ }
65+ ( 0 , 1 ) => {
66+ let x = a[ pa] ;
67+ out. iter_mut ( ) . zip ( & b[ pb..pb + cols] ) . for_each ( |( o, & y) | * o = f ( x, y) ) ;
68+ }
69+ ( sa, 1 ) if sa > 1 => out. iter_mut ( ) . zip ( a[ pa..] . iter ( ) . step_by ( sa as usize ) ) . zip ( & b[ pb..pb + cols] ) . for_each ( |( ( o, & x) , & y) | * o = f ( x, y) ) ,
70+ ( 1 , sb) if sb > 1 => out. iter_mut ( ) . zip ( & a[ pa..pa + cols] ) . zip ( b[ pb..] . iter ( ) . step_by ( sb as usize ) ) . for_each ( |( ( o, & x) , & y) | * o = f ( x, y) ) ,
71+ _ => {
72+ for ( j, o) in out. iter_mut ( ) . enumerate ( ) {
73+ let j = j as isize ;
74+ * o = f ( a[ ( pa as isize + j * sa) as usize ] , b[ ( pb as isize + j * sb) as usize ] ) ;
75+ }
76+ }
77+ }
78+ } ;
79+ let mut out = crate :: pool:: take :: < T > ( total) . unwrap_or_else ( || vec ! [ T :: default ( ) ; total] ) ;
80+ #[ cfg( feature = "rayon" ) ]
81+ if total >= 16384 {
82+ out. par_chunks_mut ( cols) . enumerate ( ) . for_each ( |( r, o) | row ( r, o) ) ;
83+ return Some ( out) ;
84+ }
85+ out. chunks_mut ( cols) . enumerate ( ) . for_each ( |( r, o) | row ( r, o) ) ;
86+ Some ( out)
87+ }
88+
1289/// Host-memory storage supporting all 8 [`DType`] variants.
1390#[ derive( Clone , Debug , PartialEq ) ]
1491pub enum CpuStorage {
@@ -459,6 +536,13 @@ impl CpuStorage {
459536 macro_rules! run_unary {
460537 ( $variant: ident, $v: expr, $map_fn: expr) => { {
461538 if let Some ( range) = layout. contiguous_range_in( $v. len( ) ) {
539+ if let Some ( mut out) = crate :: pool:: take( range. len( ) ) {
540+ #[ cfg( feature = "rayon" ) ]
541+ out. par_iter_mut( ) . zip( $v[ range] . par_iter( ) ) . for_each( |( o, & x) | * o = ( $map_fn) ( x) ) ;
542+ #[ cfg( not( feature = "rayon" ) ) ]
543+ out. iter_mut( ) . zip( $v[ range] . iter( ) ) . for_each( |( o, & x) | * o = ( $map_fn) ( x) ) ;
544+ return Ok ( Self :: $variant( out) ) ;
545+ }
462546 #[ cfg( feature = "rayon" ) ]
463547 if range. len( ) >= 16384 {
464548 return Ok ( Self :: $variant(
@@ -612,6 +696,13 @@ impl CpuStorage {
612696 ( Some ( ra) , Some ( rb) ) => {
613697 let sa = & $a[ ra] ;
614698 let sb = & $b[ rb] ;
699+ if let Some ( mut out) = crate :: pool:: take( sa. len( ) . min( sb. len( ) ) ) {
700+ #[ cfg( feature = "rayon" ) ]
701+ out. par_iter_mut( ) . zip( sa. par_iter( ) . zip( sb. par_iter( ) ) ) . for_each( |( o, ( & x, & y) ) | * o = ( $f) ( x, y) ) ;
702+ #[ cfg( not( feature = "rayon" ) ) ]
703+ out. iter_mut( ) . zip( sa. iter( ) . zip( sb. iter( ) ) ) . for_each( |( o, ( & x, & y) ) | * o = ( $f) ( x, y) ) ;
704+ return Ok ( Self :: $variant( out) ) ;
705+ }
615706 #[ cfg( feature = "rayon" ) ]
616707 if sa. len( ) >= 16384 {
617708 return Ok ( Self :: $variant(
@@ -638,6 +729,13 @@ impl CpuStorage {
638729 let ra = lhs_l. contiguous_range_in( $a. len( ) ) . unwrap( ) ;
639730 let b_val = $b[ rhs_l. offset( ) ] ;
640731 let sa = & $a[ ra] ;
732+ if let Some ( mut out) = crate :: pool:: take( sa. len( ) ) {
733+ #[ cfg( feature = "rayon" ) ]
734+ out. par_iter_mut( ) . zip( sa. par_iter( ) ) . for_each( |( o, & x) | * o = ( $f) ( x, b_val) ) ;
735+ #[ cfg( not( feature = "rayon" ) ) ]
736+ out. iter_mut( ) . zip( sa. iter( ) ) . for_each( |( o, & x) | * o = ( $f) ( x, b_val) ) ;
737+ return Ok ( Self :: $variant( out) ) ;
738+ }
641739 #[ cfg( feature = "rayon" ) ]
642740 if sa. len( ) >= 16384 {
643741 return Ok ( Self :: $variant(
@@ -655,6 +753,13 @@ impl CpuStorage {
655753 let rb = rhs_l. contiguous_range_in( $b. len( ) ) . unwrap( ) ;
656754 let a_val = $a[ lhs_l. offset( ) ] ;
657755 let sb = & $b[ rb] ;
756+ if let Some ( mut out) = crate :: pool:: take( sb. len( ) ) {
757+ #[ cfg( feature = "rayon" ) ]
758+ out. par_iter_mut( ) . zip( sb. par_iter( ) ) . for_each( |( o, & y) | * o = ( $f) ( a_val, y) ) ;
759+ #[ cfg( not( feature = "rayon" ) ) ]
760+ out. iter_mut( ) . zip( sb. iter( ) ) . for_each( |( o, & y) | * o = ( $f) ( a_val, y) ) ;
761+ return Ok ( Self :: $variant( out) ) ;
762+ }
658763 #[ cfg( feature = "rayon" ) ]
659764 if sb. len( ) >= 16384 {
660765 return Ok ( Self :: $variant(
@@ -665,6 +770,10 @@ impl CpuStorage {
665770 sb. iter( ) . map( |& y| ( $f) ( a_val, y) ) . collect( ) ,
666771 ) ) ;
667772 }
773+ // Any other pair of strided / broadcast layouts: one tight loop per row of the last dimension
774+ if let Some ( out) = strided_binary( & $a[ ..] , lhs_l, & $b[ ..] , rhs_l, $f) {
775+ return Ok ( Self :: $variant( out) ) ;
776+ }
668777 // 3. Trailing row broadcast RHS: [.., cols] where RHS has stride 1 on last dim and 0 on preceding dims
669778 if !shape. is_empty( )
670779 && rhs_l. strides( ) . last( ) == Some ( & 1 )
0 commit comments