max@0: // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2008-2011 Conrad Sanderson max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: //! \addtogroup gemm max@0: //! @{ max@0: max@0: max@0: max@0: //! for tiny square matrices, size <= 4x4 max@0: template max@0: class gemm_emul_tinysq max@0: { max@0: public: max@0: max@0: max@0: template max@0: arma_hot max@0: inline max@0: static max@0: void max@0: apply max@0: ( max@0: Mat& C, max@0: const Mat& A, max@0: const Mat& B, max@0: const eT alpha = eT(1), max@0: const eT beta = eT(0) max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: switch(A.n_rows) max@0: { max@0: case 4: max@0: gemv_emul_tinysq::apply( C.colptr(3), A, B.colptr(3), alpha, beta ); max@0: max@0: case 3: max@0: gemv_emul_tinysq::apply( C.colptr(2), A, B.colptr(2), alpha, beta ); max@0: max@0: case 2: max@0: gemv_emul_tinysq::apply( C.colptr(1), A, B.colptr(1), alpha, beta ); max@0: max@0: case 1: max@0: gemv_emul_tinysq::apply( C.colptr(0), A, B.colptr(0), alpha, beta ); max@0: max@0: default: max@0: ; max@0: } max@0: } max@0: max@0: }; max@0: max@0: max@0: max@0: template max@0: class gemm_emul_large max@0: { max@0: public: max@0: max@0: template max@0: arma_hot max@0: inline max@0: static max@0: void max@0: apply max@0: ( max@0: Mat& C, max@0: const Mat& A, max@0: const Mat& B, max@0: const eT alpha = eT(1), max@0: const eT beta = eT(0) max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: const uword A_n_rows = A.n_rows; max@0: const uword A_n_cols = A.n_cols; max@0: max@0: const uword B_n_rows = B.n_rows; max@0: const uword B_n_cols = B.n_cols; max@0: max@0: if( (do_trans_A == false) && (do_trans_B == false) ) max@0: { max@0: arma_aligned podarray tmp(A_n_cols); max@0: eT* A_rowdata = tmp.memptr(); max@0: max@0: for(uword row_A=0; row_A < A_n_rows; ++row_A) max@0: { max@0: tmp.copy_row(A, row_A); max@0: max@0: for(uword col_B=0; col_B < B_n_cols; ++col_B) max@0: { max@0: const eT acc = op_dot::direct_dot_arma(B_n_rows, A_rowdata, B.colptr(col_B)); max@0: max@0: if( (use_alpha == false) && (use_beta == false) ) max@0: { max@0: C.at(row_A,col_B) = acc; max@0: } max@0: else max@0: if( (use_alpha == true) && (use_beta == false) ) max@0: { max@0: C.at(row_A,col_B) = alpha * acc; max@0: } max@0: else max@0: if( (use_alpha == false) && (use_beta == true) ) max@0: { max@0: C.at(row_A,col_B) = acc + beta*C.at(row_A,col_B); max@0: } max@0: else max@0: if( (use_alpha == true) && (use_beta == true) ) max@0: { max@0: C.at(row_A,col_B) = alpha*acc + beta*C.at(row_A,col_B); max@0: } max@0: max@0: } max@0: } max@0: } max@0: else max@0: if( (do_trans_A == true) && (do_trans_B == false) ) max@0: { max@0: for(uword col_A=0; col_A < A_n_cols; ++col_A) max@0: { max@0: // col_A is interpreted as row_A when storing the results in matrix C max@0: max@0: const eT* A_coldata = A.colptr(col_A); max@0: max@0: for(uword col_B=0; col_B < B_n_cols; ++col_B) max@0: { max@0: const eT acc = op_dot::direct_dot_arma(B_n_rows, A_coldata, B.colptr(col_B)); max@0: max@0: if( (use_alpha == false) && (use_beta == false) ) max@0: { max@0: C.at(col_A,col_B) = acc; max@0: } max@0: else max@0: if( (use_alpha == true) && (use_beta == false) ) max@0: { max@0: C.at(col_A,col_B) = alpha * acc; max@0: } max@0: else max@0: if( (use_alpha == false) && (use_beta == true) ) max@0: { max@0: C.at(col_A,col_B) = acc + beta*C.at(col_A,col_B); max@0: } max@0: else max@0: if( (use_alpha == true) && (use_beta == true) ) max@0: { max@0: C.at(col_A,col_B) = alpha*acc + beta*C.at(col_A,col_B); max@0: } max@0: max@0: } max@0: } max@0: } max@0: else max@0: if( (do_trans_A == false) && (do_trans_B == true) ) max@0: { max@0: Mat BB; max@0: op_strans::apply_noalias(BB, B); max@0: max@0: gemm_emul_large::apply(C, A, BB, alpha, beta); max@0: } max@0: else max@0: if( (do_trans_A == true) && (do_trans_B == true) ) max@0: { max@0: // mat B_tmp = trans(B); max@0: // dgemm_arma::apply(C, A, B_tmp, alpha, beta); max@0: max@0: max@0: // By using the trans(A)*trans(B) = trans(B*A) equivalency, max@0: // transpose operations are not needed max@0: max@0: arma_aligned podarray tmp(B.n_cols); max@0: eT* B_rowdata = tmp.memptr(); max@0: max@0: for(uword row_B=0; row_B < B_n_rows; ++row_B) max@0: { max@0: tmp.copy_row(B, row_B); max@0: max@0: for(uword col_A=0; col_A < A_n_cols; ++col_A) max@0: { max@0: const eT acc = op_dot::direct_dot_arma(A_n_rows, B_rowdata, A.colptr(col_A)); max@0: max@0: if( (use_alpha == false) && (use_beta == false) ) max@0: { max@0: C.at(col_A,row_B) = acc; max@0: } max@0: else max@0: if( (use_alpha == true) && (use_beta == false) ) max@0: { max@0: C.at(col_A,row_B) = alpha * acc; max@0: } max@0: else max@0: if( (use_alpha == false) && (use_beta == true) ) max@0: { max@0: C.at(col_A,row_B) = acc + beta*C.at(col_A,row_B); max@0: } max@0: else max@0: if( (use_alpha == true) && (use_beta == true) ) max@0: { max@0: C.at(col_A,row_B) = alpha*acc + beta*C.at(col_A,row_B); max@0: } max@0: max@0: } max@0: } max@0: max@0: } max@0: } max@0: max@0: }; max@0: max@0: max@0: max@0: template max@0: class gemm_emul max@0: { max@0: public: max@0: max@0: max@0: template max@0: arma_hot max@0: inline max@0: static max@0: void max@0: apply max@0: ( max@0: Mat& C, max@0: const Mat& A, max@0: const Mat& B, max@0: const eT alpha = eT(1), max@0: const eT beta = eT(0), max@0: const typename arma_not_cx::result* junk = 0 max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(junk); max@0: max@0: const uword A_n_rows = A.n_rows; max@0: const uword A_n_cols = A.n_cols; max@0: max@0: const uword B_n_rows = B.n_rows; max@0: const uword B_n_cols = B.n_cols; max@0: max@0: if( (A_n_rows <= 4) && (A_n_rows == A_n_cols) && (A_n_rows == B_n_rows) && (B_n_rows == B_n_cols) ) max@0: { max@0: if(do_trans_B == false) max@0: { max@0: gemm_emul_tinysq::apply(C, A, B, alpha, beta); max@0: } max@0: else max@0: { max@0: Mat BB(A_n_rows, A_n_rows); max@0: op_strans::apply_noalias_tinysq(BB, B); max@0: max@0: gemm_emul_tinysq::apply(C, A, BB, alpha, beta); max@0: } max@0: } max@0: else max@0: { max@0: gemm_emul_large::apply(C, A, B, alpha, beta); max@0: } max@0: } max@0: max@0: max@0: max@0: template max@0: arma_hot max@0: inline max@0: static max@0: void max@0: apply max@0: ( max@0: Mat& C, max@0: const Mat& A, max@0: const Mat& B, max@0: const eT alpha = eT(1), max@0: const eT beta = eT(0), max@0: const typename arma_cx_only::result* junk = 0 max@0: ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: arma_ignore(junk); max@0: max@0: // "better than nothing" handling of hermitian transposes for complex number matrices max@0: max@0: Mat tmp_A; max@0: Mat tmp_B; max@0: max@0: if(do_trans_A) max@0: { max@0: op_htrans::apply_noalias(tmp_A, A); max@0: } max@0: max@0: if(do_trans_B) max@0: { max@0: op_htrans::apply_noalias(tmp_B, B); max@0: } max@0: max@0: const Mat& AA = (do_trans_A == false) ? A : tmp_A; max@0: const Mat& BB = (do_trans_B == false) ? B : tmp_B; max@0: max@0: const uword A_n_rows = AA.n_rows; max@0: const uword A_n_cols = AA.n_cols; max@0: max@0: const uword B_n_rows = BB.n_rows; max@0: const uword B_n_cols = BB.n_cols; max@0: max@0: if( (A_n_rows <= 4) && (A_n_rows == A_n_cols) && (A_n_rows == B_n_rows) && (B_n_rows == B_n_cols) ) max@0: { max@0: gemm_emul_tinysq::apply(C, AA, BB, alpha, beta); max@0: } max@0: else max@0: { max@0: gemm_emul_large::apply(C, AA, BB, alpha, beta); max@0: } max@0: } max@0: max@0: }; max@0: max@0: max@0: max@0: //! \brief max@0: //! Wrapper for ATLAS/BLAS dgemm function, using template arguments to control the arguments passed to dgemm. max@0: //! Matrix 'C' is assumed to have been set to the correct size (i.e. taking into account transposes) max@0: max@0: template max@0: class gemm max@0: { max@0: public: max@0: max@0: template max@0: inline max@0: static max@0: void max@0: apply_blas_type( Mat& C, const Mat& A, const Mat& B, const eT alpha = eT(1), const eT beta = eT(0) ) max@0: { max@0: arma_extra_debug_sigprint(); max@0: max@0: if( (A.n_elem <= 48u) && (B.n_elem <= 48u) ) max@0: { max@0: gemm_emul::apply(C,A,B,alpha,beta); max@0: } max@0: else max@0: { max@0: #if defined(ARMA_USE_ATLAS) max@0: { max@0: arma_extra_debug_print("atlas::cblas_gemm()"); max@0: max@0: atlas::cblas_gemm max@0: ( max@0: atlas::CblasColMajor, max@0: (do_trans_A) ? ( is_complex::value ? CblasConjTrans : atlas::CblasTrans ) : atlas::CblasNoTrans, max@0: (do_trans_B) ? ( is_complex::value ? CblasConjTrans : atlas::CblasTrans ) : atlas::CblasNoTrans, max@0: C.n_rows, max@0: C.n_cols, max@0: (do_trans_A) ? A.n_rows : A.n_cols, max@0: (use_alpha) ? alpha : eT(1), max@0: A.mem, max@0: (do_trans_A) ? A.n_rows : C.n_rows, max@0: B.mem, max@0: (do_trans_B) ? C.n_cols : ( (do_trans_A) ? A.n_rows : A.n_cols ), max@0: (use_beta) ? beta : eT(0), max@0: C.memptr(), max@0: C.n_rows max@0: ); max@0: } max@0: #elif defined(ARMA_USE_BLAS) max@0: { max@0: arma_extra_debug_print("blas::gemm()"); max@0: max@0: const char trans_A = (do_trans_A) ? ( is_complex::value ? 'C' : 'T' ) : 'N'; max@0: const char trans_B = (do_trans_B) ? ( is_complex::value ? 'C' : 'T' ) : 'N'; max@0: max@0: const blas_int m = C.n_rows; max@0: const blas_int n = C.n_cols; max@0: const blas_int k = (do_trans_A) ? A.n_rows : A.n_cols; max@0: max@0: const eT local_alpha = (use_alpha) ? alpha : eT(1); max@0: max@0: const blas_int lda = (do_trans_A) ? k : m; max@0: const blas_int ldb = (do_trans_B) ? n : k; max@0: max@0: const eT local_beta = (use_beta) ? beta : eT(0); max@0: max@0: arma_extra_debug_print( arma_boost::format("blas::gemm(): trans_A = %c") % trans_A ); max@0: arma_extra_debug_print( arma_boost::format("blas::gemm(): trans_B = %c") % trans_B ); max@0: max@0: blas::gemm max@0: ( max@0: &trans_A, max@0: &trans_B, max@0: &m, max@0: &n, max@0: &k, max@0: &local_alpha, max@0: A.mem, max@0: &lda, max@0: B.mem, max@0: &ldb, max@0: &local_beta, max@0: C.memptr(), max@0: &m max@0: ); max@0: } max@0: #else max@0: { max@0: gemm_emul::apply(C,A,B,alpha,beta); max@0: } max@0: #endif max@0: } max@0: } max@0: max@0: max@0: max@0: //! immediate multiplication of matrices A and B, storing the result in C max@0: template max@0: inline max@0: static max@0: void max@0: apply( Mat& C, const Mat& A, const Mat& B, const eT alpha = eT(1), const eT beta = eT(0) ) max@0: { max@0: gemm_emul::apply(C,A,B,alpha,beta); max@0: } max@0: max@0: max@0: max@0: arma_inline max@0: static max@0: void max@0: apply max@0: ( max@0: Mat& C, max@0: const Mat& A, max@0: const Mat& B, max@0: const float alpha = float(1), max@0: const float beta = float(0) max@0: ) max@0: { max@0: gemm::apply_blas_type(C,A,B,alpha,beta); max@0: } max@0: max@0: max@0: max@0: arma_inline max@0: static max@0: void max@0: apply max@0: ( max@0: Mat& C, max@0: const Mat& A, max@0: const Mat& B, max@0: const double alpha = double(1), max@0: const double beta = double(0) max@0: ) max@0: { max@0: gemm::apply_blas_type(C,A,B,alpha,beta); max@0: } max@0: max@0: max@0: max@0: arma_inline max@0: static max@0: void max@0: apply max@0: ( max@0: Mat< std::complex >& C, max@0: const Mat< std::complex >& A, max@0: const Mat< std::complex >& B, max@0: const std::complex alpha = std::complex(1), max@0: const std::complex beta = std::complex(0) max@0: ) max@0: { max@0: gemm::apply_blas_type(C,A,B,alpha,beta); max@0: } max@0: max@0: max@0: max@0: arma_inline max@0: static max@0: void max@0: apply max@0: ( max@0: Mat< std::complex >& C, max@0: const Mat< std::complex >& A, max@0: const Mat< std::complex >& B, max@0: const std::complex alpha = std::complex(1), max@0: const std::complex beta = std::complex(0) max@0: ) max@0: { max@0: gemm::apply_blas_type(C,A,B,alpha,beta); max@0: } max@0: max@0: }; max@0: max@0: max@0: max@0: //! @}