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.

spr_thread.c 8.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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;
  43. BLASLONG incx;
  44. BLASLONG i, m_from, m_to;
  45. FLOAT alpha_r;
  46. #if defined(COMPLEX) && !defined(HEMV) && !defined(HEMVREV)
  47. FLOAT alpha_i;
  48. #endif
  49. x = (FLOAT *)args -> a;
  50. a = (FLOAT *)args -> b;
  51. incx = args -> lda;
  52. alpha_r = *((FLOAT *)args -> alpha + 0);
  53. #if defined(COMPLEX) && !defined(HEMV) && !defined(HEMVREV)
  54. alpha_i = *((FLOAT *)args -> alpha + 1);
  55. #endif
  56. m_from = 0;
  57. m_to = args -> m;
  58. if (range_m) {
  59. m_from = *(range_m + 0);
  60. m_to = *(range_m + 1);
  61. }
  62. if (incx != 1) {
  63. #ifndef LOWER
  64. COPY_K(m_to, x, incx, buffer, 1);
  65. #else
  66. COPY_K(args -> m - m_from, x + m_from * incx * COMPSIZE, incx, buffer + m_from * COMPSIZE, 1);
  67. #endif
  68. x = buffer;
  69. }
  70. #ifndef LOWER
  71. a += (m_from + 1) * m_from / 2 * COMPSIZE;
  72. #else
  73. a += (2 * args -> m - m_from + 1) * m_from / 2 * COMPSIZE;
  74. #endif
  75. for (i = m_from; i < m_to; i++){
  76. #if !defined(HEMV) && !defined(HEMVREV)
  77. #ifndef COMPLEX
  78. if (x[i] != ZERO) {
  79. #ifndef LOWER
  80. AXPYU_K(i + 1, 0, 0, alpha_r * x[i], x, 1, a, 1, NULL, 0);
  81. #else
  82. AXPYU_K(args -> m - i, 0, 0, alpha_r * x[i], x + i, 1, a, 1, NULL, 0);
  83. #endif
  84. }
  85. #else
  86. if ((x[i * COMPSIZE + 0] != ZERO) || (x[i * COMPSIZE + 1] != ZERO)) {
  87. #ifndef LOWER
  88. AXPYU_K(i + 1, 0, 0,
  89. alpha_r * x[i * COMPSIZE + 0] - alpha_i * x[i * COMPSIZE + 1],
  90. alpha_i * x[i * COMPSIZE + 0] + alpha_r * x[i * COMPSIZE + 1],
  91. x, 1, a, 1, NULL, 0);
  92. #else
  93. AXPYU_K(args -> m - i, 0, 0,
  94. alpha_r * x[i * COMPSIZE + 0] - alpha_i * x[i * COMPSIZE + 1],
  95. alpha_i * x[i * COMPSIZE + 0] + alpha_r * x[i * COMPSIZE + 1],
  96. x + i * COMPSIZE, 1, a, 1, NULL, 0);
  97. #endif
  98. }
  99. #endif
  100. #else
  101. if ((x[i * COMPSIZE + 0] != ZERO) || (x[i * COMPSIZE + 1] != ZERO)) {
  102. #ifndef HEMVREV
  103. #ifndef LOWER
  104. AXPYU_K(i + 1, 0, 0,
  105. alpha_r * x[i * COMPSIZE + 0], - alpha_r * x[i * COMPSIZE + 1],
  106. x, 1, a, 1, NULL, 0);
  107. #else
  108. AXPYU_K(args -> m - i, 0, 0,
  109. alpha_r * x[i * COMPSIZE + 0], - alpha_r * x[i * COMPSIZE + 1],
  110. x + i * COMPSIZE, 1, a, 1, NULL, 0);
  111. #endif
  112. #else
  113. #ifndef LOWER
  114. AXPYC_K(i + 1, 0, 0,
  115. alpha_r * x[i * COMPSIZE + 0], alpha_r * x[i * COMPSIZE + 1],
  116. x, 1, a, 1, NULL, 0);
  117. #else
  118. AXPYC_K(args -> m - i, 0, 0,
  119. alpha_r * x[i * COMPSIZE + 0], alpha_r * x[i * COMPSIZE + 1],
  120. x + i * COMPSIZE, 1, a, 1, NULL, 0);
  121. #endif
  122. #endif
  123. }
  124. #ifndef LOWER
  125. a[i * COMPSIZE + 1] = ZERO;
  126. #else
  127. a[ 1] = ZERO;
  128. #endif
  129. #endif
  130. #ifndef LOWER
  131. a += (i + 1) * COMPSIZE;
  132. #else
  133. a += (args -> m - i) * COMPSIZE;
  134. #endif
  135. }
  136. return 0;
  137. }
  138. #if !defined(COMPLEX) || defined(HEMV) || defined(HEMVREV)
  139. int CNAME(BLASLONG m, FLOAT alpha, FLOAT *x, BLASLONG incx, FLOAT *a, FLOAT *buffer, int nthreads){
  140. #else
  141. int CNAME(BLASLONG m, FLOAT *alpha, FLOAT *x, BLASLONG incx, FLOAT *a, FLOAT *buffer, int nthreads){
  142. #endif
  143. blas_arg_t args;
  144. blas_queue_t queue[MAX_CPU_NUMBER];
  145. BLASLONG range_m[MAX_CPU_NUMBER + 1];
  146. BLASLONG width, i, num_cpu;
  147. double dnum;
  148. int mask = 7;
  149. #ifdef SMP
  150. #ifndef COMPLEX
  151. #ifdef XDOUBLE
  152. int mode = BLAS_XDOUBLE | BLAS_REAL;
  153. #elif defined(DOUBLE)
  154. int mode = BLAS_DOUBLE | BLAS_REAL;
  155. #else
  156. int mode = BLAS_SINGLE | BLAS_REAL;
  157. #endif
  158. #else
  159. #ifdef XDOUBLE
  160. int mode = BLAS_XDOUBLE | BLAS_COMPLEX;
  161. #elif defined(DOUBLE)
  162. int mode = BLAS_DOUBLE | BLAS_COMPLEX;
  163. #else
  164. int mode = BLAS_SINGLE | BLAS_COMPLEX;
  165. #endif
  166. #endif
  167. #endif
  168. args.m = m;
  169. args.a = (void *)x;
  170. args.b = (void *)a;
  171. args.lda = incx;
  172. #if !defined(COMPLEX) || defined(HEMV) || defined(HEMVREV)
  173. args.alpha = (void *)&alpha;
  174. #else
  175. args.alpha = (void *)alpha;
  176. #endif
  177. dnum = (double)m * (double)m / (double)nthreads;
  178. num_cpu = 0;
  179. #ifndef LOWER
  180. range_m[MAX_CPU_NUMBER] = m;
  181. i = 0;
  182. while (i < m){
  183. if (nthreads - num_cpu > 1) {
  184. double di = (double)(m - i);
  185. if (di * di - dnum > 0) {
  186. width = ((BLASLONG)(-sqrt(di * di - dnum) + di) + mask) & ~mask;
  187. } else {
  188. width = m - i;
  189. }
  190. if (width < 16) width = 16;
  191. if (width > m - i) width = m - i;
  192. } else {
  193. width = m - i;
  194. }
  195. range_m[MAX_CPU_NUMBER - num_cpu - 1] = range_m[MAX_CPU_NUMBER - num_cpu] - width;
  196. queue[num_cpu].mode = mode;
  197. queue[num_cpu].routine = syr_kernel;
  198. queue[num_cpu].args = &args;
  199. queue[num_cpu].range_m = &range_m[MAX_CPU_NUMBER - num_cpu - 1];
  200. queue[num_cpu].range_n = NULL;
  201. queue[num_cpu].sa = NULL;
  202. queue[num_cpu].sb = NULL;
  203. queue[num_cpu].next = &queue[num_cpu + 1];
  204. num_cpu ++;
  205. i += width;
  206. }
  207. #else
  208. range_m[0] = 0;
  209. i = 0;
  210. while (i < m){
  211. if (nthreads - num_cpu > 1) {
  212. double di = (double)(m - i);
  213. if (di * di - dnum > 0) {
  214. width = ((BLASLONG)(-sqrt(di * di - dnum) + di) + mask) & ~mask;
  215. } else {
  216. width = m - i;
  217. }
  218. if (width < 16) width = 16;
  219. if (width > m - i) width = m - i;
  220. } else {
  221. width = m - i;
  222. }
  223. range_m[num_cpu + 1] = range_m[num_cpu] + width;
  224. queue[num_cpu].mode = mode;
  225. queue[num_cpu].routine = syr_kernel;
  226. queue[num_cpu].args = &args;
  227. queue[num_cpu].range_m = &range_m[num_cpu];
  228. queue[num_cpu].range_n = NULL;
  229. queue[num_cpu].sa = NULL;
  230. queue[num_cpu].sb = NULL;
  231. queue[num_cpu].next = &queue[num_cpu + 1];
  232. num_cpu ++;
  233. i += width;
  234. }
  235. #endif
  236. if (num_cpu) {
  237. queue[0].sa = NULL;
  238. queue[0].sb = buffer;
  239. queue[num_cpu - 1].next = NULL;
  240. exec_blas(num_cpu, queue);
  241. }
  242. return 0;
  243. }