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.

sbmv_thread.c 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #if !defined(HEMV) && !defined(HEMVREV)
  42. #define MYAXPY AXPYU_K
  43. #define MYDOT DOTU_K
  44. #elif defined HEMV
  45. #define MYAXPY AXPYU_K
  46. #define MYDOT DOTC_K
  47. #else
  48. #define MYAXPY AXPYC_K
  49. #define MYDOT DOTU_K
  50. #endif
  51. static int sbmv_kernel(blas_arg_t *args, BLASLONG *range_m, BLASLONG *range_n, FLOAT *dummy1, FLOAT *buffer, BLASLONG pos){
  52. FLOAT *a, *x, *y;
  53. BLASLONG lda, incx;
  54. BLASLONG n, k, n_from, n_to;
  55. BLASLONG i, length;
  56. #ifndef COMPLEX
  57. FLOAT result;
  58. #else
  59. OPENBLAS_COMPLEX_FLOAT result;
  60. #endif
  61. a = (FLOAT *)args -> a;
  62. x = (FLOAT *)args -> b;
  63. lda = args -> lda;
  64. incx = args -> ldb;
  65. n = args -> n;
  66. k = args -> k;
  67. n_from = 0;
  68. n_to = n;
  69. //Use y as each thread's n* COMPSIZE elements in sb buffer
  70. y = buffer;
  71. buffer += ((COMPSIZE * n + 1023) & ~1023);
  72. if (range_m) {
  73. n_from = *(range_m + 0);
  74. n_to = *(range_m + 1);
  75. a += n_from * lda * COMPSIZE;
  76. }
  77. if (incx != 1) {
  78. COPY_K(n, x, incx, buffer, 1);
  79. x = buffer;
  80. // buffer += ((COMPSIZE * n + 1023) & ~1023);
  81. }
  82. SCAL_K(n, 0, 0, ZERO,
  83. #ifdef COMPLEX
  84. ZERO,
  85. #endif
  86. y, 1, NULL, 0, NULL, 0);
  87. for (i = n_from; i < n_to; i++) {
  88. #ifndef LOWER
  89. length = i;
  90. if (length > k) length = k;
  91. MYAXPY(length, 0, 0,
  92. *(x + i * COMPSIZE + 0),
  93. #ifdef COMPLEX
  94. *(x + i * COMPSIZE + 1),
  95. #endif
  96. a + (k - length) * COMPSIZE, 1, y + (i - length) * COMPSIZE, 1, NULL, 0);
  97. #if !defined(HEMV) && !defined(HEMVREV)
  98. result = MYDOT(length + 1, a + (k - length) * COMPSIZE, 1, x + (i - length) * COMPSIZE, 1);
  99. #else
  100. result = MYDOT(length , a + (k - length) * COMPSIZE, 1, x + (i - length) * COMPSIZE, 1);
  101. #endif
  102. #ifndef COMPLEX
  103. *(y + i * COMPSIZE + 0) += result;
  104. #else
  105. #if !defined(HEMV) && !defined(HEMVREV)
  106. *(y + i * COMPSIZE + 0) += CREAL(result);
  107. *(y + i * COMPSIZE + 1) += CIMAG(result);
  108. #else
  109. *(y + i * COMPSIZE + 0) += CREAL(result) + *(a + k * COMPSIZE) * *(x + i * COMPSIZE + 0);
  110. *(y + i * COMPSIZE + 1) += CIMAG(result) + *(a + k * COMPSIZE) * *(x + i * COMPSIZE + 1);
  111. #endif
  112. #endif
  113. #else
  114. length = k;
  115. if (n - i - 1 < k) length = n - i - 1;
  116. MYAXPY(length, 0, 0,
  117. *(x + i * COMPSIZE + 0),
  118. #ifdef COMPLEX
  119. *(x + i * COMPSIZE + 1),
  120. #endif
  121. a + COMPSIZE, 1, y + (i + 1) * COMPSIZE, 1, NULL, 0);
  122. #if !defined(HEMV) && !defined(HEMVREV)
  123. result = MYDOT(length + 1, a, 1, x + i * COMPSIZE, 1);
  124. #else
  125. result = MYDOT(length , a + COMPSIZE, 1, x + (i + 1) * COMPSIZE, 1) ;
  126. #endif
  127. #ifndef COMPLEX
  128. *(y + i * COMPSIZE + 0) += result;
  129. #else
  130. #if !defined(HEMV) && !defined(HEMVREV)
  131. *(y + i * COMPSIZE + 0) += CREAL(result);
  132. *(y + i * COMPSIZE + 1) += CIMAG(result);
  133. #else
  134. *(y + i * COMPSIZE + 0) += CREAL(result) + *a * *(x + i * COMPSIZE + 0);
  135. *(y + i * COMPSIZE + 1) += CIMAG(result) + *a * *(x + i * COMPSIZE + 1);
  136. #endif
  137. #endif
  138. #endif
  139. a += lda * COMPSIZE;
  140. }
  141. return 0;
  142. }
  143. #ifndef COMPLEX
  144. int CNAME(BLASLONG n, BLASLONG k, FLOAT alpha, FLOAT *a, BLASLONG lda, FLOAT *x, BLASLONG incx, FLOAT *y, BLASLONG incy, FLOAT *buffer, int nthreads){
  145. #else
  146. int CNAME(BLASLONG n, BLASLONG k, FLOAT *alpha, FLOAT *a, BLASLONG lda, FLOAT *x, BLASLONG incx, FLOAT *y, BLASLONG incy, FLOAT *buffer, int nthreads){
  147. #endif
  148. blas_arg_t args;
  149. blas_queue_t queue[MAX_CPU_NUMBER + 1];
  150. BLASLONG range_m[MAX_CPU_NUMBER + 1];
  151. BLASLONG range_n[MAX_CPU_NUMBER];
  152. BLASLONG width, i, num_cpu;
  153. double dnum;
  154. int mask = 7;
  155. #ifdef SMP
  156. #ifndef COMPLEX
  157. #ifdef XDOUBLE
  158. int mode = BLAS_XDOUBLE | BLAS_REAL;
  159. #elif defined(DOUBLE)
  160. int mode = BLAS_DOUBLE | BLAS_REAL;
  161. #else
  162. int mode = BLAS_SINGLE | BLAS_REAL;
  163. #endif
  164. #else
  165. #ifdef XDOUBLE
  166. int mode = BLAS_XDOUBLE | BLAS_COMPLEX;
  167. #elif defined(DOUBLE)
  168. int mode = BLAS_DOUBLE | BLAS_COMPLEX;
  169. #else
  170. int mode = BLAS_SINGLE | BLAS_COMPLEX;
  171. #endif
  172. #endif
  173. #endif
  174. args.n = n;
  175. args.k = k;
  176. args.a = (void *)a;
  177. args.b = (void *)x;
  178. args.c = (void *)buffer;
  179. args.lda = lda;
  180. args.ldb = incx;
  181. args.ldc = incy;
  182. dnum = (double)n * (double)n / (double)nthreads;
  183. num_cpu = 0;
  184. if (n < 2 * k) {
  185. #ifndef LOWER
  186. range_m[MAX_CPU_NUMBER] = n;
  187. i = 0;
  188. while (i < n){
  189. if (nthreads - num_cpu > 1) {
  190. double di = (double)(n - i);
  191. if (di * di - dnum > 0) {
  192. width = ((BLASLONG)(-sqrt(di * di - dnum) + di) + mask) & ~mask;
  193. } else {
  194. width = n - i;
  195. }
  196. if (width < 16) width = 16;
  197. if (width > n - i) width = n - i;
  198. } else {
  199. width = n - i;
  200. }
  201. range_m[MAX_CPU_NUMBER - num_cpu - 1] = range_m[MAX_CPU_NUMBER - num_cpu] - width;
  202. range_n[num_cpu] = num_cpu * (((n + 15) & ~15) + 16);
  203. if (range_n[num_cpu] > n * num_cpu) range_n[num_cpu] = n * num_cpu;
  204. queue[num_cpu].mode = mode;
  205. queue[num_cpu].routine = sbmv_kernel;
  206. queue[num_cpu].args = &args;
  207. queue[num_cpu].range_m = &range_m[MAX_CPU_NUMBER - num_cpu - 1];
  208. queue[num_cpu].range_n = &range_n[num_cpu];
  209. queue[num_cpu].sa = NULL;
  210. queue[num_cpu].sb = NULL;
  211. queue[num_cpu].next = &queue[num_cpu + 1];
  212. num_cpu ++;
  213. i += width;
  214. }
  215. #else
  216. range_m[0] = 0;
  217. i = 0;
  218. while (i < n){
  219. if (nthreads - num_cpu > 1) {
  220. double di = (double)(n - i);
  221. if (di * di - dnum > 0) {
  222. width = ((BLASLONG)(-sqrt(di * di - dnum) + di) + mask) & ~mask;
  223. } else {
  224. width = n - i;
  225. }
  226. if (width < 16) width = 16;
  227. if (width > n - i) width = n - i;
  228. } else {
  229. width = n - i;
  230. }
  231. range_m[num_cpu + 1] = range_m[num_cpu] + width;
  232. range_n[num_cpu] = num_cpu * (((n + 15) & ~15) + 16);
  233. if (range_n[num_cpu] > n * num_cpu) range_n[num_cpu] = n * num_cpu;
  234. queue[num_cpu].mode = mode;
  235. queue[num_cpu].routine = sbmv_kernel;
  236. queue[num_cpu].args = &args;
  237. queue[num_cpu].range_m = &range_m[num_cpu];
  238. queue[num_cpu].range_n = &range_n[num_cpu];
  239. queue[num_cpu].sa = NULL;
  240. queue[num_cpu].sb = NULL;
  241. queue[num_cpu].next = &queue[num_cpu + 1];
  242. num_cpu ++;
  243. i += width;
  244. }
  245. #endif
  246. } else {
  247. range_m[0] = 0;
  248. i = n;
  249. while (i > 0){
  250. width = blas_quickdivide(i + nthreads - num_cpu - 1, nthreads - num_cpu);
  251. if (width < 4) width = 4;
  252. if (i < width) width = i;
  253. range_m[num_cpu + 1] = range_m[num_cpu] + width;
  254. range_n[num_cpu] = num_cpu * ((n + 15) & ~15);
  255. if (range_n[num_cpu] > n * num_cpu) range_n[num_cpu] = n * num_cpu;
  256. queue[num_cpu].mode = mode;
  257. queue[num_cpu].routine = sbmv_kernel;
  258. queue[num_cpu].args = &args;
  259. queue[num_cpu].range_m = &range_m[num_cpu];
  260. queue[num_cpu].range_n = &range_n[num_cpu];
  261. queue[num_cpu].sa = NULL;
  262. queue[num_cpu].sb = NULL;
  263. queue[num_cpu].next = &queue[num_cpu + 1];
  264. num_cpu ++;
  265. i -= width;
  266. }
  267. }
  268. if (num_cpu) {
  269. queue[0].sa = NULL;
  270. queue[0].sb = buffer;
  271. queue[num_cpu - 1].next = NULL;
  272. exec_blas(num_cpu, queue);
  273. }
  274. for (i = 1; i < num_cpu; i ++) {
  275. AXPYU_K(n, 0, 0,
  276. #ifndef COMPLEX
  277. ONE,
  278. #else
  279. ONE, ZERO,
  280. #endif
  281. (FLOAT*)(queue[i].sb), 1, buffer, 1, NULL, 0);
  282. }
  283. AXPYU_K(n, 0, 0,
  284. #ifndef COMPLEX
  285. alpha,
  286. #else
  287. alpha[0], alpha[1],
  288. #endif
  289. buffer, 1, y, incy, NULL, 0);
  290. return 0;
  291. }