xlu_kernels.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include "xlu.h"
  17. #include <math.h>
  18. /*
  19. * U22
  20. */
  21. static inline void STARPU_LU(common_u22)(void *descr[],
  22. int s, __attribute__((unused)) void *_args)
  23. {
  24. TYPE *right = (TYPE *)GET_BLAS_PTR(descr[0]);
  25. TYPE *left = (TYPE *)GET_BLAS_PTR(descr[1]);
  26. TYPE *center = (TYPE *)GET_BLAS_PTR(descr[2]);
  27. unsigned dx = GET_BLAS_NX(descr[2]);
  28. unsigned dy = GET_BLAS_NY(descr[2]);
  29. unsigned dz = GET_BLAS_NY(descr[0]);
  30. unsigned ld12 = GET_BLAS_LD(descr[0]);
  31. unsigned ld21 = GET_BLAS_LD(descr[1]);
  32. unsigned ld22 = GET_BLAS_LD(descr[2]);
  33. #ifdef USE_CUDA
  34. cublasStatus status;
  35. cudaError_t cures;
  36. #endif
  37. switch (s) {
  38. case 0:
  39. CPU_GEMM("N", "N", dy, dx, dz,
  40. (TYPE)-1.0, right, ld21, left, ld12,
  41. (TYPE)1.0, center, ld22);
  42. break;
  43. #ifdef USE_CUDA
  44. case 1:
  45. CUBLAS_GEMM('n', 'n', dx, dy, dz,
  46. (TYPE)-1.0, right, ld21, left, ld12,
  47. (TYPE)1.0f, center, ld22);
  48. status = cublasGetError();
  49. if (STARPU_UNLIKELY(status != CUBLAS_STATUS_SUCCESS))
  50. STARPU_ABORT();
  51. if (STARPU_UNLIKELY((cures = cudaThreadSynchronize()) != cudaSuccess))
  52. CUDA_REPORT_ERROR(cures);
  53. break;
  54. #endif
  55. default:
  56. STARPU_ABORT();
  57. break;
  58. }
  59. }
  60. void STARPU_LU(cpu_u22)(void *descr[], void *_args)
  61. {
  62. STARPU_LU(common_u22)(descr, 0, _args);
  63. }
  64. #ifdef USE_CUDA
  65. void STARPU_LU(cublas_u22)(void *descr[], void *_args)
  66. {
  67. STARPU_LU(common_u22)(descr, 1, _args);
  68. }
  69. #endif// USE_CUDA
  70. /*
  71. * U12
  72. */
  73. static inline void STARPU_LU(common_u12)(void *descr[],
  74. int s, __attribute__((unused)) void *_args)
  75. {
  76. TYPE *sub11;
  77. TYPE *sub12;
  78. sub11 = (TYPE *)GET_BLAS_PTR(descr[0]);
  79. sub12 = (TYPE *)GET_BLAS_PTR(descr[1]);
  80. unsigned ld11 = GET_BLAS_LD(descr[0]);
  81. unsigned ld12 = GET_BLAS_LD(descr[1]);
  82. unsigned nx12 = GET_BLAS_NX(descr[1]);
  83. unsigned ny12 = GET_BLAS_NY(descr[1]);
  84. #ifdef USE_CUDA
  85. cublasStatus status;
  86. cudaError_t cures;
  87. #endif
  88. /* solve L11 U12 = A12 (find U12) */
  89. switch (s) {
  90. case 0:
  91. CPU_TRSM("L", "L", "N", "N", nx12, ny12,
  92. (TYPE)1.0, sub11, ld11, sub12, ld12);
  93. break;
  94. #ifdef USE_CUDA
  95. case 1:
  96. CUBLAS_TRSM('L', 'L', 'N', 'N', ny12, nx12,
  97. (TYPE)1.0, sub11, ld11, sub12, ld12);
  98. status = cublasGetError();
  99. if (STARPU_UNLIKELY(status != CUBLAS_STATUS_SUCCESS))
  100. STARPU_ABORT();
  101. if (STARPU_UNLIKELY((cures = cudaThreadSynchronize()) != cudaSuccess))
  102. CUDA_REPORT_ERROR(cures);
  103. break;
  104. #endif
  105. default:
  106. STARPU_ABORT();
  107. break;
  108. }
  109. }
  110. void STARPU_LU(cpu_u12)(void *descr[], void *_args)
  111. {
  112. STARPU_LU(common_u12)(descr, 0, _args);
  113. }
  114. #ifdef USE_CUDA
  115. void STARPU_LU(cublas_u12)(void *descr[], void *_args)
  116. {
  117. STARPU_LU(common_u12)(descr, 1, _args);
  118. }
  119. #endif // USE_CUDA
  120. /*
  121. * U21
  122. */
  123. static inline void STARPU_LU(common_u21)(void *descr[],
  124. int s, __attribute__((unused)) void *_args)
  125. {
  126. TYPE *sub11;
  127. TYPE *sub21;
  128. sub11 = (TYPE *)GET_BLAS_PTR(descr[0]);
  129. sub21 = (TYPE *)GET_BLAS_PTR(descr[1]);
  130. unsigned ld11 = GET_BLAS_LD(descr[0]);
  131. unsigned ld21 = GET_BLAS_LD(descr[1]);
  132. unsigned nx21 = GET_BLAS_NX(descr[1]);
  133. unsigned ny21 = GET_BLAS_NY(descr[1]);
  134. #ifdef USE_CUDA
  135. cublasStatus status;
  136. cudaError_t cures;
  137. #endif
  138. switch (s) {
  139. case 0:
  140. CPU_TRSM("R", "U", "N", "U", nx21, ny21,
  141. (TYPE)1.0, sub11, ld11, sub21, ld21);
  142. break;
  143. #ifdef USE_CUDA
  144. case 1:
  145. CUBLAS_TRSM('R', 'U', 'N', 'U', ny21, nx21,
  146. (TYPE)1.0, sub11, ld11, sub21, ld21);
  147. status = cublasGetError();
  148. if (status != CUBLAS_STATUS_SUCCESS)
  149. STARPU_ABORT();
  150. cudaThreadSynchronize();
  151. break;
  152. #endif
  153. default:
  154. STARPU_ABORT();
  155. break;
  156. }
  157. }
  158. void STARPU_LU(cpu_u21)(void *descr[], void *_args)
  159. {
  160. STARPU_LU(common_u21)(descr, 0, _args);
  161. }
  162. #ifdef USE_CUDA
  163. void STARPU_LU(cublas_u21)(void *descr[], void *_args)
  164. {
  165. STARPU_LU(common_u21)(descr, 1, _args);
  166. }
  167. #endif
  168. /*
  169. * U11
  170. */
  171. static inline void STARPU_LU(common_u11)(void *descr[],
  172. int s, __attribute__((unused)) void *_args)
  173. {
  174. TYPE *sub11;
  175. sub11 = (TYPE *)GET_BLAS_PTR(descr[0]);
  176. unsigned long nx = GET_BLAS_NX(descr[0]);
  177. unsigned long ld = GET_BLAS_LD(descr[0]);
  178. unsigned long z;
  179. switch (s) {
  180. case 0:
  181. for (z = 0; z < nx; z++)
  182. {
  183. TYPE pivot;
  184. pivot = sub11[z+z*ld];
  185. STARPU_ASSERT(pivot != 0.0);
  186. CPU_SCAL(nx - z - 1, (1.0/pivot), &sub11[z+(z+1)*ld], ld);
  187. CPU_GER(nx - z - 1, nx - z - 1, -1.0,
  188. &sub11[(z+1)+z*ld], 1,
  189. &sub11[z+(z+1)*ld], ld,
  190. &sub11[(z+1) + (z+1)*ld],ld);
  191. }
  192. break;
  193. #ifdef USE_CUDA
  194. case 1:
  195. for (z = 0; z < nx; z++)
  196. {
  197. TYPE pivot;
  198. cudaMemcpy(&pivot, &sub11[z+z*ld], sizeof(TYPE), cudaMemcpyDeviceToHost);
  199. cudaStreamSynchronize(0);
  200. STARPU_ASSERT(pivot != 0.0);
  201. CUBLAS_SCAL(nx - z - 1, 1.0/pivot, &sub11[z+(z+1)*ld], ld);
  202. CUBLAS_GER(nx - z - 1, nx - z - 1, -1.0,
  203. &sub11[(z+1)+z*ld], 1,
  204. &sub11[z+(z+1)*ld], ld,
  205. &sub11[(z+1) + (z+1)*ld],ld);
  206. }
  207. cudaThreadSynchronize();
  208. break;
  209. #endif
  210. default:
  211. STARPU_ABORT();
  212. break;
  213. }
  214. }
  215. void STARPU_LU(cpu_u11)(void *descr[], void *_args)
  216. {
  217. STARPU_LU(common_u11)(descr, 0, _args);
  218. }
  219. #ifdef USE_CUDA
  220. void STARPU_LU(cublas_u11)(void *descr[], void *_args)
  221. {
  222. STARPU_LU(common_u11)(descr, 1, _args);
  223. }
  224. #endif// USE_CUDA
  225. /*
  226. * U11 with pivoting
  227. */
  228. static inline void STARPU_LU(common_u11_pivot)(void *descr[],
  229. int s, void *_args)
  230. {
  231. TYPE *sub11;
  232. sub11 = (TYPE *)GET_BLAS_PTR(descr[0]);
  233. unsigned long nx = GET_BLAS_NX(descr[0]);
  234. unsigned long ld = GET_BLAS_LD(descr[0]);
  235. unsigned long z;
  236. struct piv_s *piv = _args;
  237. unsigned *ipiv = piv->piv;
  238. unsigned first = piv->first;
  239. int i,j;
  240. switch (s) {
  241. case 0:
  242. for (z = 0; z < nx; z++)
  243. {
  244. TYPE pivot;
  245. pivot = sub11[z+z*ld];
  246. if (fabs((double)(pivot)) < PIVOT_THRESHHOLD)
  247. {
  248. /* find the pivot */
  249. int piv_ind = CPU_IAMAX(nx - z, &sub11[z*(ld+1)], ld);
  250. ipiv[z + first] = piv_ind + z + first;
  251. /* swap if needed */
  252. if (piv_ind != 0)
  253. {
  254. CPU_SWAP(nx, &sub11[z*ld], 1, &sub11[(z+piv_ind)*ld], 1);
  255. }
  256. pivot = sub11[z+z*ld];
  257. }
  258. STARPU_ASSERT(pivot != 0.0);
  259. CPU_SCAL(nx - z - 1, (1.0/pivot), &sub11[z+(z+1)*ld], ld);
  260. CPU_GER(nx - z - 1, nx - z - 1, -1.0,
  261. &sub11[(z+1)+z*ld], 1,
  262. &sub11[z+(z+1)*ld], ld,
  263. &sub11[(z+1) + (z+1)*ld],ld);
  264. }
  265. break;
  266. #ifdef USE_CUDA
  267. case 1:
  268. for (z = 0; z < nx; z++)
  269. {
  270. TYPE pivot;
  271. cudaMemcpy(&pivot, &sub11[z+z*ld], sizeof(TYPE), cudaMemcpyDeviceToHost);
  272. cudaStreamSynchronize(0);
  273. if (fabs((double)(pivot)) < PIVOT_THRESHHOLD)
  274. {
  275. /* find the pivot */
  276. int piv_ind = CUBLAS_IAMAX(nx - z, &sub11[z*(ld+1)], ld) - 1;
  277. ipiv[z + first] = piv_ind + z + first;
  278. /* swap if needed */
  279. if (piv_ind != 0)
  280. {
  281. CUBLAS_SWAP(nx, &sub11[z*ld], 1, &sub11[(z+piv_ind)*ld], 1);
  282. }
  283. cudaMemcpy(&pivot, &sub11[z+z*ld], sizeof(TYPE), cudaMemcpyDeviceToHost);
  284. cudaStreamSynchronize(0);
  285. }
  286. STARPU_ASSERT(pivot != 0.0);
  287. CUBLAS_SCAL(nx - z - 1, 1.0/pivot, &sub11[z+(z+1)*ld], ld);
  288. CUBLAS_GER(nx - z - 1, nx - z - 1, -1.0,
  289. &sub11[(z+1)+z*ld], 1,
  290. &sub11[z+(z+1)*ld], ld,
  291. &sub11[(z+1) + (z+1)*ld],ld);
  292. }
  293. cudaThreadSynchronize();
  294. break;
  295. #endif
  296. default:
  297. STARPU_ABORT();
  298. break;
  299. }
  300. }
  301. void STARPU_LU(cpu_u11_pivot)(void *descr[], void *_args)
  302. {
  303. STARPU_LU(common_u11_pivot)(descr, 0, _args);
  304. }
  305. #ifdef USE_CUDA
  306. void STARPU_LU(cublas_u11_pivot)(void *descr[], void *_args)
  307. {
  308. STARPU_LU(common_u11_pivot)(descr, 1, _args);
  309. }
  310. #endif// USE_CUDA
  311. /*
  312. * Pivoting
  313. */
  314. static inline void STARPU_LU(common_pivot)(void *descr[],
  315. int s, void *_args)
  316. {
  317. TYPE *matrix;
  318. matrix = (TYPE *)GET_BLAS_PTR(descr[0]);
  319. unsigned long nx = GET_BLAS_NX(descr[0]);
  320. unsigned long ld = GET_BLAS_LD(descr[0]);
  321. unsigned row, rowaux;
  322. struct piv_s *piv = _args;
  323. unsigned *ipiv = piv->piv;
  324. unsigned first = piv->first;
  325. unsigned last = piv->last;
  326. switch (s) {
  327. case 0:
  328. for (row = 0; row < nx; row++)
  329. {
  330. unsigned rowpiv = ipiv[row+first] - first;
  331. if (rowpiv != row)
  332. {
  333. CPU_SWAP(nx, &matrix[row*ld], 1, &matrix[rowpiv*ld], 1);
  334. }
  335. }
  336. break;
  337. #ifdef USE_CUDA
  338. case 1:
  339. for (row = 0; row < nx; row++)
  340. {
  341. unsigned rowpiv = ipiv[row+first] - first;
  342. if (rowpiv != row)
  343. {
  344. CUBLAS_SWAP(nx, &matrix[row*ld], 1, &matrix[rowpiv*ld], 1);
  345. }
  346. }
  347. cudaThreadSynchronize();
  348. break;
  349. #endif
  350. default:
  351. STARPU_ABORT();
  352. break;
  353. }
  354. }
  355. void STARPU_LU(cpu_pivot)(void *descr[], void *_args)
  356. {
  357. STARPU_LU(common_pivot)(descr, 0, _args);
  358. }
  359. #ifdef USE_CUDA
  360. void STARPU_LU(cublas_pivot)(void *descr[], void *_args)
  361. {
  362. STARPU_LU(common_pivot)(descr, 1, _args);
  363. }
  364. #endif// USE_CUDA