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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 thread_local
  63. # if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
  64. # define thread_local _Thread_local
  65. # elif defined _WIN32 && ( \
  66. defined _MSC_VER || \
  67. defined __ICL || \
  68. defined __DMC__ || \
  69. defined __BORLANDC__ )
  70. # define thread_local __declspec(thread)
  71. /* note that ICC (linux) and Clang are covered by __GNUC__ */
  72. # elif (defined __GNUC__ || \
  73. defined __SUNPRO_C || \
  74. defined __xlC__) && !defined(__APPLE__)
  75. # define thread_local __thread
  76. # else
  77. # define UNSAFE
  78. #endif
  79. #endif
  80. #if defined USE_OPENMP
  81. #undef UNSAFE
  82. #endif
  83. #if !defined(TRANSA) && !defined(UNSAFE)
  84. #define Y_DUMMY_NUM 1024
  85. #if defined(USE_OPENMP)
  86. static FLOAT y_dummy[Y_DUMMY_NUM];
  87. #pragma omp threadprivate(y_dummy)
  88. # else
  89. static thread_local FLOAT y_dummy[Y_DUMMY_NUM];
  90. # endif
  91. #endif
  92. static int gemv_kernel(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, FLOAT *dummy1, FLOAT *buffer, BLASLONG pos){
  93. FLOAT *a, *x, *y;
  94. BLASLONG lda, incx, incy;
  95. BLASLONG m_from, m_to, n_from, n_to;
  96. a = (FLOAT *)args -> a;
  97. x = (FLOAT *)args -> b;
  98. y = (FLOAT *)args -> c;
  99. lda = args -> lda;
  100. incx = args -> ldb;
  101. incy = args -> ldc;
  102. m_from = 0;
  103. m_to = args -> m;
  104. if (range_m) {
  105. m_from = *(range_m + 0);
  106. m_to = *(range_m + 1);
  107. a += m_from * COMPSIZE;
  108. #ifndef TRANSA
  109. y += m_from * incy * COMPSIZE;
  110. #endif
  111. }
  112. n_from = 0;
  113. n_to = args -> n;
  114. if (range_n) {
  115. n_from = *(range_n + 0);
  116. n_to = *(range_n + 1);
  117. a += n_from * lda * COMPSIZE;
  118. #ifdef TRANSA
  119. y += n_from * incy * COMPSIZE;
  120. #else
  121. # ifndef UNSAFE
  122. //for split matrix row (n) direction and vector x of gemv_n
  123. x += n_from * incx * COMPSIZE;
  124. //store partial result for every thread
  125. y += (m_to - m_from) * 1 * COMPSIZE * pos;
  126. # endif
  127. #endif
  128. }
  129. //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);
  130. GEMV(m_to - m_from, n_to - n_from, 0,
  131. *((FLOAT *)args -> alpha + 0),
  132. #ifdef COMPLEX
  133. *((FLOAT *)args -> alpha + 1),
  134. #endif
  135. a, lda, x, incx, y, incy, buffer);
  136. return 0;
  137. }
  138. #ifndef COMPLEX
  139. int CNAME(BLASLONG m, BLASLONG n, FLOAT alpha, FLOAT *a, BLASLONG lda, FLOAT *x, BLASLONG incx, FLOAT *y, BLASLONG incy, FLOAT *buffer, int nthreads){
  140. #else
  141. int CNAME(BLASLONG m, BLASLONG n, FLOAT *alpha, FLOAT *a, BLASLONG lda, FLOAT *x, BLASLONG incx, FLOAT *y, BLASLONG incy, FLOAT *buffer, int nthreads){
  142. #endif
  143. blas_arg_t args;
  144. blas_queue_t queue[MAX_CPU_NUMBER];
  145. BLASLONG range[MAX_CPU_NUMBER + 1];
  146. BLASLONG width, i, num_cpu;
  147. #if !defined(TRANSA) && !defined(UNSAFE)
  148. int split_x=0;
  149. #endif
  150. #ifdef SMP
  151. #ifndef COMPLEX
  152. #ifdef XDOUBLE
  153. int mode = BLAS_XDOUBLE | BLAS_REAL;
  154. #elif defined(DOUBLE)
  155. int mode = BLAS_DOUBLE | BLAS_REAL;
  156. #else
  157. int mode = BLAS_SINGLE | BLAS_REAL;
  158. #endif
  159. #else
  160. #ifdef XDOUBLE
  161. int mode = BLAS_XDOUBLE | BLAS_COMPLEX;
  162. #elif defined(DOUBLE)
  163. int mode = BLAS_DOUBLE | BLAS_COMPLEX;
  164. #else
  165. int mode = BLAS_SINGLE | BLAS_COMPLEX;
  166. #endif
  167. #endif
  168. #endif
  169. args.m = m;
  170. args.n = n;
  171. args.a = (void *)a;
  172. args.b = (void *)x;
  173. args.c = (void *)y;
  174. args.lda = lda;
  175. args.ldb = incx;
  176. args.ldc = incy;
  177. #ifndef COMPLEX
  178. args.alpha = (void *)&alpha;
  179. #else
  180. args.alpha = (void *) alpha;
  181. #endif
  182. num_cpu = 0;
  183. range[0] = 0;
  184. #ifndef TRANSA
  185. i = m;
  186. #else
  187. i = n;
  188. #endif
  189. while (i > 0){
  190. width = blas_quickdivide(i + nthreads - num_cpu - 1, nthreads - num_cpu);
  191. if (width < 4) width = 4;
  192. if (i < width) width = i;
  193. range[num_cpu + 1] = range[num_cpu] + width;
  194. queue[num_cpu].mode = mode;
  195. queue[num_cpu].routine = gemv_kernel;
  196. queue[num_cpu].args = &args;
  197. #ifndef TRANSA
  198. queue[num_cpu].range_m = &range[num_cpu];
  199. queue[num_cpu].range_n = NULL;
  200. #else
  201. queue[num_cpu].range_m = NULL;
  202. queue[num_cpu].range_n = &range[num_cpu];
  203. #endif
  204. queue[num_cpu].sa = NULL;
  205. queue[num_cpu].sb = NULL;
  206. queue[num_cpu].next = &queue[num_cpu + 1];
  207. num_cpu ++;
  208. i -= width;
  209. }
  210. #if !defined(TRANSA) && !defined(UNSAFE)
  211. //try to split matrix on row direction and x.
  212. //Then, reduction.
  213. if (num_cpu < nthreads) {
  214. //too small to split or bigger than the y_dummy buffer.
  215. double MN = (double) m * (double) n;
  216. if ( MN <= (24.0 * 24.0 * (double) (GEMM_MULTITHREAD_THRESHOLD*GEMM_MULTITHREAD_THRESHOLD))
  217. || m*COMPSIZE*nthreads > Y_DUMMY_NUM)
  218. goto Outer;
  219. num_cpu = 0;
  220. range[0] = 0;
  221. memset(y_dummy, 0, sizeof(FLOAT) * m * COMPSIZE * nthreads);
  222. args.ldc = 1;
  223. args.c = (void *)y_dummy;
  224. //split on row (n) and x
  225. i=n;
  226. split_x=1;
  227. while (i > 0){
  228. width = blas_quickdivide(i + nthreads - num_cpu - 1, nthreads - num_cpu);
  229. if (width < 4) width = 4;
  230. if (i < width) width = i;
  231. range[num_cpu + 1] = range[num_cpu] + width;
  232. queue[num_cpu].mode = mode;
  233. queue[num_cpu].routine = gemv_kernel;
  234. queue[num_cpu].args = &args;
  235. queue[num_cpu].position = num_cpu;
  236. queue[num_cpu].range_m = NULL;
  237. queue[num_cpu].range_n = &range[num_cpu];
  238. queue[num_cpu].sa = NULL;
  239. queue[num_cpu].sb = NULL;
  240. queue[num_cpu].next = &queue[num_cpu + 1];
  241. num_cpu ++;
  242. i -= width;
  243. }
  244. }
  245. Outer:
  246. #endif
  247. if (num_cpu) {
  248. queue[0].sa = NULL;
  249. queue[0].sb = buffer;
  250. queue[num_cpu - 1].next = NULL;
  251. exec_blas(num_cpu, queue);
  252. }
  253. #if !defined(TRANSA) && !defined(UNSAFE)
  254. if(split_x==1){
  255. //reduction
  256. for(i=0; i<num_cpu; i++){
  257. int j;
  258. for(j=0; j<m; j++){
  259. y[j*incy*COMPSIZE] +=y_dummy[i*m*COMPSIZE + j*COMPSIZE];
  260. #ifdef COMPLEX
  261. y[j*incy*COMPSIZE+1] +=y_dummy[i*m*COMPSIZE + j*COMPSIZE+1];
  262. #endif
  263. }
  264. }
  265. }
  266. #endif
  267. return 0;
  268. }