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.

gemv.c 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "l1param.h"
  41. #ifdef FUNCTION_PROFILE
  42. #include "functable.h"
  43. #endif
  44. #ifdef XDOUBLE
  45. #define ERROR_NAME "QGEMV "
  46. #elif defined(DOUBLE)
  47. #define ERROR_NAME "DGEMV "
  48. #else
  49. #define ERROR_NAME "SGEMV "
  50. #endif
  51. #ifdef SMP
  52. static int (*gemv_thread[])(BLASLONG, BLASLONG, FLOAT, FLOAT *, BLASLONG, FLOAT * , BLASLONG, FLOAT *, BLASLONG, FLOAT *, int) = {
  53. #ifdef XDOUBLE
  54. qgemv_thread_n, qgemv_thread_t,
  55. #elif defined DOUBLE
  56. dgemv_thread_n, dgemv_thread_t,
  57. #else
  58. sgemv_thread_n, sgemv_thread_t,
  59. #endif
  60. };
  61. #endif
  62. #ifdef SMP
  63. #ifdef DYNAMIC_ARCH
  64. extern char* gotoblas_corename(void);
  65. #endif
  66. #if defined(DYNAMIC_ARCH) || defined(NEOVERSEV1)
  67. static inline int get_gemv_optimal_nthreads_neoversev1(BLASLONG MN, int ncpu) {
  68. return
  69. MN < 25600L ? 1
  70. : MN < 63001L ? MIN(ncpu, 4)
  71. : MN < 459684L ? MIN(ncpu, 16)
  72. : ncpu;
  73. }
  74. #endif
  75. #if defined(DYNAMIC_ARCH) || defined(NEOVERSEV2)
  76. static inline int get_gemv_optimal_nthreads_neoversev2(BLASLONG MN, int ncpu) {
  77. return
  78. MN < 24964L ? 1
  79. : MN < 65536L ? MIN(ncpu, 8)
  80. : MN < 262144L ? MIN(ncpu, 32)
  81. : MN < 1638400L ? MIN(ncpu, 64)
  82. : ncpu;
  83. }
  84. #endif
  85. //thread throttling for dgemv
  86. #if defined(DYNAMIC_ARCH) || defined(NEOVERSEV1)
  87. static inline int get_dgemv_optimal_nthreads_neoversev1(BLASLONG MN, int ncpu) {
  88. return
  89. MN < 8100L ? 1
  90. : MN < 12100L ? MIN(ncpu, 2)
  91. : MN < 36100L ? MIN(ncpu, 4)
  92. : MN < 84100L ? MIN(ncpu, 8)
  93. : MN < 348100L ? MIN(ncpu, 16)
  94. : MN < 435600L ? MIN(ncpu, 24)
  95. : MN < 810000L ? MIN(ncpu, 32)
  96. : MN < 1050625 ? MIN(ncpu, 40)
  97. : ncpu;
  98. }
  99. #endif
  100. static inline int get_gemv_optimal_nthreads(BLASLONG MN) {
  101. int ncpu = num_cpu_avail(3);
  102. #if defined(_WIN64) && defined(_M_ARM64)
  103. if (MN > 100000000L)
  104. return num_cpu_avail(4);
  105. return 1;
  106. #endif
  107. #if defined(NEOVERSEV1) && !defined(COMPLEX) && !defined(DOUBLE) && !defined(BFLOAT16)
  108. return get_gemv_optimal_nthreads_neoversev1(MN, ncpu);
  109. #elif defined(NEOVERSEV1) && !defined(COMPLEX) && defined(DOUBLE) && !defined(BFLOAT16)
  110. return get_dgemv_optimal_nthreads_neoversev1(MN, ncpu);
  111. #elif defined(NEOVERSEV2) && !defined(COMPLEX) && !defined(DOUBLE) && !defined(BFLOAT16)
  112. return get_gemv_optimal_nthreads_neoversev2(MN, ncpu);
  113. #elif defined(DYNAMIC_ARCH) && !defined(COMPLEX) && !defined(DOUBLE) && !defined(BFLOAT16)
  114. if (strcmp(gotoblas_corename(), "neoversev1") == 0) {
  115. return get_gemv_optimal_nthreads_neoversev1(MN, ncpu);
  116. }
  117. if (strcmp(gotoblas_corename(), "neoversev2") == 0) {
  118. return get_gemv_optimal_nthreads_neoversev2(MN, ncpu);
  119. }
  120. #elif defined(DYNAMIC_ARCH) && !defined(COMPLEX) && defined(DOUBLE) && !defined(BFLOAT16)
  121. if (strcmp(gotoblas_corename(), "neoversev1") == 0) {
  122. return get_dgemv_optimal_nthreads_neoversev1(MN, ncpu);
  123. }
  124. #endif
  125. if ( MN < 115200L * GEMM_MULTITHREAD_THRESHOLD )
  126. return 1;
  127. else
  128. return num_cpu_avail(2);
  129. }
  130. #endif
  131. #ifndef CBLAS
  132. void NAME(char *TRANS, blasint *M, blasint *N,
  133. FLOAT *ALPHA, FLOAT *a, blasint *LDA,
  134. FLOAT *x, blasint *INCX,
  135. FLOAT *BETA, FLOAT *y, blasint *INCY){
  136. char trans = *TRANS;
  137. blasint m = *M;
  138. blasint n = *N;
  139. blasint lda = *LDA;
  140. blasint incx = *INCX;
  141. blasint incy = *INCY;
  142. FLOAT alpha = *ALPHA;
  143. FLOAT beta = *BETA;
  144. FLOAT *buffer;
  145. int buffer_size;
  146. #ifdef SMP
  147. int nthreads;
  148. #endif
  149. int (*gemv[])(BLASLONG, BLASLONG, BLASLONG, FLOAT, FLOAT *, BLASLONG, FLOAT * , BLASLONG, FLOAT *, BLASLONG, FLOAT *) = {
  150. GEMV_N, GEMV_T,
  151. };
  152. blasint info;
  153. blasint lenx, leny;
  154. blasint i;
  155. PRINT_DEBUG_NAME;
  156. TOUPPER(trans);
  157. info = 0;
  158. i = -1;
  159. if (trans == 'N') i = 0;
  160. if (trans == 'T') i = 1;
  161. if (trans == 'R') i = 0;
  162. if (trans == 'C') i = 1;
  163. if (incy == 0) info = 11;
  164. if (incx == 0) info = 8;
  165. if (lda < MAX(1, m)) info = 6;
  166. if (n < 0) info = 3;
  167. if (m < 0) info = 2;
  168. if (i < 0) info = 1;
  169. trans = i;
  170. if (info != 0){
  171. BLASFUNC(xerbla)(ERROR_NAME, &info, sizeof(ERROR_NAME));
  172. return;
  173. }
  174. #else
  175. void CNAME(enum CBLAS_ORDER order,
  176. enum CBLAS_TRANSPOSE TransA,
  177. blasint m, blasint n,
  178. FLOAT alpha,
  179. FLOAT *a, blasint lda,
  180. FLOAT *x, blasint incx,
  181. FLOAT beta,
  182. FLOAT *y, blasint incy){
  183. FLOAT *buffer;
  184. blasint lenx, leny;
  185. int trans, buffer_size;
  186. blasint info, t;
  187. #ifdef SMP
  188. int nthreads;
  189. #endif
  190. int (*gemv[])(BLASLONG, BLASLONG, BLASLONG, FLOAT, FLOAT *, BLASLONG, FLOAT * , BLASLONG, FLOAT *, BLASLONG, FLOAT *) = {
  191. GEMV_N, GEMV_T,
  192. };
  193. PRINT_DEBUG_CNAME;
  194. trans = -1;
  195. info = 0;
  196. if (order == CblasColMajor) {
  197. if (TransA == CblasNoTrans) trans = 0;
  198. if (TransA == CblasTrans) trans = 1;
  199. if (TransA == CblasConjNoTrans) trans = 0;
  200. if (TransA == CblasConjTrans) trans = 1;
  201. info = -1;
  202. if (incy == 0) info = 11;
  203. if (incx == 0) info = 8;
  204. if (lda < MAX(1, m)) info = 6;
  205. if (n < 0) info = 3;
  206. if (m < 0) info = 2;
  207. if (trans < 0) info = 1;
  208. }
  209. if (order == CblasRowMajor) {
  210. if (TransA == CblasNoTrans) trans = 1;
  211. if (TransA == CblasTrans) trans = 0;
  212. if (TransA == CblasConjNoTrans) trans = 1;
  213. if (TransA == CblasConjTrans) trans = 0;
  214. info = -1;
  215. t = n;
  216. n = m;
  217. m = t;
  218. if (incy == 0) info = 11;
  219. if (incx == 0) info = 8;
  220. if (lda < MAX(1, m)) info = 6;
  221. if (n < 0) info = 3;
  222. if (m < 0) info = 2;
  223. if (trans < 0) info = 1;
  224. }
  225. if (info >= 0) {
  226. BLASFUNC(xerbla)(ERROR_NAME, &info, sizeof(ERROR_NAME));
  227. return;
  228. }
  229. #endif
  230. if ((m==0) || (n==0)) return;
  231. lenx = n;
  232. leny = m;
  233. if (trans) lenx = m;
  234. if (trans) leny = n;
  235. if (beta != ONE) SCAL_K(leny, 0, 0, beta, y, blasabs(incy), NULL, 0, NULL, 0);
  236. if (alpha == ZERO) return;
  237. IDEBUG_START;
  238. FUNCTION_PROFILE_START();
  239. if (incx < 0) x -= (lenx - 1) * incx;
  240. if (incy < 0) y -= (leny - 1) * incy;
  241. buffer_size = m + n + 128 / sizeof(FLOAT);
  242. #ifdef WINDOWS_ABI
  243. buffer_size += 160 / sizeof(FLOAT) ;
  244. #endif
  245. // for alignment
  246. buffer_size = (buffer_size + 3) & ~3;
  247. STACK_ALLOC(buffer_size, FLOAT, buffer);
  248. #ifdef SMP
  249. nthreads = get_gemv_optimal_nthreads(1L * m * n);
  250. if (nthreads == 1) {
  251. #endif
  252. (gemv[(int)trans])(m, n, 0, alpha, a, lda, x, incx, y, incy, buffer);
  253. #ifdef SMP
  254. } else {
  255. (gemv_thread[(int)trans])(m, n, alpha, a, lda, x, incx, y, incy, buffer, nthreads);
  256. }
  257. #endif
  258. STACK_FREE(buffer);
  259. FUNCTION_PROFILE_END(1, m * n + m + n, 2 * m * n);
  260. IDEBUG_END;
  261. return;
  262. }