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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. #ifndef CBLAS
  63. void NAME(char *TRANS, blasint *M, blasint *N,
  64. FLOAT *ALPHA, FLOAT *a, blasint *LDA,
  65. FLOAT *x, blasint *INCX,
  66. FLOAT *BETA, FLOAT *y, blasint *INCY){
  67. char trans = *TRANS;
  68. blasint m = *M;
  69. blasint n = *N;
  70. blasint lda = *LDA;
  71. blasint incx = *INCX;
  72. blasint incy = *INCY;
  73. FLOAT alpha = *ALPHA;
  74. FLOAT beta = *BETA;
  75. FLOAT *buffer;
  76. #ifdef SMP
  77. int nthreads;
  78. int nthreads_max;
  79. int nthreads_avail;
  80. double MNK;
  81. #endif
  82. int (*gemv[])(BLASLONG, BLASLONG, BLASLONG, FLOAT, FLOAT *, BLASLONG, FLOAT * , BLASLONG, FLOAT *, BLASLONG, FLOAT *) = {
  83. GEMV_N, GEMV_T,
  84. };
  85. blasint info;
  86. blasint lenx, leny;
  87. blasint i;
  88. PRINT_DEBUG_NAME;
  89. TOUPPER(trans);
  90. info = 0;
  91. i = -1;
  92. if (trans == 'N') i = 0;
  93. if (trans == 'T') i = 1;
  94. if (trans == 'R') i = 0;
  95. if (trans == 'C') i = 1;
  96. if (incy == 0) info = 11;
  97. if (incx == 0) info = 8;
  98. if (lda < MAX(1, m)) info = 6;
  99. if (n < 0) info = 3;
  100. if (m < 0) info = 2;
  101. if (i < 0) info = 1;
  102. trans = i;
  103. if (info != 0){
  104. BLASFUNC(xerbla)(ERROR_NAME, &info, sizeof(ERROR_NAME));
  105. return;
  106. }
  107. #else
  108. void CNAME(enum CBLAS_ORDER order,
  109. enum CBLAS_TRANSPOSE TransA,
  110. blasint m, blasint n,
  111. FLOAT alpha,
  112. FLOAT *a, blasint lda,
  113. FLOAT *x, blasint incx,
  114. FLOAT beta,
  115. FLOAT *y, blasint incy){
  116. FLOAT *buffer;
  117. blasint lenx, leny;
  118. int trans;
  119. blasint info, t;
  120. #ifdef SMP
  121. int nthreads;
  122. int nthreads_max;
  123. int nthreads_avail;
  124. double MNK;
  125. #endif
  126. int (*gemv[])(BLASLONG, BLASLONG, BLASLONG, FLOAT, FLOAT *, BLASLONG, FLOAT * , BLASLONG, FLOAT *, BLASLONG, FLOAT *) = {
  127. GEMV_N, GEMV_T,
  128. };
  129. PRINT_DEBUG_CNAME;
  130. trans = -1;
  131. info = 0;
  132. if (order == CblasColMajor) {
  133. if (TransA == CblasNoTrans) trans = 0;
  134. if (TransA == CblasTrans) trans = 1;
  135. if (TransA == CblasConjNoTrans) trans = 0;
  136. if (TransA == CblasConjTrans) trans = 1;
  137. info = -1;
  138. if (incy == 0) info = 11;
  139. if (incx == 0) info = 8;
  140. if (lda < MAX(1, m)) info = 6;
  141. if (n < 0) info = 3;
  142. if (m < 0) info = 2;
  143. if (trans < 0) info = 1;
  144. }
  145. if (order == CblasRowMajor) {
  146. if (TransA == CblasNoTrans) trans = 1;
  147. if (TransA == CblasTrans) trans = 0;
  148. if (TransA == CblasConjNoTrans) trans = 1;
  149. if (TransA == CblasConjTrans) trans = 0;
  150. info = -1;
  151. t = n;
  152. n = m;
  153. m = t;
  154. if (incy == 0) info = 11;
  155. if (incx == 0) info = 8;
  156. if (lda < MAX(1, m)) info = 6;
  157. if (n < 0) info = 3;
  158. if (m < 0) info = 2;
  159. if (trans < 0) info = 1;
  160. }
  161. if (info >= 0) {
  162. BLASFUNC(xerbla)(ERROR_NAME, &info, sizeof(ERROR_NAME));
  163. return;
  164. }
  165. #endif
  166. //printf("m=%d, n=%d, trans=%d, incx=%d, incy=%d, alpha=%f, beta=%f\n", m, n, trans, incx, incy, alpha, beta);
  167. if ((m==0) || (n==0)) return;
  168. lenx = n;
  169. leny = m;
  170. if (trans) lenx = m;
  171. if (trans) leny = n;
  172. if (beta != ONE) SCAL_K(leny, 0, 0, beta, y, abs(incy), NULL, 0, NULL, 0);
  173. if (alpha == ZERO) return;
  174. IDEBUG_START;
  175. FUNCTION_PROFILE_START();
  176. if (incx < 0) x -= (lenx - 1) * incx;
  177. if (incy < 0) y -= (leny - 1) * incy;
  178. #ifdef MAX_STACK_ALLOC
  179. // make it volatile because some gemv implementation (ex: dgemv_n.S)
  180. // do not restore all register
  181. volatile int stack_alloc_size = 0;
  182. //for gemv_n and gemv_t, try to allocate on stack
  183. stack_alloc_size = m + n;
  184. #ifdef ALIGNED_ACCESS
  185. stack_alloc_size += 3;
  186. #endif
  187. if(stack_alloc_size < 128)
  188. //dgemv_n.S require a 128 bytes buffer
  189. stack_alloc_size = 128;
  190. if(stack_alloc_size > MAX_STACK_ALLOC / sizeof(FLOAT))
  191. stack_alloc_size = 0;
  192. FLOAT stack_buffer[stack_alloc_size];
  193. buffer = stack_alloc_size ? stack_buffer : (FLOAT *)blas_memory_alloc(1);
  194. // printf("stack_alloc_size=%d\n", stack_alloc_size);
  195. #else
  196. //Original OpenBLAS/GotoBLAS codes.
  197. buffer = (FLOAT *)blas_memory_alloc(1);
  198. #endif
  199. #ifdef SMP
  200. nthreads_max = num_cpu_avail(2);
  201. nthreads_avail = nthreads_max;
  202. MNK = (double) m * (double) n;
  203. if ( MNK <= (24.0 * 24.0 * (double) (GEMM_MULTITHREAD_THRESHOLD*GEMM_MULTITHREAD_THRESHOLD) ) )
  204. nthreads_max = 1;
  205. if ( nthreads_max > nthreads_avail )
  206. nthreads = nthreads_avail;
  207. else
  208. nthreads = nthreads_max;
  209. if (nthreads == 1) {
  210. #endif
  211. (gemv[(int)trans])(m, n, 0, alpha, a, lda, x, incx, y, incy, buffer);
  212. #ifdef SMP
  213. } else {
  214. (gemv_thread[(int)trans])(m, n, alpha, a, lda, x, incx, y, incy, buffer, nthreads);
  215. }
  216. #endif
  217. #ifdef MAX_STACK_ALLOC
  218. if(!stack_alloc_size){
  219. blas_memory_free(buffer);
  220. }
  221. #else
  222. blas_memory_free(buffer);
  223. #endif
  224. FUNCTION_PROFILE_END(1, m * n + m + n, 2 * m * n);
  225. IDEBUG_END;
  226. return;
  227. }