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.

spr2_thread.c 11 kB

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