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.

syr_thread.c 8.4 kB

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