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.

syr2_thread.c 11 kB

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