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.

tpmv_thread.c 9.8 kB

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