File tree Expand file tree Collapse file tree 3 files changed +49
-10
lines changed Expand file tree Collapse file tree 3 files changed +49
-10
lines changed Original file line number Diff line number Diff line change @@ -370,3 +370,23 @@ fn iter_axis_chunks_5_iter_sum(bench: &mut Bencher) {
370
370
. sum :: < f32 > ( )
371
371
} ) ;
372
372
}
373
+
374
+ pub fn zip_mut_with ( data : & Array3 < f32 > , out : & mut Array3 < f32 > ) {
375
+ out. zip_mut_with ( & data, |o, & i| {
376
+ * o = i;
377
+ } ) ;
378
+ }
379
+
380
+ #[ bench]
381
+ fn zip_mut_with_cc ( b : & mut Bencher ) {
382
+ let data: Array3 < f32 > = Array3 :: zeros ( ( ISZ , ISZ , ISZ ) ) ;
383
+ let mut out = Array3 :: zeros ( data. dim ( ) ) ;
384
+ b. iter ( || black_box ( zip_mut_with ( & data, & mut out) ) ) ;
385
+ }
386
+
387
+ #[ bench]
388
+ fn zip_mut_with_ff ( b : & mut Bencher ) {
389
+ let data: Array3 < f32 > = Array3 :: zeros ( ( ISZ , ISZ , ISZ ) . f ( ) ) ;
390
+ let mut out = Array3 :: zeros ( data. dim ( ) . f ( ) ) ;
391
+ b. iter ( || black_box ( zip_mut_with ( & data, & mut out) ) ) ;
392
+ }
Original file line number Diff line number Diff line change @@ -229,6 +229,25 @@ pub trait Dimension:
229
229
!end_iteration
230
230
}
231
231
232
+ /// Returns `true` iff `strides1` and `strides2` are equivalent for the
233
+ /// shape `self`.
234
+ ///
235
+ /// The strides are equivalent if, for each axis with length > 1, the
236
+ /// strides are equal.
237
+ ///
238
+ /// Note: Returns `false` if any of the ndims don't match.
239
+ #[ doc( hidden) ]
240
+ fn strides_equivalent < D > ( & self , strides1 : & Self , strides2 : & D ) -> bool
241
+ where
242
+ D : Dimension ,
243
+ {
244
+ let shape_ndim = self . ndim ( ) ;
245
+ shape_ndim == strides1. ndim ( )
246
+ && shape_ndim == strides2. ndim ( )
247
+ && izip ! ( self . slice( ) , strides1. slice( ) , strides2. slice( ) )
248
+ . all ( |( & d, & s1, & s2) | d <= 1 || s1 as isize == s2 as isize )
249
+ }
250
+
232
251
#[ doc( hidden) ]
233
252
/// Return stride offset for index.
234
253
fn stride_offset ( index : & Self , strides : & Self ) -> isize {
Original file line number Diff line number Diff line change 6
6
// option. This file may not be copied, modified, or distributed
7
7
// except according to those terms.
8
8
9
- use std:: cmp;
10
9
use std:: ptr as std_ptr;
11
10
use std:: slice;
12
11
@@ -1937,18 +1936,19 @@ where
1937
1936
F : FnMut ( & mut A , & B ) ,
1938
1937
{
1939
1938
debug_assert_eq ! ( self . shape( ) , rhs. shape( ) ) ;
1940
- if let Some ( self_s) = self . as_slice_mut ( ) {
1941
- if let Some ( rhs_s) = rhs. as_slice ( ) {
1942
- let len = cmp:: min ( self_s. len ( ) , rhs_s. len ( ) ) ;
1943
- let s = & mut self_s[ ..len] ;
1944
- let r = & rhs_s[ ..len] ;
1945
- for i in 0 ..len {
1946
- f ( & mut s[ i] , & r[ i] ) ;
1939
+
1940
+ if self . dim . strides_equivalent ( & self . strides , & rhs. strides ) {
1941
+ if let Some ( self_s) = self . as_slice_memory_order_mut ( ) {
1942
+ if let Some ( rhs_s) = rhs. as_slice_memory_order ( ) {
1943
+ for ( s, r) in self_s. iter_mut ( ) . zip ( rhs_s) {
1944
+ f ( s, & r) ;
1945
+ }
1946
+ return ;
1947
1947
}
1948
- return ;
1949
1948
}
1950
1949
}
1951
- // otherwise, fall back to the outer iter
1950
+
1951
+ // Otherwise, fall back to the outer iter
1952
1952
self . zip_mut_with_by_rows ( rhs, f) ;
1953
1953
}
1954
1954
You can’t perform that action at this time.
0 commit comments