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_thread.c 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <stdlib.h>
  40. #include "common.h"
  41. #ifndef TRANSA
  42. #if !defined(CONJ) && !defined(XCONJ)
  43. #define GEMV GEMV_N
  44. #elif defined(CONJ) && !defined(XCONJ)
  45. #define GEMV GEMV_R
  46. #elif !defined(CONJ) && defined(XCONJ)
  47. #define GEMV GEMV_O
  48. #else
  49. #define GEMV GEMV_S
  50. #endif
  51. #else
  52. #if !defined(CONJ) && !defined(XCONJ)
  53. #define GEMV GEMV_T
  54. #elif defined(CONJ) && !defined(XCONJ)
  55. #define GEMV GEMV_C
  56. #elif !defined(CONJ) && defined(XCONJ)
  57. #define GEMV GEMV_U
  58. #else
  59. #define GEMV GEMV_D
  60. #endif
  61. #endif
  62. #ifndef TRANSA
  63. #define Y_DUMMY_NUM 1024
  64. static FLOAT y_dummy[Y_DUMMY_NUM];
  65. #endif
  66. static int gemv_kernel(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, FLOAT *dummy1, FLOAT *buffer, BLASLONG pos){
  67. FLOAT *a, *x, *y;
  68. BLASLONG lda, incx, incy;
  69. BLASLONG m_from, m_to, n_from, n_to;
  70. a = (FLOAT *)args -> a;
  71. x = (FLOAT *)args -> b;
  72. y = (FLOAT *)args -> c;
  73. lda = args -> lda;
  74. incx = args -> ldb;
  75. incy = args -> ldc;
  76. m_from = 0;
  77. m_to = args -> m;
  78. if (range_m) {
  79. m_from = *(range_m + 0);
  80. m_to = *(range_m + 1);
  81. a += m_from * COMPSIZE;
  82. #ifndef TRANSA
  83. y += m_from * incy * COMPSIZE;
  84. #endif
  85. }
  86. n_from = 0;
  87. n_to = args -> n;
  88. if (range_n) {
  89. n_from = *(range_n + 0);
  90. n_to = *(range_n + 1);
  91. a += n_from * lda * COMPSIZE;
  92. #ifdef TRANSA
  93. y += n_from * incy * COMPSIZE;
  94. #else
  95. //for split matrix row (n) direction and vector x of gemv_n
  96. x += n_from * incx * COMPSIZE;
  97. //store partial result for every thread
  98. y += (m_to - m_from) * 1 * COMPSIZE * pos;
  99. #endif
  100. }
  101. //fprintf(stderr, "M_From = %d M_To = %d N_From = %d N_To = %d POS=%d\n", m_from, m_to, n_from, n_to, pos);
  102. GEMV(m_to - m_from, n_to - n_from, 0,
  103. *((FLOAT *)args -> alpha + 0),
  104. #ifdef COMPLEX
  105. *((FLOAT *)args -> alpha + 1),
  106. #endif
  107. a, lda, x, incx, y, incy, buffer);
  108. return 0;
  109. }
  110. #ifndef COMPLEX
  111. int CNAME(BLASLONG m, BLASLONG n, FLOAT alpha, FLOAT *a, BLASLONG lda, FLOAT *x, BLASLONG incx, FLOAT *y, BLASLONG incy, FLOAT *buffer, int nthreads){
  112. #else
  113. int CNAME(BLASLONG m, BLASLONG n, FLOAT *alpha, FLOAT *a, BLASLONG lda, FLOAT *x, BLASLONG incx, FLOAT *y, BLASLONG incy, FLOAT *buffer, int nthreads){
  114. #endif
  115. blas_arg_t args;
  116. blas_queue_t queue[MAX_CPU_NUMBER];
  117. BLASLONG range[MAX_CPU_NUMBER + 1];
  118. BLASLONG width, i, num_cpu;
  119. #ifndef TRANSA
  120. int split_x=0;
  121. #endif
  122. #ifdef SMP
  123. #ifndef COMPLEX
  124. #ifdef XDOUBLE
  125. int mode = BLAS_XDOUBLE | BLAS_REAL;
  126. #elif defined(DOUBLE)
  127. int mode = BLAS_DOUBLE | BLAS_REAL;
  128. #else
  129. int mode = BLAS_SINGLE | BLAS_REAL;
  130. #endif
  131. #else
  132. #ifdef XDOUBLE
  133. int mode = BLAS_XDOUBLE | BLAS_COMPLEX;
  134. #elif defined(DOUBLE)
  135. int mode = BLAS_DOUBLE | BLAS_COMPLEX;
  136. #else
  137. int mode = BLAS_SINGLE | BLAS_COMPLEX;
  138. #endif
  139. #endif
  140. #endif
  141. args.m = m;
  142. args.n = n;
  143. args.a = (void *)a;
  144. args.b = (void *)x;
  145. args.c = (void *)y;
  146. args.lda = lda;
  147. args.ldb = incx;
  148. args.ldc = incy;
  149. #ifndef COMPLEX
  150. args.alpha = (void *)&alpha;
  151. #else
  152. args.alpha = (void *) alpha;
  153. #endif
  154. num_cpu = 0;
  155. range[0] = 0;
  156. #ifndef TRANSA
  157. i = m;
  158. #else
  159. i = n;
  160. #endif
  161. while (i > 0){
  162. width = blas_quickdivide(i + nthreads - num_cpu - 1, nthreads - num_cpu);
  163. if (width < 4) width = 4;
  164. if (i < width) width = i;
  165. range[num_cpu + 1] = range[num_cpu] + width;
  166. queue[num_cpu].mode = mode;
  167. queue[num_cpu].routine = gemv_kernel;
  168. queue[num_cpu].args = &args;
  169. #ifndef TRANSA
  170. queue[num_cpu].range_m = &range[num_cpu];
  171. queue[num_cpu].range_n = NULL;
  172. #else
  173. queue[num_cpu].range_m = NULL;
  174. queue[num_cpu].range_n = &range[num_cpu];
  175. #endif
  176. queue[num_cpu].sa = NULL;
  177. queue[num_cpu].sb = NULL;
  178. queue[num_cpu].next = &queue[num_cpu + 1];
  179. num_cpu ++;
  180. i -= width;
  181. }
  182. #ifndef TRANSA
  183. //try to split matrix on row direction and x.
  184. //Then, reduction.
  185. if (num_cpu < nthreads) {
  186. //too small to split or bigger than the y_dummy buffer.
  187. double MN = (double) m * (double) n;
  188. if ( MN <= (24.0 * 24.0 * (double) (GEMM_MULTITHREAD_THRESHOLD*GEMM_MULTITHREAD_THRESHOLD))
  189. || m*COMPSIZE*nthreads > Y_DUMMY_NUM)
  190. goto Outer;
  191. num_cpu = 0;
  192. range[0] = 0;
  193. memset(y_dummy, 0, sizeof(FLOAT) * m * COMPSIZE * nthreads);
  194. args.ldc = 1;
  195. args.c = (void *)y_dummy;
  196. //split on row (n) and x
  197. i=n;
  198. split_x=1;
  199. while (i > 0){
  200. width = blas_quickdivide(i + nthreads - num_cpu - 1, nthreads - num_cpu);
  201. if (width < 4) width = 4;
  202. if (i < width) width = i;
  203. range[num_cpu + 1] = range[num_cpu] + width;
  204. queue[num_cpu].mode = mode;
  205. queue[num_cpu].routine = gemv_kernel;
  206. queue[num_cpu].args = &args;
  207. queue[num_cpu].position = num_cpu;
  208. queue[num_cpu].range_m = NULL;
  209. queue[num_cpu].range_n = &range[num_cpu];
  210. queue[num_cpu].sa = NULL;
  211. queue[num_cpu].sb = NULL;
  212. queue[num_cpu].next = &queue[num_cpu + 1];
  213. num_cpu ++;
  214. i -= width;
  215. }
  216. }
  217. Outer:
  218. #endif
  219. if (num_cpu) {
  220. queue[0].sa = NULL;
  221. queue[0].sb = buffer;
  222. queue[num_cpu - 1].next = NULL;
  223. exec_blas(num_cpu, queue);
  224. }
  225. #ifndef TRANSA
  226. if(split_x==1){
  227. //reduction
  228. for(i=0; i<num_cpu; i++){
  229. int j;
  230. for(j=0; j<m; j++){
  231. y[j*incy*COMPSIZE] +=y_dummy[i*m*COMPSIZE + j*COMPSIZE];
  232. #ifdef COMPLEX
  233. y[j*incy*COMPSIZE+1] +=y_dummy[i*m*COMPSIZE + j*COMPSIZE+1];
  234. #endif
  235. }
  236. }
  237. }
  238. #endif
  239. return 0;
  240. }