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

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