mul_herk.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup herk
  16. //! @{
  17. class herk_helper
  18. {
  19. public:
  20. template<typename eT>
  21. inline
  22. static
  23. void
  24. inplace_conj_copy_upper_tri_to_lower_tri(Mat<eT>& C)
  25. {
  26. // under the assumption that C is a square matrix
  27. const uword N = C.n_rows;
  28. for(uword k=0; k < N; ++k)
  29. {
  30. eT* colmem = C.colptr(k);
  31. for(uword i=(k+1); i < N; ++i)
  32. {
  33. colmem[i] = std::conj( C.at(k,i) );
  34. }
  35. }
  36. }
  37. template<typename eT>
  38. static
  39. arma_hot
  40. inline
  41. eT
  42. dot_conj_row(const uword n_elem, const eT* const A, const Mat<eT>& B, const uword row)
  43. {
  44. arma_extra_debug_sigprint();
  45. typedef typename get_pod_type<eT>::result T;
  46. T val_real = T(0);
  47. T val_imag = T(0);
  48. for(uword i=0; i<n_elem; ++i)
  49. {
  50. const std::complex<T>& X = A[i];
  51. const std::complex<T>& Y = B.at(row,i);
  52. const T a = X.real();
  53. const T b = X.imag();
  54. const T c = Y.real();
  55. const T d = Y.imag();
  56. val_real += (a*c) + (b*d);
  57. val_imag += (b*c) - (a*d);
  58. }
  59. return std::complex<T>(val_real, val_imag);
  60. }
  61. };
  62. template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
  63. class herk_vec
  64. {
  65. public:
  66. template<typename T, typename TA>
  67. arma_hot
  68. inline
  69. static
  70. void
  71. apply
  72. (
  73. Mat< std::complex<T> >& C,
  74. const TA& A,
  75. const T alpha = T(1),
  76. const T beta = T(0)
  77. )
  78. {
  79. arma_extra_debug_sigprint();
  80. typedef std::complex<T> eT;
  81. const uword A_n_rows = A.n_rows;
  82. const uword A_n_cols = A.n_cols;
  83. // for beta != 0, C is assumed to be hermitian
  84. // do_trans_A == false -> C = alpha * A * A^H + beta*C
  85. // do_trans_A == true -> C = alpha * A^H * A + beta*C
  86. const eT* A_mem = A.memptr();
  87. if(do_trans_A == false)
  88. {
  89. if(A_n_rows == 1)
  90. {
  91. const eT acc = op_cdot::direct_cdot(A_n_cols, A_mem, A_mem);
  92. if( (use_alpha == false) && (use_beta == false) ) { C[0] = acc; }
  93. else if( (use_alpha == true ) && (use_beta == false) ) { C[0] = alpha*acc; }
  94. else if( (use_alpha == false) && (use_beta == true ) ) { C[0] = acc + beta*C[0]; }
  95. else if( (use_alpha == true ) && (use_beta == true ) ) { C[0] = alpha*acc + beta*C[0]; }
  96. }
  97. else
  98. for(uword row_A=0; row_A < A_n_rows; ++row_A)
  99. {
  100. const eT& A_rowdata = A_mem[row_A];
  101. for(uword k=row_A; k < A_n_rows; ++k)
  102. {
  103. const eT acc = A_rowdata * std::conj( A_mem[k] );
  104. if( (use_alpha == false) && (use_beta == false) )
  105. {
  106. C.at(row_A, k) = acc;
  107. if(row_A != k) { C.at(k, row_A) = std::conj(acc); }
  108. }
  109. else
  110. if( (use_alpha == true) && (use_beta == false) )
  111. {
  112. const eT val = alpha*acc;
  113. C.at(row_A, k) = val;
  114. if(row_A != k) { C.at(k, row_A) = std::conj(val); }
  115. }
  116. else
  117. if( (use_alpha == false) && (use_beta == true) )
  118. {
  119. C.at(row_A, k) = acc + beta*C.at(row_A, k);
  120. if(row_A != k) { C.at(k, row_A) = std::conj(acc) + beta*C.at(k, row_A); }
  121. }
  122. else
  123. if( (use_alpha == true) && (use_beta == true) )
  124. {
  125. const eT val = alpha*acc;
  126. C.at(row_A, k) = val + beta*C.at(row_A, k);
  127. if(row_A != k) { C.at(k, row_A) = std::conj(val) + beta*C.at(k, row_A); }
  128. }
  129. }
  130. }
  131. }
  132. else
  133. if(do_trans_A == true)
  134. {
  135. if(A_n_cols == 1)
  136. {
  137. const eT acc = op_cdot::direct_cdot(A_n_rows, A_mem, A_mem);
  138. if( (use_alpha == false) && (use_beta == false) ) { C[0] = acc; }
  139. else if( (use_alpha == true ) && (use_beta == false) ) { C[0] = alpha*acc; }
  140. else if( (use_alpha == false) && (use_beta == true ) ) { C[0] = acc + beta*C[0]; }
  141. else if( (use_alpha == true ) && (use_beta == true ) ) { C[0] = alpha*acc + beta*C[0]; }
  142. }
  143. else
  144. for(uword col_A=0; col_A < A_n_cols; ++col_A)
  145. {
  146. // col_A is interpreted as row_A when storing the results in matrix C
  147. const eT A_coldata = std::conj( A_mem[col_A] );
  148. for(uword k=col_A; k < A_n_cols ; ++k)
  149. {
  150. const eT acc = A_coldata * A_mem[k];
  151. if( (use_alpha == false) && (use_beta == false) )
  152. {
  153. C.at(col_A, k) = acc;
  154. if(col_A != k) { C.at(k, col_A) = std::conj(acc); }
  155. }
  156. else
  157. if( (use_alpha == true ) && (use_beta == false) )
  158. {
  159. const eT val = alpha*acc;
  160. C.at(col_A, k) = val;
  161. if(col_A != k) { C.at(k, col_A) = std::conj(val); }
  162. }
  163. else
  164. if( (use_alpha == false) && (use_beta == true ) )
  165. {
  166. C.at(col_A, k) = acc + beta*C.at(col_A, k);
  167. if(col_A != k) { C.at(k, col_A) = std::conj(acc) + beta*C.at(k, col_A); }
  168. }
  169. else
  170. if( (use_alpha == true ) && (use_beta == true ) )
  171. {
  172. const eT val = alpha*acc;
  173. C.at(col_A, k) = val + beta*C.at(col_A, k);
  174. if(col_A != k) { C.at(k, col_A) = std::conj(val) + beta*C.at(k, col_A); }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. };
  181. template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
  182. class herk_emul
  183. {
  184. public:
  185. template<typename T, typename TA>
  186. arma_hot
  187. inline
  188. static
  189. void
  190. apply
  191. (
  192. Mat< std::complex<T> >& C,
  193. const TA& A,
  194. const T alpha = T(1),
  195. const T beta = T(0)
  196. )
  197. {
  198. arma_extra_debug_sigprint();
  199. typedef std::complex<T> eT;
  200. // do_trans_A == false -> C = alpha * A * A^H + beta*C
  201. // do_trans_A == true -> C = alpha * A^H * A + beta*C
  202. if(do_trans_A == false)
  203. {
  204. Mat<eT> AA;
  205. op_htrans::apply_mat_noalias(AA, A);
  206. herk_emul<true, use_alpha, use_beta>::apply(C, AA, alpha, beta);
  207. }
  208. else
  209. if(do_trans_A == true)
  210. {
  211. const uword A_n_rows = A.n_rows;
  212. const uword A_n_cols = A.n_cols;
  213. for(uword col_A=0; col_A < A_n_cols; ++col_A)
  214. {
  215. // col_A is interpreted as row_A when storing the results in matrix C
  216. const eT* A_coldata = A.colptr(col_A);
  217. for(uword k=col_A; k < A_n_cols ; ++k)
  218. {
  219. const eT acc = op_cdot::direct_cdot(A_n_rows, A_coldata, A.colptr(k));
  220. if( (use_alpha == false) && (use_beta == false) )
  221. {
  222. C.at(col_A, k) = acc;
  223. if(col_A != k) { C.at(k, col_A) = std::conj(acc); }
  224. }
  225. else
  226. if( (use_alpha == true) && (use_beta == false) )
  227. {
  228. const eT val = alpha*acc;
  229. C.at(col_A, k) = val;
  230. if(col_A != k) { C.at(k, col_A) = std::conj(val); }
  231. }
  232. else
  233. if( (use_alpha == false) && (use_beta == true) )
  234. {
  235. C.at(col_A, k) = acc + beta*C.at(col_A, k);
  236. if(col_A != k) { C.at(k, col_A) = std::conj(acc) + beta*C.at(k, col_A); }
  237. }
  238. else
  239. if( (use_alpha == true) && (use_beta == true) )
  240. {
  241. const eT val = alpha*acc;
  242. C.at(col_A, k) = val + beta*C.at(col_A, k);
  243. if(col_A != k) { C.at(k, col_A) = std::conj(val) + beta*C.at(k, col_A); }
  244. }
  245. }
  246. }
  247. }
  248. }
  249. };
  250. template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
  251. class herk
  252. {
  253. public:
  254. template<typename T, typename TA>
  255. inline
  256. static
  257. void
  258. apply_blas_type( Mat<std::complex<T> >& C, const TA& A, const T alpha = T(1), const T beta = T(0) )
  259. {
  260. arma_extra_debug_sigprint();
  261. const uword threshold = 16;
  262. if(A.is_vec())
  263. {
  264. // work around poor handling of vectors by herk() in ATLAS 3.8.4 and standard BLAS
  265. herk_vec<do_trans_A, use_alpha, use_beta>::apply(C,A,alpha,beta);
  266. return;
  267. }
  268. if( (A.n_elem <= threshold) )
  269. {
  270. herk_emul<do_trans_A, use_alpha, use_beta>::apply(C,A,alpha,beta);
  271. }
  272. else
  273. {
  274. #if defined(ARMA_USE_ATLAS)
  275. {
  276. if(use_beta == true)
  277. {
  278. typedef typename std::complex<T> eT;
  279. // use a temporary matrix, as we can't assume that matrix C is already symmetric
  280. Mat<eT> D(C.n_rows, C.n_cols);
  281. herk<do_trans_A, use_alpha, false>::apply_blas_type(D,A,alpha);
  282. // NOTE: assuming beta=1; this is okay for now, as currently glue_times only uses beta=1
  283. arrayops::inplace_plus(C.memptr(), D.memptr(), C.n_elem);
  284. return;
  285. }
  286. atlas::cblas_herk<T>
  287. (
  288. atlas::CblasColMajor,
  289. atlas::CblasUpper,
  290. (do_trans_A) ? CblasConjTrans : atlas::CblasNoTrans,
  291. C.n_cols,
  292. (do_trans_A) ? A.n_rows : A.n_cols,
  293. (use_alpha) ? alpha : T(1),
  294. A.mem,
  295. (do_trans_A) ? A.n_rows : C.n_cols,
  296. (use_beta) ? beta : T(0),
  297. C.memptr(),
  298. C.n_cols
  299. );
  300. herk_helper::inplace_conj_copy_upper_tri_to_lower_tri(C);
  301. }
  302. #elif defined(ARMA_USE_BLAS)
  303. {
  304. if(use_beta == true)
  305. {
  306. typedef typename std::complex<T> eT;
  307. // use a temporary matrix, as we can't assume that matrix C is already symmetric
  308. Mat<eT> D(C.n_rows, C.n_cols);
  309. herk<do_trans_A, use_alpha, false>::apply_blas_type(D,A,alpha);
  310. // NOTE: assuming beta=1; this is okay for now, as currently glue_times only uses beta=1
  311. arrayops::inplace_plus(C.memptr(), D.memptr(), C.n_elem);
  312. return;
  313. }
  314. arma_extra_debug_print("blas::herk()");
  315. const char uplo = 'U';
  316. const char trans_A = (do_trans_A) ? 'C' : 'N';
  317. const blas_int n = blas_int(C.n_cols);
  318. const blas_int k = (do_trans_A) ? blas_int(A.n_rows) : blas_int(A.n_cols);
  319. const T local_alpha = (use_alpha) ? alpha : T(1);
  320. const T local_beta = (use_beta) ? beta : T(0);
  321. const blas_int lda = (do_trans_A) ? k : n;
  322. arma_extra_debug_print( arma_str::format("blas::herk(): trans_A = %c") % trans_A );
  323. blas::herk<T>
  324. (
  325. &uplo,
  326. &trans_A,
  327. &n,
  328. &k,
  329. &local_alpha,
  330. A.mem,
  331. &lda,
  332. &local_beta,
  333. C.memptr(),
  334. &n // &ldc
  335. );
  336. herk_helper::inplace_conj_copy_upper_tri_to_lower_tri(C);
  337. }
  338. #else
  339. {
  340. herk_emul<do_trans_A, use_alpha, use_beta>::apply(C,A,alpha,beta);
  341. }
  342. #endif
  343. }
  344. }
  345. template<typename eT, typename TA>
  346. inline
  347. static
  348. void
  349. apply( Mat<eT>& C, const TA& A, const eT alpha = eT(1), const eT beta = eT(0), const typename arma_not_cx<eT>::result* junk = 0 )
  350. {
  351. arma_ignore(C);
  352. arma_ignore(A);
  353. arma_ignore(alpha);
  354. arma_ignore(beta);
  355. arma_ignore(junk);
  356. // herk() cannot be used by non-complex matrices
  357. return;
  358. }
  359. template<typename TA>
  360. arma_inline
  361. static
  362. void
  363. apply
  364. (
  365. Mat< std::complex<float> >& C,
  366. const TA& A,
  367. const float alpha = float(1),
  368. const float beta = float(0)
  369. )
  370. {
  371. herk<do_trans_A, use_alpha, use_beta>::apply_blas_type(C,A,alpha,beta);
  372. }
  373. template<typename TA>
  374. arma_inline
  375. static
  376. void
  377. apply
  378. (
  379. Mat< std::complex<double> >& C,
  380. const TA& A,
  381. const double alpha = double(1),
  382. const double beta = double(0)
  383. )
  384. {
  385. herk<do_trans_A, use_alpha, use_beta>::apply_blas_type(C,A,alpha,beta);
  386. }
  387. };
  388. //! @}