1
// Copyright (C) 2013 Conrad Sanderson
2
// Copyright (C) 2013 NICTA (www.nicta.com.au)
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/.
22
inplace_copy_upper_tri_to_lower_tri(Mat<eT>& C)
24
// under the assumption that C is a square matrix
26
const uword N = C.n_rows;
28
for(uword k=0; k < N; ++k)
30
eT* colmem = C.colptr(k);
33
for(i=(k+1), j=(k+2); j < N; i+=2, j+=2)
35
const eT tmp_i = C.at(k,i);
36
const eT tmp_j = C.at(k,j);
44
colmem[i] = C.at(k,i);
52
//! partial emulation of BLAS function syrk(), specialised for A being a vector
53
template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
58
template<typename eT, typename TA>
67
const eT alpha = eT(1),
71
arma_extra_debug_sigprint();
73
const uword A_n1 = (do_trans_A == false) ? A.n_rows : A.n_cols;
74
const uword A_n2 = (do_trans_A == false) ? A.n_cols : A.n_rows;
76
const eT* A_mem = A.memptr();
80
const eT acc1 = op_dot::direct_dot(A_n2, A_mem, A_mem);
82
if( (use_alpha == false) && (use_beta == false) ) { C[0] = acc1; }
83
else if( (use_alpha == true ) && (use_beta == false) ) { C[0] = alpha*acc1; }
84
else if( (use_alpha == false) && (use_beta == true ) ) { C[0] = acc1 + beta*C[0]; }
85
else if( (use_alpha == true ) && (use_beta == true ) ) { C[0] = alpha*acc1 + beta*C[0]; }
88
for(uword k=0; k < A_n1; ++k)
90
const eT A_k = A_mem[k];
93
for(i=(k), j=(k+1); j < A_n1; i+=2, j+=2)
95
const eT acc1 = A_k * A_mem[i];
96
const eT acc2 = A_k * A_mem[j];
98
if( (use_alpha == false) && (use_beta == false) )
107
if( (use_alpha == true ) && (use_beta == false) )
109
const eT val1 = alpha*acc1;
110
const eT val2 = alpha*acc2;
119
if( (use_alpha == false) && (use_beta == true) )
121
C.at(k, i) = acc1 + beta*C.at(k, i);
122
C.at(k, j) = acc2 + beta*C.at(k, j);
124
if(i != k) { C.at(i, k) = acc1 + beta*C.at(i, k); }
125
C.at(j, k) = acc2 + beta*C.at(j, k);
128
if( (use_alpha == true ) && (use_beta == true) )
130
const eT val1 = alpha*acc1;
131
const eT val2 = alpha*acc2;
133
C.at(k, i) = val1 + beta*C.at(k, i);
134
C.at(k, j) = val2 + beta*C.at(k, j);
136
if(i != k) { C.at(i, k) = val1 + beta*C.at(i, k); }
137
C.at(j, k) = val2 + beta*C.at(j, k);
143
const eT acc1 = A_k * A_mem[i];
145
if( (use_alpha == false) && (use_beta == false) )
151
if( (use_alpha == true) && (use_beta == false) )
153
const eT val1 = alpha*acc1;
159
if( (use_alpha == false) && (use_beta == true) )
161
C.at(k, i) = acc1 + beta*C.at(k, i);
162
if(i != k) { C.at(i, k) = acc1 + beta*C.at(i, k); }
165
if( (use_alpha == true) && (use_beta == true) )
167
const eT val1 = alpha*acc1;
169
C.at(k, i) = val1 + beta*C.at(k, i);
170
if(i != k) { C.at(i, k) = val1 + beta*C.at(i, k); }
180
//! partial emulation of BLAS function syrk()
181
template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
186
template<typename eT, typename TA>
195
const eT alpha = eT(1),
196
const eT beta = eT(0)
199
arma_extra_debug_sigprint();
201
// do_trans_A == false -> C = alpha * A * A^T + beta*C
202
// do_trans_A == true -> C = alpha * A^T * A + beta*C
204
if(do_trans_A == false)
208
op_strans::apply_noalias(AA, A);
210
syrk_emul<true, use_alpha, use_beta>::apply(C, AA, alpha, beta);
213
if(do_trans_A == true)
215
const uword A_n_rows = A.n_rows;
216
const uword A_n_cols = A.n_cols;
218
for(uword col_A=0; col_A < A_n_cols; ++col_A)
220
// col_A is interpreted as row_A when storing the results in matrix C
222
const eT* A_coldata = A.colptr(col_A);
224
for(uword k=col_A; k < A_n_cols; ++k)
226
const eT acc = op_dot::direct_dot_arma(A_n_rows, A_coldata, A.colptr(k));
228
if( (use_alpha == false) && (use_beta == false) )
230
C.at(col_A, k) = acc;
231
C.at(k, col_A) = acc;
234
if( (use_alpha == true ) && (use_beta == false) )
236
const eT val = alpha*acc;
238
C.at(col_A, k) = val;
239
C.at(k, col_A) = val;
242
if( (use_alpha == false) && (use_beta == true ) )
244
C.at(col_A, k) = acc + beta*C.at(col_A, k);
245
if(col_A != k) { C.at(k, col_A) = acc + beta*C.at(k, col_A); }
248
if( (use_alpha == true ) && (use_beta == true ) )
250
const eT val = alpha*acc;
252
C.at(col_A, k) = val + beta*C.at(col_A, k);
253
if(col_A != k) { C.at(k, col_A) = val + beta*C.at(k, col_A); }
264
template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
269
template<typename eT, typename TA>
273
apply_blas_type( Mat<eT>& C, const TA& A, const eT alpha = eT(1), const eT beta = eT(0) )
275
arma_extra_debug_sigprint();
279
// work around poor handling of vectors by syrk() in ATLAS 3.8.4 and standard BLAS
281
syrk_vec<do_trans_A, use_alpha, use_beta>::apply(C,A,alpha,beta);
286
const uword threshold = (is_cx<eT>::yes ? 16u : 48u);
288
if( A.n_elem <= threshold )
290
syrk_emul<do_trans_A, use_alpha, use_beta>::apply(C,A,alpha,beta);
294
#if defined(ARMA_USE_ATLAS)
298
// use a temporary matrix, as we can't assume that matrix C is already symmetric
299
Mat<eT> D(C.n_rows, C.n_cols);
301
syrk<do_trans_A, use_alpha, false>::apply_blas_type(D,A,alpha);
303
// NOTE: assuming beta=1; this is okay for now, as currently glue_times only uses beta=1
304
arrayops::inplace_plus(C.memptr(), D.memptr(), C.n_elem);
309
atlas::cblas_syrk<eT>
311
atlas::CblasColMajor,
313
(do_trans_A) ? atlas::CblasTrans : atlas::CblasNoTrans,
315
(do_trans_A) ? A.n_rows : A.n_cols,
316
(use_alpha) ? alpha : eT(1),
318
(do_trans_A) ? A.n_rows : C.n_cols,
319
(use_beta) ? beta : eT(0),
324
syrk_helper::inplace_copy_upper_tri_to_lower_tri(C);
326
#elif defined(ARMA_USE_BLAS)
330
// use a temporary matrix, as we can't assume that matrix C is already symmetric
331
Mat<eT> D(C.n_rows, C.n_cols);
333
syrk<do_trans_A, use_alpha, false>::apply_blas_type(D,A,alpha);
335
// NOTE: assuming beta=1; this is okay for now, as currently glue_times only uses beta=1
336
arrayops::inplace_plus(C.memptr(), D.memptr(), C.n_elem);
341
arma_extra_debug_print("blas::syrk()");
343
const char uplo = 'U';
345
const char trans_A = (do_trans_A) ? 'T' : 'N';
347
const blas_int n = C.n_cols;
348
const blas_int k = (do_trans_A) ? A.n_rows : A.n_cols;
350
const eT local_alpha = (use_alpha) ? alpha : eT(1);
351
const eT local_beta = (use_beta) ? beta : eT(0);
353
const blas_int lda = (do_trans_A) ? k : n;
355
arma_extra_debug_print( arma_boost::format("blas::syrk(): trans_A = %c") % trans_A );
371
syrk_helper::inplace_copy_upper_tri_to_lower_tri(C);
375
syrk_emul<do_trans_A, use_alpha, use_beta>::apply(C,A,alpha,beta);
383
template<typename eT, typename TA>
387
apply( Mat<eT>& C, const TA& A, const eT alpha = eT(1), const eT beta = eT(0) )
393
syrk_vec<do_trans_A, use_alpha, use_beta>::apply(C,A,alpha,beta);
397
syrk_emul<do_trans_A, use_alpha, use_beta>::apply(C,A,alpha,beta);
402
// handling of complex matrix by syrk_emul() is not yet implemented
409
template<typename TA>
417
const float alpha = float(1),
418
const float beta = float(0)
421
syrk<do_trans_A, use_alpha, use_beta>::apply_blas_type(C,A,alpha,beta);
426
template<typename TA>
434
const double alpha = double(1),
435
const double beta = double(0)
438
syrk<do_trans_A, use_alpha, use_beta>::apply_blas_type(C,A,alpha,beta);
443
template<typename TA>
449
Mat< std::complex<float> >& C,
451
const std::complex<float> alpha = std::complex<float>(1),
452
const std::complex<float> beta = std::complex<float>(0)
460
// handling of complex matrix by syrk() is not yet implemented
466
template<typename TA>
472
Mat< std::complex<double> >& C,
474
const std::complex<double> alpha = std::complex<double>(1),
475
const std::complex<double> beta = std::complex<double>(0)
483
// handling of complex matrix by syrk() is not yet implemented