You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

zgemv.c 8.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #include <stdio.h>
  39. #include "common.h"
  40. #ifdef FUNCTION_PROFILE
  41. #include "functable.h"
  42. #endif
  43. #ifdef XDOUBLE
  44. #define ERROR_NAME "XGEMV "
  45. #elif defined(DOUBLE)
  46. #define ERROR_NAME "ZGEMV "
  47. #else
  48. #define ERROR_NAME "CGEMV "
  49. #endif
  50. #ifdef SMP
  51. static int (*gemv_thread[])(BLASLONG, BLASLONG, FLOAT *, FLOAT *, BLASLONG, FLOAT * , BLASLONG, FLOAT *, BLASLONG, FLOAT *, int) = {
  52. #ifdef XDOUBLE
  53. xgemv_thread_n, xgemv_thread_t, xgemv_thread_r, xgemv_thread_c, xgemv_thread_o, xgemv_thread_u, xgemv_thread_s, xgemv_thread_d,
  54. #elif defined DOUBLE
  55. zgemv_thread_n, zgemv_thread_t, zgemv_thread_r, zgemv_thread_c, zgemv_thread_o, zgemv_thread_u, zgemv_thread_s, zgemv_thread_d,
  56. #else
  57. cgemv_thread_n, cgemv_thread_t, cgemv_thread_r, cgemv_thread_c, cgemv_thread_o, cgemv_thread_u, cgemv_thread_s, cgemv_thread_d,
  58. #endif
  59. };
  60. #endif
  61. #ifndef CBLAS
  62. void NAME(char *TRANS, blasint *M, blasint *N,
  63. FLOAT *ALPHA, FLOAT *a, blasint *LDA,
  64. FLOAT *x, blasint *INCX,
  65. FLOAT *BETA, FLOAT *y, blasint *INCY){
  66. char trans = *TRANS;
  67. blasint m = *M;
  68. blasint n = *N;
  69. blasint lda = *LDA;
  70. blasint incx = *INCX;
  71. blasint incy = *INCY;
  72. FLOAT *buffer;
  73. int buffer_size;
  74. #ifdef SMP
  75. int nthreads;
  76. #endif
  77. int (*gemv[])(BLASLONG, BLASLONG, BLASLONG, FLOAT, FLOAT, FLOAT *, BLASLONG,
  78. FLOAT * , BLASLONG, FLOAT *, BLASLONG, FLOAT *) = {
  79. GEMV_N, GEMV_T, GEMV_R, GEMV_C,
  80. GEMV_O, GEMV_U, GEMV_S, GEMV_D,
  81. };
  82. blasint info;
  83. blasint lenx, leny;
  84. blasint i;
  85. FLOAT alpha_r = *(ALPHA + 0);
  86. FLOAT alpha_i = *(ALPHA + 1);
  87. FLOAT beta_r = *(BETA + 0);
  88. FLOAT beta_i = *(BETA + 1);
  89. PRINT_DEBUG_NAME;
  90. TOUPPER(trans);
  91. info = 0;
  92. i = -1;
  93. if (trans == 'N') i = 0;
  94. if (trans == 'T') i = 1;
  95. if (trans == 'R') i = 2;
  96. if (trans == 'C') i = 3;
  97. if (trans == 'O') i = 4;
  98. if (trans == 'U') i = 5;
  99. if (trans == 'S') i = 6;
  100. if (trans == 'D') i = 7;
  101. if (incy == 0) info = 11;
  102. if (incx == 0) info = 8;
  103. if (lda < MAX(1,m)) info = 6;
  104. if (n < 0) info = 3;
  105. if (m < 0) info = 2;
  106. if (i < 0) info = 1;
  107. trans = i;
  108. if (info != 0) {
  109. BLASFUNC(xerbla)(ERROR_NAME, &info, sizeof(ERROR_NAME));
  110. return;
  111. }
  112. #else
  113. void CNAME(enum CBLAS_ORDER order,
  114. enum CBLAS_TRANSPOSE TransA,
  115. blasint m, blasint n,
  116. void *VALPHA,
  117. void *va, blasint lda,
  118. void *vx, blasint incx,
  119. void *VBETA,
  120. void *vy, blasint incy){
  121. FLOAT *ALPHA = (FLOAT*) VALPHA;
  122. FLOAT *a = (FLOAT*) va;
  123. FLOAT *x = (FLOAT*) vx;
  124. FLOAT *BETA = (FLOAT*) VBETA;
  125. FLOAT *y = (FLOAT*) vy;
  126. FLOAT *buffer;
  127. blasint lenx, leny;
  128. int trans, buffer_size;
  129. blasint info, t;
  130. #ifdef SMP
  131. int nthreads;
  132. #endif
  133. int (*gemv[])(BLASLONG, BLASLONG, BLASLONG, FLOAT, FLOAT, FLOAT *, BLASLONG,
  134. FLOAT * , BLASLONG, FLOAT *, BLASLONG, FLOAT *) = {
  135. GEMV_N, GEMV_T, GEMV_R, GEMV_C,
  136. GEMV_O, GEMV_U, GEMV_S, GEMV_D,
  137. };
  138. FLOAT alpha_r = *(ALPHA + 0);
  139. FLOAT alpha_i = *(ALPHA + 1);
  140. FLOAT beta_r = *(BETA + 0);
  141. FLOAT beta_i = *(BETA + 1);
  142. PRINT_DEBUG_CNAME;
  143. trans = -1;
  144. info = 0;
  145. if (order == CblasColMajor) {
  146. if (TransA == CblasNoTrans) trans = 0;
  147. if (TransA == CblasTrans) trans = 1;
  148. if (TransA == CblasConjNoTrans) trans = 2;
  149. if (TransA == CblasConjTrans) trans = 3;
  150. info = -1;
  151. if (incy == 0) info = 11;
  152. if (incx == 0) info = 8;
  153. if (lda < MAX(1, m)) info = 6;
  154. if (n < 0) info = 3;
  155. if (m < 0) info = 2;
  156. if (trans < 0) info = 1;
  157. }
  158. if (order == CblasRowMajor) {
  159. if (TransA == CblasNoTrans) trans = 1;
  160. if (TransA == CblasTrans) trans = 0;
  161. if (TransA == CblasConjNoTrans) trans = 3;
  162. if (TransA == CblasConjTrans) trans = 2;
  163. info = -1;
  164. t = n;
  165. n = m;
  166. m = t;
  167. if (incy == 0) info = 11;
  168. if (incx == 0) info = 8;
  169. if (lda < MAX(1, m)) info = 6;
  170. if (n < 0) info = 3;
  171. if (m < 0) info = 2;
  172. if (trans < 0) info = 1;
  173. }
  174. if (info >= 0) {
  175. BLASFUNC(xerbla)(ERROR_NAME, &info, sizeof(ERROR_NAME));
  176. return;
  177. }
  178. #endif
  179. /* Quick return if possible. */
  180. if (m == 0 || n == 0) return;
  181. lenx = n;
  182. leny = m;
  183. if (trans & 1) lenx = m;
  184. if (trans & 1) leny = n;
  185. if (beta_r != ONE || beta_i != ZERO) SCAL_K(leny, 0, 0, beta_r, beta_i, y, blasabs(incy), NULL, 0, NULL, 0);
  186. if (alpha_r == ZERO && alpha_i == ZERO) return;
  187. IDEBUG_START;
  188. FUNCTION_PROFILE_START();
  189. if (incx < 0) x -= (lenx - 1) * incx * 2;
  190. if (incy < 0) y -= (leny - 1) * incy * 2;
  191. buffer_size = 2 * (m + n) + 128 / sizeof(FLOAT);
  192. #ifdef WINDOWS_ABI
  193. buffer_size += 160 / sizeof(FLOAT) ;
  194. #endif
  195. // for alignment
  196. buffer_size = (buffer_size + 3) & ~3;
  197. STACK_ALLOC(buffer_size, FLOAT, buffer);
  198. #if defined(ARCH_X86_64) && defined(MAX_STACK_ALLOC) && MAX_STACK_ALLOC > 0
  199. // cgemv_t.S return NaN if there are NaN or Inf in the buffer (see bug #746)
  200. if(trans && stack_alloc_size)
  201. memset(buffer, 0, MIN(BUFFER_SIZE, sizeof(FLOAT) * buffer_size));
  202. #endif
  203. #ifdef SMP
  204. #if defined(_WIN64) && defined(_M_ARM64)
  205. if (m*n > 25000000L)
  206. nthreads = num_cpu_avail(4);
  207. else
  208. nthreads = 1;
  209. #else
  210. if (1L * m * n < 1024L * GEMM_MULTITHREAD_THRESHOLD)
  211. nthreads = 1;
  212. else
  213. nthreads = num_cpu_avail(2);
  214. #endif
  215. if (nthreads == 1) {
  216. #endif
  217. (gemv[(int)trans])(m, n, 0, alpha_r, alpha_i, a, lda, x, incx, y, incy, buffer);
  218. #ifdef SMP
  219. } else {
  220. (gemv_thread[(int)trans])(m, n, ALPHA, a, lda, x, incx, y, incy, buffer, nthreads);
  221. }
  222. #endif
  223. STACK_FREE(buffer);
  224. FUNCTION_PROFILE_END(4, m * n + m + n, 2 * m * n);
  225. IDEBUG_END;
  226. return;
  227. }