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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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
  153. #ifndef C_MSVC
  154. __attribute__((visibility("hidden")))
  155. #endif
  156. CNAME(BLASLONG m, FLOAT alpha, FLOAT *a, FLOAT *x, BLASLONG incx, FLOAT *y, BLASLONG incy, FLOAT *buffer, int nthreads){
  157. #else
  158. int
  159. #ifndef C_MSVC
  160. __attribute__((visibility("hidden")))
  161. #endif
  162. CNAME(BLASLONG m, FLOAT *alpha, FLOAT *a, FLOAT *x, BLASLONG incx, FLOAT *y, BLASLONG incy, FLOAT *buffer, int nthreads){
  163. #endif
  164. blas_arg_t args;
  165. blas_queue_t queue[MAX_CPU_NUMBER];
  166. BLASLONG range_m[MAX_CPU_NUMBER + 1];
  167. BLASLONG range_n[MAX_CPU_NUMBER + 1];
  168. BLASLONG width, i, num_cpu;
  169. double dnum;
  170. int mask = 7;
  171. #ifdef SMP
  172. #ifndef COMPLEX
  173. #ifdef XDOUBLE
  174. int mode = BLAS_XDOUBLE | BLAS_REAL;
  175. #elif defined(DOUBLE)
  176. int mode = BLAS_DOUBLE | BLAS_REAL;
  177. #else
  178. int mode = BLAS_SINGLE | BLAS_REAL;
  179. #endif
  180. #else
  181. #ifdef XDOUBLE
  182. int mode = BLAS_XDOUBLE | BLAS_COMPLEX;
  183. #elif defined(DOUBLE)
  184. int mode = BLAS_DOUBLE | BLAS_COMPLEX;
  185. #else
  186. int mode = BLAS_SINGLE | BLAS_COMPLEX;
  187. #endif
  188. #endif
  189. #endif
  190. args.m = m;
  191. args.a = (void *)a;
  192. args.b = (void *)x;
  193. args.c = (void *)buffer;
  194. args.ldb = incx;
  195. args.ldc = incy;
  196. dnum = (double)m * (double)m / (double)nthreads;
  197. num_cpu = 0;
  198. #ifndef LOWER
  199. range_m[MAX_CPU_NUMBER] = m;
  200. i = 0;
  201. while (i < m){
  202. if (nthreads - num_cpu > 1) {
  203. double di = (double)(m - i);
  204. if (di * di - dnum > 0) {
  205. width = ((BLASLONG)(-sqrt(di * di - dnum) + di) + mask) & ~mask;
  206. } else {
  207. width = m - i;
  208. }
  209. if (width < 16) width = 16;
  210. if (width > m - i) width = m - i;
  211. } else {
  212. width = m - i;
  213. }
  214. range_m[MAX_CPU_NUMBER - num_cpu - 1] = range_m[MAX_CPU_NUMBER - num_cpu] - width;
  215. range_n[num_cpu] = num_cpu * (((m + 15) & ~15) + 16);
  216. if (range_n[num_cpu] > m * num_cpu) range_n[num_cpu] = m * num_cpu;
  217. queue[num_cpu].mode = mode;
  218. queue[num_cpu].routine = spmv_kernel;
  219. queue[num_cpu].args = &args;
  220. queue[num_cpu].range_m = &range_m[MAX_CPU_NUMBER - num_cpu - 1];
  221. queue[num_cpu].range_n = &range_n[num_cpu];
  222. queue[num_cpu].sa = NULL;
  223. queue[num_cpu].sb = NULL;
  224. queue[num_cpu].next = &queue[num_cpu + 1];
  225. num_cpu ++;
  226. i += width;
  227. }
  228. #else
  229. range_m[0] = 0;
  230. i = 0;
  231. while (i < m){
  232. if (nthreads - num_cpu > 1) {
  233. double di = (double)(m - i);
  234. if (di * di - dnum > 0) {
  235. width = ((BLASLONG)(-sqrt(di * di - dnum) + di) + mask) & ~mask;
  236. } else {
  237. width = m - i;
  238. }
  239. if (width < 16) width = 16;
  240. if (width > m - i) width = m - i;
  241. } else {
  242. width = m - i;
  243. }
  244. range_m[num_cpu + 1] = range_m[num_cpu] + width;
  245. range_n[num_cpu] = num_cpu * (((m + 15) & ~15) + 16);
  246. if (range_n[num_cpu] > m * num_cpu) range_n[num_cpu] = m * num_cpu;
  247. queue[num_cpu].mode = mode;
  248. queue[num_cpu].routine = spmv_kernel;
  249. queue[num_cpu].args = &args;
  250. queue[num_cpu].range_m = &range_m[num_cpu];
  251. queue[num_cpu].range_n = &range_n[num_cpu];
  252. queue[num_cpu].sa = NULL;
  253. queue[num_cpu].sb = NULL;
  254. queue[num_cpu].next = &queue[num_cpu + 1];
  255. num_cpu ++;
  256. i += width;
  257. }
  258. #endif
  259. if (num_cpu) {
  260. queue[0].sa = NULL;
  261. queue[0].sb = buffer + num_cpu * (((m + 255) & ~255) + 16) * COMPSIZE;
  262. queue[num_cpu - 1].next = NULL;
  263. exec_blas(num_cpu, queue);
  264. }
  265. for (i = 1; i < num_cpu; i ++) {
  266. #ifndef LOWER
  267. AXPYU_K(range_m[MAX_CPU_NUMBER - i], 0, 0, ONE,
  268. #ifdef COMPLEX
  269. ZERO,
  270. #endif
  271. buffer + range_n[i] * COMPSIZE, 1, buffer, 1, NULL, 0);
  272. #else
  273. AXPYU_K(m - range_m[i], 0, 0, ONE,
  274. #ifdef COMPLEX
  275. ZERO,
  276. #endif
  277. buffer + (range_n[i] + range_m[i]) * COMPSIZE, 1, buffer + range_m[i] * COMPSIZE, 1, NULL, 0);
  278. #endif
  279. }
  280. AXPYU_K(m, 0, 0,
  281. #ifndef COMPLEX
  282. alpha,
  283. #else
  284. alpha[0], alpha[1],
  285. #endif
  286. buffer, 1, y, incy, NULL, 0);
  287. return 0;
  288. }