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.

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