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.4 kB

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