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 8.2 kB

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