@@ -5,6 +5,8 @@ use ndarray::prelude::*;
55
66const M : usize = 1024 * 10 ;
77const N : usize = 100 ;
8+ const CHUNK_SIZE : usize = 100 ;
9+ const N_CHUNKS : usize = ( M + CHUNK_SIZE - 1 ) / CHUNK_SIZE ;
810
911#[ test]
1012fn test_axis_iter ( ) {
@@ -53,3 +55,34 @@ fn test_regular_iter_collect() {
5355 let v = a. view ( ) . into_par_iter ( ) . map ( |& x| x) . collect :: < Vec < _ > > ( ) ;
5456 assert_eq ! ( v. len( ) , a. len( ) ) ;
5557}
58+
59+ #[ test]
60+ fn test_axis_chunks_iter ( ) {
61+ let mut a = Array2 :: < f64 > :: zeros ( ( M , N ) ) ;
62+ for ( i, mut v) in a. axis_chunks_iter_mut ( Axis ( 0 ) , CHUNK_SIZE ) . enumerate ( ) {
63+ v. fill ( i as _ ) ;
64+ }
65+ assert_eq ! ( a. axis_chunks_iter( Axis ( 0 ) , CHUNK_SIZE ) . len( ) , N_CHUNKS ) ;
66+ let s: f64 = a
67+ . axis_chunks_iter ( Axis ( 0 ) , CHUNK_SIZE )
68+ . into_par_iter ( )
69+ . map ( |x| x. sum ( ) )
70+ . sum ( ) ;
71+ println ! ( "{:?}" , a. slice( s![ ..10 , ..5 ] ) ) ;
72+ assert_eq ! ( s, a. sum( ) ) ;
73+ }
74+
75+ #[ test]
76+ #[ cfg( feature = "approx" ) ]
77+ fn test_axis_chunks_iter_mut ( ) {
78+ use approx:: assert_abs_diff_eq;
79+ let mut a = Array :: linspace ( 0. , 1.0f64 , M * N )
80+ . into_shape ( ( M , N ) )
81+ . unwrap ( ) ;
82+ let b = a. mapv ( |x| x. exp ( ) ) ;
83+ a. axis_chunks_iter_mut ( Axis ( 0 ) , CHUNK_SIZE )
84+ . into_par_iter ( )
85+ . for_each ( |mut v| v. mapv_inplace ( |x| x. exp ( ) ) ) ;
86+ println ! ( "{:?}" , a. slice( s![ ..10 , ..5 ] ) ) ;
87+ assert_abs_diff_eq ! ( a, b, epsilon = 0.001 ) ;
88+ }
0 commit comments