mul_gemv.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 gemv
  16. //! @{
  17. //! for tiny square matrices, size <= 4x4
  18. template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
  19. class gemv_emul_tinysq
  20. {
  21. public:
  22. template<const uword row, const uword col>
  23. struct pos
  24. {
  25. static const uword n2 = (do_trans_A == false) ? (row + col*2) : (col + row*2);
  26. static const uword n3 = (do_trans_A == false) ? (row + col*3) : (col + row*3);
  27. static const uword n4 = (do_trans_A == false) ? (row + col*4) : (col + row*4);
  28. };
  29. template<typename eT, const uword i>
  30. arma_inline
  31. static
  32. void
  33. assign(eT* y, const eT acc, const eT alpha, const eT beta)
  34. {
  35. if(use_beta == false)
  36. {
  37. y[i] = (use_alpha == false) ? acc : alpha*acc;
  38. }
  39. else
  40. {
  41. const eT tmp = y[i];
  42. y[i] = beta*tmp + ( (use_alpha == false) ? acc : alpha*acc );
  43. }
  44. }
  45. template<typename eT, typename TA>
  46. arma_cold
  47. inline
  48. static
  49. void
  50. apply( eT* y, const TA& A, const eT* x, const eT alpha = eT(1), const eT beta = eT(0) )
  51. {
  52. arma_extra_debug_sigprint();
  53. const eT* Am = A.memptr();
  54. switch(A.n_rows)
  55. {
  56. case 1:
  57. {
  58. const eT acc = Am[0] * x[0];
  59. assign<eT, 0>(y, acc, alpha, beta);
  60. }
  61. break;
  62. case 2:
  63. {
  64. const eT x0 = x[0];
  65. const eT x1 = x[1];
  66. const eT acc0 = Am[pos<0,0>::n2]*x0 + Am[pos<0,1>::n2]*x1;
  67. const eT acc1 = Am[pos<1,0>::n2]*x0 + Am[pos<1,1>::n2]*x1;
  68. assign<eT, 0>(y, acc0, alpha, beta);
  69. assign<eT, 1>(y, acc1, alpha, beta);
  70. }
  71. break;
  72. case 3:
  73. {
  74. const eT x0 = x[0];
  75. const eT x1 = x[1];
  76. const eT x2 = x[2];
  77. const eT acc0 = Am[pos<0,0>::n3]*x0 + Am[pos<0,1>::n3]*x1 + Am[pos<0,2>::n3]*x2;
  78. const eT acc1 = Am[pos<1,0>::n3]*x0 + Am[pos<1,1>::n3]*x1 + Am[pos<1,2>::n3]*x2;
  79. const eT acc2 = Am[pos<2,0>::n3]*x0 + Am[pos<2,1>::n3]*x1 + Am[pos<2,2>::n3]*x2;
  80. assign<eT, 0>(y, acc0, alpha, beta);
  81. assign<eT, 1>(y, acc1, alpha, beta);
  82. assign<eT, 2>(y, acc2, alpha, beta);
  83. }
  84. break;
  85. case 4:
  86. {
  87. const eT x0 = x[0];
  88. const eT x1 = x[1];
  89. const eT x2 = x[2];
  90. const eT x3 = x[3];
  91. 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;
  92. 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;
  93. 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;
  94. 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;
  95. assign<eT, 0>(y, acc0, alpha, beta);
  96. assign<eT, 1>(y, acc1, alpha, beta);
  97. assign<eT, 2>(y, acc2, alpha, beta);
  98. assign<eT, 3>(y, acc3, alpha, beta);
  99. }
  100. break;
  101. default:
  102. ;
  103. }
  104. }
  105. };
  106. class gemv_emul_helper
  107. {
  108. public:
  109. template<typename eT, typename TA>
  110. arma_hot
  111. inline
  112. static
  113. typename arma_not_cx<eT>::result
  114. dot_row_col( const TA& A, const eT* x, const uword row, const uword N )
  115. {
  116. eT acc1 = eT(0);
  117. eT acc2 = eT(0);
  118. uword i,j;
  119. for(i=0, j=1; j < N; i+=2, j+=2)
  120. {
  121. const eT xi = x[i];
  122. const eT xj = x[j];
  123. acc1 += A.at(row,i) * xi;
  124. acc2 += A.at(row,j) * xj;
  125. }
  126. if(i < N)
  127. {
  128. acc1 += A.at(row,i) * x[i];
  129. }
  130. return (acc1 + acc2);
  131. }
  132. template<typename eT, typename TA>
  133. arma_hot
  134. inline
  135. static
  136. typename arma_cx_only<eT>::result
  137. dot_row_col( const TA& A, const eT* x, const uword row, const uword N )
  138. {
  139. typedef typename get_pod_type<eT>::result T;
  140. T val_real = T(0);
  141. T val_imag = T(0);
  142. for(uword i=0; i<N; ++i)
  143. {
  144. const std::complex<T>& Ai = A.at(row,i);
  145. const std::complex<T>& xi = x[i];
  146. const T a = Ai.real();
  147. const T b = Ai.imag();
  148. const T c = xi.real();
  149. const T d = xi.imag();
  150. val_real += (a*c) - (b*d);
  151. val_imag += (a*d) + (b*c);
  152. }
  153. return std::complex<T>(val_real, val_imag);
  154. }
  155. };
  156. //! \brief
  157. //! Partial emulation of ATLAS/BLAS gemv().
  158. //! 'y' is assumed to have been set to the correct size (i.e. taking into account the transpose)
  159. template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
  160. class gemv_emul
  161. {
  162. public:
  163. template<typename eT, typename TA>
  164. arma_hot
  165. inline
  166. static
  167. void
  168. apply( eT* y, const TA& A, const eT* x, const eT alpha = eT(1), const eT beta = eT(0) )
  169. {
  170. arma_extra_debug_sigprint();
  171. const uword A_n_rows = A.n_rows;
  172. const uword A_n_cols = A.n_cols;
  173. if(do_trans_A == false)
  174. {
  175. if(A_n_rows == 1)
  176. {
  177. const eT acc = op_dot::direct_dot_arma(A_n_cols, A.memptr(), x);
  178. if( (use_alpha == false) && (use_beta == false) ) { y[0] = acc; }
  179. else if( (use_alpha == true ) && (use_beta == false) ) { y[0] = alpha*acc; }
  180. else if( (use_alpha == false) && (use_beta == true ) ) { y[0] = acc + beta*y[0]; }
  181. else if( (use_alpha == true ) && (use_beta == true ) ) { y[0] = alpha*acc + beta*y[0]; }
  182. }
  183. else
  184. for(uword row=0; row < A_n_rows; ++row)
  185. {
  186. const eT acc = gemv_emul_helper::dot_row_col(A, x, row, A_n_cols);
  187. if( (use_alpha == false) && (use_beta == false) ) { y[row] = acc; }
  188. else if( (use_alpha == true ) && (use_beta == false) ) { y[row] = alpha*acc; }
  189. else if( (use_alpha == false) && (use_beta == true ) ) { y[row] = acc + beta*y[row]; }
  190. else if( (use_alpha == true ) && (use_beta == true ) ) { y[row] = alpha*acc + beta*y[row]; }
  191. }
  192. }
  193. else
  194. if(do_trans_A == true)
  195. {
  196. if(is_cx<eT>::no)
  197. {
  198. for(uword col=0; col < A_n_cols; ++col)
  199. {
  200. // col is interpreted as row when storing the results in 'y'
  201. // const eT* A_coldata = A.colptr(col);
  202. //
  203. // eT acc = eT(0);
  204. // for(uword row=0; row < A_n_rows; ++row)
  205. // {
  206. // acc += A_coldata[row] * x[row];
  207. // }
  208. const eT acc = op_dot::direct_dot_arma(A_n_rows, A.colptr(col), x);
  209. if( (use_alpha == false) && (use_beta == false) ) { y[col] = acc; }
  210. else if( (use_alpha == true ) && (use_beta == false) ) { y[col] = alpha*acc; }
  211. else if( (use_alpha == false) && (use_beta == true ) ) { y[col] = acc + beta*y[col]; }
  212. else if( (use_alpha == true ) && (use_beta == true ) ) { y[col] = alpha*acc + beta*y[col]; }
  213. }
  214. }
  215. else
  216. {
  217. Mat<eT> AA;
  218. op_htrans::apply_mat_noalias(AA, A);
  219. gemv_emul<false, use_alpha, use_beta>::apply(y, AA, x, alpha, beta);
  220. }
  221. }
  222. }
  223. };
  224. //! \brief
  225. //! Wrapper for ATLAS/BLAS gemv function, using template arguments to control the arguments passed to gemv.
  226. //! 'y' is assumed to have been set to the correct size (i.e. taking into account the transpose)
  227. template<const bool do_trans_A=false, const bool use_alpha=false, const bool use_beta=false>
  228. class gemv
  229. {
  230. public:
  231. template<typename eT, typename TA>
  232. inline
  233. static
  234. void
  235. apply_blas_type( eT* y, const TA& A, const eT* x, const eT alpha = eT(1), const eT beta = eT(0) )
  236. {
  237. arma_extra_debug_sigprint();
  238. if( (A.n_rows <= 4) && (A.n_rows == A.n_cols) && (is_cx<eT>::no) )
  239. {
  240. gemv_emul_tinysq<do_trans_A, use_alpha, use_beta>::apply(y, A, x, alpha, beta);
  241. }
  242. else
  243. {
  244. #if defined(ARMA_USE_ATLAS)
  245. {
  246. arma_debug_assert_atlas_size(A);
  247. if(is_cx<eT>::no)
  248. {
  249. // use gemm() instead of gemv() to work around a speed issue in Atlas 3.8.4
  250. arma_extra_debug_print("atlas::cblas_gemm()");
  251. atlas::cblas_gemm<eT>
  252. (
  253. atlas::CblasColMajor,
  254. (do_trans_A) ? ( is_cx<eT>::yes ? CblasConjTrans : atlas::CblasTrans ) : atlas::CblasNoTrans,
  255. atlas::CblasNoTrans,
  256. (do_trans_A) ? A.n_cols : A.n_rows,
  257. 1,
  258. (do_trans_A) ? A.n_rows : A.n_cols,
  259. (use_alpha) ? alpha : eT(1),
  260. A.mem,
  261. A.n_rows,
  262. x,
  263. (do_trans_A) ? A.n_rows : A.n_cols,
  264. (use_beta) ? beta : eT(0),
  265. y,
  266. (do_trans_A) ? A.n_cols : A.n_rows
  267. );
  268. }
  269. else
  270. {
  271. arma_extra_debug_print("atlas::cblas_gemv()");
  272. atlas::cblas_gemv<eT>
  273. (
  274. atlas::CblasColMajor,
  275. (do_trans_A) ? ( is_cx<eT>::yes ? CblasConjTrans : atlas::CblasTrans ) : atlas::CblasNoTrans,
  276. A.n_rows,
  277. A.n_cols,
  278. (use_alpha) ? alpha : eT(1),
  279. A.mem,
  280. A.n_rows,
  281. x,
  282. 1,
  283. (use_beta) ? beta : eT(0),
  284. y,
  285. 1
  286. );
  287. }
  288. }
  289. #elif defined(ARMA_USE_BLAS)
  290. {
  291. arma_extra_debug_print("blas::gemv()");
  292. arma_debug_assert_blas_size(A);
  293. const char trans_A = (do_trans_A) ? ( is_cx<eT>::yes ? 'C' : 'T' ) : 'N';
  294. const blas_int m = blas_int(A.n_rows);
  295. const blas_int n = blas_int(A.n_cols);
  296. const eT local_alpha = (use_alpha) ? alpha : eT(1);
  297. //const blas_int lda = A.n_rows;
  298. const blas_int inc = blas_int(1);
  299. const eT local_beta = (use_beta) ? beta : eT(0);
  300. arma_extra_debug_print( arma_str::format("blas::gemv(): trans_A = %c") % trans_A );
  301. blas::gemv<eT>
  302. (
  303. &trans_A,
  304. &m,
  305. &n,
  306. &local_alpha,
  307. A.mem,
  308. &m, // lda
  309. x,
  310. &inc,
  311. &local_beta,
  312. y,
  313. &inc
  314. );
  315. }
  316. #else
  317. {
  318. gemv_emul<do_trans_A, use_alpha, use_beta>::apply(y,A,x,alpha,beta);
  319. }
  320. #endif
  321. }
  322. }
  323. template<typename eT, typename TA>
  324. arma_inline
  325. static
  326. void
  327. apply( eT* y, const TA& A, const eT* x, const eT alpha = eT(1), const eT beta = eT(0) )
  328. {
  329. gemv_emul<do_trans_A, use_alpha, use_beta>::apply(y,A,x,alpha,beta);
  330. }
  331. template<typename TA>
  332. arma_inline
  333. static
  334. void
  335. apply
  336. (
  337. float* y,
  338. const TA& A,
  339. const float* x,
  340. const float alpha = float(1),
  341. const float beta = float(0)
  342. )
  343. {
  344. gemv<do_trans_A, use_alpha, use_beta>::apply_blas_type(y,A,x,alpha,beta);
  345. }
  346. template<typename TA>
  347. arma_inline
  348. static
  349. void
  350. apply
  351. (
  352. double* y,
  353. const TA& A,
  354. const double* x,
  355. const double alpha = double(1),
  356. const double beta = double(0)
  357. )
  358. {
  359. gemv<do_trans_A, use_alpha, use_beta>::apply_blas_type(y,A,x,alpha,beta);
  360. }
  361. template<typename TA>
  362. arma_inline
  363. static
  364. void
  365. apply
  366. (
  367. std::complex<float>* y,
  368. const TA& A,
  369. const std::complex<float>* x,
  370. const std::complex<float> alpha = std::complex<float>(1),
  371. const std::complex<float> beta = std::complex<float>(0)
  372. )
  373. {
  374. gemv<do_trans_A, use_alpha, use_beta>::apply_blas_type(y,A,x,alpha,beta);
  375. }
  376. template<typename TA>
  377. arma_inline
  378. static
  379. void
  380. apply
  381. (
  382. std::complex<double>* y,
  383. const TA& A,
  384. const std::complex<double>* x,
  385. const std::complex<double> alpha = std::complex<double>(1),
  386. const std::complex<double> beta = std::complex<double>(0)
  387. )
  388. {
  389. gemv<do_trans_A, use_alpha, use_beta>::apply_blas_type(y,A,x,alpha,beta);
  390. }
  391. };
  392. //! @}