1
// Copyright (C) 2008-2013 NICTA (www.nicta.com.au)
2
// Copyright (C) 2008-2013 Conrad Sanderson
4
// This Source Code Form is subject to the terms of the Mozilla Public
5
// License, v. 2.0. If a copy of the MPL was not distributed with this
6
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
14
//! for tiny square matrices, size <= 4x4
15
template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
16
class gemv_emul_tinysq
21
template<const uword row, const uword col>
24
static const uword n2 = (do_trans_A == false) ? (row + col*2) : (col + row*2);
25
static const uword n3 = (do_trans_A == false) ? (row + col*3) : (col + row*3);
26
static const uword n4 = (do_trans_A == false) ? (row + col*4) : (col + row*4);
31
template<typename eT, const uword i>
36
assign(eT* y, const eT acc, const eT alpha, const eT beta)
40
y[i] = (use_alpha == false) ? acc : alpha*acc;
46
y[i] = beta*tmp + ( (use_alpha == false) ? acc : alpha*acc );
52
template<typename eT, typename TA>
57
apply( eT* y, const TA& A, const eT* x, const eT alpha = eT(1), const eT beta = eT(0) )
59
arma_extra_debug_sigprint();
61
const eT* Am = A.memptr();
67
const eT acc = Am[0] * x[0];
69
assign<eT, 0>(y, acc, alpha, beta);
79
const eT acc0 = Am[pos<0,0>::n2]*x0 + Am[pos<0,1>::n2]*x1;
80
const eT acc1 = Am[pos<1,0>::n2]*x0 + Am[pos<1,1>::n2]*x1;
82
assign<eT, 0>(y, acc0, alpha, beta);
83
assign<eT, 1>(y, acc1, alpha, beta);
94
const eT acc0 = Am[pos<0,0>::n3]*x0 + Am[pos<0,1>::n3]*x1 + Am[pos<0,2>::n3]*x2;
95
const eT acc1 = Am[pos<1,0>::n3]*x0 + Am[pos<1,1>::n3]*x1 + Am[pos<1,2>::n3]*x2;
96
const eT acc2 = Am[pos<2,0>::n3]*x0 + Am[pos<2,1>::n3]*x1 + Am[pos<2,2>::n3]*x2;
98
assign<eT, 0>(y, acc0, alpha, beta);
99
assign<eT, 1>(y, acc1, alpha, beta);
100
assign<eT, 2>(y, acc2, alpha, beta);
112
const eT acc0 = Am[pos<0,0>::n4]*x0 + Am[pos<0,1>::n4]*x1 + Am[pos<0,2>::n4]*x2 + Am[pos<0,3>::n4]*x3;
113
const eT acc1 = Am[pos<1,0>::n4]*x0 + Am[pos<1,1>::n4]*x1 + Am[pos<1,2>::n4]*x2 + Am[pos<1,3>::n4]*x3;
114
const eT acc2 = Am[pos<2,0>::n4]*x0 + Am[pos<2,1>::n4]*x1 + Am[pos<2,2>::n4]*x2 + Am[pos<2,3>::n4]*x3;
115
const eT acc3 = Am[pos<3,0>::n4]*x0 + Am[pos<3,1>::n4]*x1 + Am[pos<3,2>::n4]*x2 + Am[pos<3,3>::n4]*x3;
117
assign<eT, 0>(y, acc0, alpha, beta);
118
assign<eT, 1>(y, acc1, alpha, beta);
119
assign<eT, 2>(y, acc2, alpha, beta);
120
assign<eT, 3>(y, acc3, alpha, beta);
134
class gemv_emul_large_helper
138
template<typename eT, typename TA>
142
typename arma_not_cx<eT>::result
143
dot_row_col( const TA& A, const eT* x, const uword row, const uword N )
149
for(i=0, j=1; j < N; i+=2, j+=2)
154
acc1 += A.at(row,i) * xi;
155
acc2 += A.at(row,j) * xj;
160
acc1 += A.at(row,i) * x[i];
163
return (acc1 + acc2);
168
template<typename eT, typename TA>
172
typename arma_cx_only<eT>::result
173
dot_row_col( const TA& A, const eT* x, const uword row, const uword N )
175
typedef typename get_pod_type<eT>::result T;
180
for(uword i=0; i<N; ++i)
182
const std::complex<T>& Ai = A.at(row,i);
183
const std::complex<T>& xi = x[i];
185
const T a = Ai.real();
186
const T b = Ai.imag();
188
const T c = xi.real();
189
const T d = xi.imag();
191
val_real += (a*c) - (b*d);
192
val_imag += (a*d) + (b*c);
195
return std::complex<T>(val_real, val_imag);
203
//! Partial emulation of ATLAS/BLAS gemv().
204
//! 'y' is assumed to have been set to the correct size (i.e. taking into account the transpose)
206
template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
207
class gemv_emul_large
211
template<typename eT, typename TA>
216
apply( eT* y, const TA& A, const eT* x, const eT alpha = eT(1), const eT beta = eT(0) )
218
arma_extra_debug_sigprint();
220
const uword A_n_rows = A.n_rows;
221
const uword A_n_cols = A.n_cols;
223
if(do_trans_A == false)
227
const eT acc = op_dot::direct_dot_arma(A_n_cols, A.memptr(), x);
229
if( (use_alpha == false) && (use_beta == false) )
234
if( (use_alpha == true) && (use_beta == false) )
239
if( (use_alpha == false) && (use_beta == true) )
241
y[0] = acc + beta*y[0];
244
if( (use_alpha == true) && (use_beta == true) )
246
y[0] = alpha*acc + beta*y[0];
250
for(uword row=0; row < A_n_rows; ++row)
252
const eT acc = gemv_emul_large_helper::dot_row_col(A, x, row, A_n_cols);
254
if( (use_alpha == false) && (use_beta == false) )
259
if( (use_alpha == true) && (use_beta == false) )
261
y[row] = alpha * acc;
264
if( (use_alpha == false) && (use_beta == true) )
266
y[row] = acc + beta*y[row];
269
if( (use_alpha == true) && (use_beta == true) )
271
y[row] = alpha*acc + beta*y[row];
276
if(do_trans_A == true)
278
for(uword col=0; col < A_n_cols; ++col)
280
// col is interpreted as row when storing the results in 'y'
283
// const eT* A_coldata = A.colptr(col);
286
// for(uword row=0; row < A_n_rows; ++row)
288
// acc += A_coldata[row] * x[row];
291
const eT acc = op_dot::direct_dot_arma(A_n_rows, A.colptr(col), x);
293
if( (use_alpha == false) && (use_beta == false) )
298
if( (use_alpha == true) && (use_beta == false) )
300
y[col] = alpha * acc;
303
if( (use_alpha == false) && (use_beta == true) )
305
y[col] = acc + beta*y[col];
308
if( (use_alpha == true) && (use_beta == true) )
310
y[col] = alpha*acc + beta*y[col];
321
template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
326
template<typename eT, typename TA>
331
apply( eT* y, const TA& A, const eT* x, const eT alpha = eT(1), const eT beta = eT(0), const typename arma_not_cx<eT>::result* junk = 0 )
333
arma_extra_debug_sigprint();
336
const uword A_n_rows = A.n_rows;
337
const uword A_n_cols = A.n_cols;
339
if( (A_n_rows <= 4) && (A_n_rows == A_n_cols) )
341
gemv_emul_tinysq<do_trans_A, use_alpha, use_beta>::apply(y, A, x, alpha, beta);
345
gemv_emul_large<do_trans_A, use_alpha, use_beta>::apply(y, A, x, alpha, beta);
351
template<typename eT>
356
apply( eT* y, const Mat<eT>& A, const eT* x, const eT alpha = eT(1), const eT beta = eT(0), const typename arma_cx_only<eT>::result* junk = 0 )
358
arma_extra_debug_sigprint();
365
op_htrans::apply_noalias(tmp_A, A);
368
const Mat<eT>& AA = (do_trans_A == false) ? A : tmp_A;
370
const uword AA_n_rows = AA.n_rows;
371
const uword AA_n_cols = AA.n_cols;
373
if( (AA_n_rows <= 4) && (AA_n_rows == AA_n_cols) )
375
gemv_emul_tinysq<false, use_alpha, use_beta>::apply(y, AA, x, alpha, beta);
379
gemv_emul_large<false, use_alpha, use_beta>::apply(y, AA, x, alpha, beta);
387
//! Wrapper for ATLAS/BLAS gemv function, using template arguments to control the arguments passed to gemv.
388
//! 'y' is assumed to have been set to the correct size (i.e. taking into account the transpose)
390
template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
395
template<typename eT, typename TA>
399
apply_blas_type( eT* y, const TA& A, const eT* x, const eT alpha = eT(1), const eT beta = eT(0) )
401
arma_extra_debug_sigprint();
403
//const uword threshold = (is_complex<eT>::value == true) ? 16u : 64u;
404
const uword threshold = (is_complex<eT>::value == true) ? 64u : 100u;
406
if(A.n_elem <= threshold)
408
gemv_emul<do_trans_A, use_alpha, use_beta>::apply(y,A,x,alpha,beta);
412
#if defined(ARMA_USE_ATLAS)
414
if(is_complex<eT>::value == false)
416
// use gemm() instead of gemv() to work around a speed issue in Atlas 3.8.4
418
arma_extra_debug_print("atlas::cblas_gemm()");
420
atlas::cblas_gemm<eT>
422
atlas::CblasColMajor,
423
(do_trans_A) ? ( is_complex<eT>::value ? CblasConjTrans : atlas::CblasTrans ) : atlas::CblasNoTrans,
425
(do_trans_A) ? A.n_cols : A.n_rows,
427
(do_trans_A) ? A.n_rows : A.n_cols,
428
(use_alpha) ? alpha : eT(1),
432
(do_trans_A) ? A.n_rows : A.n_cols,
433
(use_beta) ? beta : eT(0),
435
(do_trans_A) ? A.n_cols : A.n_rows
440
arma_extra_debug_print("atlas::cblas_gemv()");
442
atlas::cblas_gemv<eT>
444
atlas::CblasColMajor,
445
(do_trans_A) ? ( is_complex<eT>::value ? CblasConjTrans : atlas::CblasTrans ) : atlas::CblasNoTrans,
448
(use_alpha) ? alpha : eT(1),
453
(use_beta) ? beta : eT(0),
459
#elif defined(ARMA_USE_BLAS)
461
arma_extra_debug_print("blas::gemv()");
463
const char trans_A = (do_trans_A) ? ( is_complex<eT>::value ? 'C' : 'T' ) : 'N';
464
const blas_int m = A.n_rows;
465
const blas_int n = A.n_cols;
466
const eT local_alpha = (use_alpha) ? alpha : eT(1);
467
//const blas_int lda = A.n_rows;
468
const blas_int inc = 1;
469
const eT local_beta = (use_beta) ? beta : eT(0);
471
arma_extra_debug_print( arma_boost::format("blas::gemv(): trans_A = %c") % trans_A );
490
gemv_emul<do_trans_A, use_alpha, use_beta>::apply(y,A,x,alpha,beta);
499
template<typename eT, typename TA>
503
apply( eT* y, const TA& A, const eT* x, const eT alpha = eT(1), const eT beta = eT(0) )
505
gemv_emul<do_trans_A, use_alpha, use_beta>::apply(y,A,x,alpha,beta);
510
template<typename TA>
519
const float alpha = float(1),
520
const float beta = float(0)
523
gemv<do_trans_A, use_alpha, use_beta>::apply_blas_type(y,A,x,alpha,beta);
528
template<typename TA>
537
const double alpha = double(1),
538
const double beta = double(0)
541
gemv<do_trans_A, use_alpha, use_beta>::apply_blas_type(y,A,x,alpha,beta);
546
template<typename TA>
552
std::complex<float>* y,
554
const std::complex<float>* x,
555
const std::complex<float> alpha = std::complex<float>(1),
556
const std::complex<float> beta = std::complex<float>(0)
559
gemv<do_trans_A, use_alpha, use_beta>::apply_blas_type(y,A,x,alpha,beta);
564
template<typename TA>
570
std::complex<double>* y,
572
const std::complex<double>* x,
573
const std::complex<double> alpha = std::complex<double>(1),
574
const std::complex<double> beta = std::complex<double>(0)
577
gemv<do_trans_A, use_alpha, use_beta>::apply_blas_type(y,A,x,alpha,beta);