@@ -26,7 +26,7 @@ use std::os::raw::c_int;
2626#[ cfg( feature="blas" ) ]
2727use cblas_sys as blas_sys;
2828#[ cfg( feature="blas" ) ]
29- use cblas_sys:: { CblasNoTrans , CblasTrans , CblasRowMajor } ;
29+ use cblas_sys:: { CblasNoTrans , CblasTrans , CblasRowMajor , CBLAS_LAYOUT } ;
3030
3131/// len of vector before we use blas
3232#[ cfg( feature="blas" ) ]
@@ -578,25 +578,22 @@ pub fn general_mat_vec_mul<A, S1, S2, S3>(alpha: A,
578578 #[ cfg( feature = "blas" ) ]
579579 macro_rules! gemv {
580580 ( $ty: ty, $gemv: ident) => {
581- if blas_row_major_2d :: <$ty, _>( & a)
582- && blas_compat_1d:: <$ty, _>( & x)
581+ if let Some ( layout ) = blas_layout :: <$ty, _>( & a) {
582+ if blas_compat_1d:: <$ty, _>( & x)
583583 && blas_compat_1d:: <$ty, _>( & y)
584584 {
585- let mut a_trans = CblasNoTrans ;
586- let mut a = a. view( ) ;
587- let a_s0 = a. strides( ) [ 0 ] ;
588- if a_s0 == 1 && m == k {
589- a = a. reversed_axes( ) ;
590- a_trans = CblasTrans ;
591- }
592- // adjust strides, these may [1, 1] for column matrices
593- let a_stride = cmp:: max( a. strides( ) [ 0 ] as blas_index, k as blas_index) ;
585+ let a_trans = CblasNoTrans ;
586+ let a_stride = match layout {
587+ CBLAS_LAYOUT :: CblasRowMajor => a. strides( ) [ 0 ] as blas_index,
588+ CBLAS_LAYOUT :: CblasColMajor => a. strides( ) [ 1 ] as blas_index,
589+ } ;
590+
594591 let x_stride = x. strides( ) [ 0 ] as blas_index;
595592 let y_stride = y. strides( ) [ 0 ] as blas_index;
596593
597594 unsafe {
598595 blas_sys:: $gemv(
599- CblasRowMajor ,
596+ layout ,
600597 a_trans,
601598 m as blas_index, // m, rows of Op(a)
602599 k as blas_index, // n, cols of Op(a)
@@ -612,6 +609,7 @@ pub fn general_mat_vec_mul<A, S1, S2, S3>(alpha: A,
612609 }
613610 return ;
614611 }
612+ }
615613 }
616614 }
617615 #[ cfg( feature = "blas" ) ]
@@ -685,7 +683,7 @@ fn blas_row_major_2d<A, S>(a: &ArrayBase<S, Ix2>) -> bool
685683 let ( m, n) = a. dim ( ) ;
686684 let s0 = a. strides ( ) [ 0 ] ;
687685 let s1 = a. strides ( ) [ 1 ] ;
688- if ! ( s1 == 1 || n == 1 ) {
686+ if s1 != 1 {
689687 return false ;
690688 }
691689 if s0 < 1 || s1 < 1 {
@@ -716,7 +714,7 @@ fn blas_column_major_2d<A, S>(a: &ArrayBase<S, Ix2>) -> bool
716714 let ( m, n) = a. dim ( ) ;
717715 let s0 = a. strides ( ) [ 0 ] ;
718716 let s1 = a. strides ( ) [ 1 ] ;
719- if ! ( s0 == 1 || m == 1 ) {
717+ if s0 != 1 {
720718 return false ;
721719 }
722720 if s0 < 1 || s1 < 1 {
@@ -735,6 +733,21 @@ fn blas_column_major_2d<A, S>(a: &ArrayBase<S, Ix2>) -> bool
735733 true
736734}
737735
736+ #[ cfg( feature="blas" ) ]
737+ fn blas_layout < A , S > ( a : & ArrayBase < S , Ix2 > ) -> Option < CBLAS_LAYOUT >
738+ where S : Data ,
739+ A : ' static ,
740+ S :: Elem : ' static ,
741+ {
742+ if blas_row_major_2d :: < A , _ > ( a) {
743+ Some ( CBLAS_LAYOUT :: CblasRowMajor )
744+ } else if blas_column_major_2d :: < A , _ > ( a) {
745+ Some ( CBLAS_LAYOUT :: CblasColMajor )
746+ } else {
747+ None
748+ }
749+ }
750+
738751#[ cfg( test) ]
739752#[ cfg( feature="blas" ) ]
740753mod blas_tests {
@@ -751,7 +764,7 @@ mod blas_tests {
751764 fn blas_row_major_2d_row_matrix ( ) {
752765 let m: Array2 < f32 > = Array2 :: zeros ( ( 1 , 5 ) ) ;
753766 assert ! ( blas_row_major_2d:: <f32 , _>( & m) ) ;
754- assert ! ( blas_column_major_2d:: <f32 , _>( & m) ) ;
767+ assert ! ( ! blas_column_major_2d:: <f32 , _>( & m) ) ;
755768 }
756769
757770 #[ test]
@@ -765,16 +778,16 @@ mod blas_tests {
765778 fn blas_row_major_2d_transposed_row_matrix ( ) {
766779 let m: Array2 < f32 > = Array2 :: zeros ( ( 1 , 5 ) ) ;
767780 let m_t = m. t ( ) ;
768- assert ! ( blas_row_major_2d:: <f32 , _>( & m_t) ) ;
769- assert ! ( blas_column_major_2d:: <f32 , _>( & m ) ) ;
781+ assert ! ( ! blas_row_major_2d:: <f32 , _>( & m_t) ) ;
782+ assert ! ( blas_column_major_2d:: <f32 , _>( & m_t ) ) ;
770783 }
771784
772785 #[ test]
773786 fn blas_row_major_2d_transposed_column_matrix ( ) {
774787 let m: Array2 < f32 > = Array2 :: zeros ( ( 5 , 1 ) ) ;
775788 let m_t = m. t ( ) ;
776789 assert ! ( blas_row_major_2d:: <f32 , _>( & m_t) ) ;
777- assert ! ( blas_column_major_2d:: <f32 , _>( & m ) ) ;
790+ assert ! ( blas_column_major_2d:: <f32 , _>( & m_t ) ) ;
778791 }
779792
780793 #[ test]
0 commit comments