cholesky_kernels.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include "cholesky.h"
  18. #include "cholesky_kernels.h"
  19. #include "common/blas.h"
  20. #ifdef STARPU_USE_CUDA
  21. #include <cuda.h>
  22. #include <cuda_runtime.h>
  23. #include <cublas.h>
  24. #ifdef STARPU_HAVE_MAGMA
  25. #include "magma.h"
  26. #include "magma_lapack.h"
  27. #endif
  28. #endif
  29. /*
  30. * U22
  31. */
  32. static inline void chol_common_cpu_codelet_update_u22(const float *left, const float *right, float *center, unsigned dx, unsigned dy, unsigned dz,
  33. unsigned ld21, unsigned ld12, unsigned ld22, int s)
  34. {
  35. //printf("22\n");
  36. #ifdef STARPU_USE_CUDA
  37. cublasStatus st;
  38. #endif
  39. switch (s) {
  40. case 0:
  41. SGEMM("N", "T", dy, dx, dz, -1.0f, left, ld21,
  42. right, ld12, 1.0f, center, ld22);
  43. break;
  44. #ifdef STARPU_USE_CUDA
  45. case 1:
  46. cublasSgemm('n', 't', dy, dx, dz,
  47. -1.0f, left, ld21, right, ld12,
  48. 1.0f, center, ld22);
  49. st = cublasGetError();
  50. STARPU_ASSERT(!st);
  51. cudaThreadSynchronize();
  52. break;
  53. #endif
  54. default:
  55. STARPU_ABORT();
  56. break;
  57. }
  58. }
  59. void chol_cpu_codelet_update_u22(const float *left, const float *right, float *center, unsigned dx, unsigned dy, unsigned dz,
  60. unsigned ld21, unsigned ld12, unsigned ld22)
  61. __attribute__ ((task_implementation ("cpu", chol_codelet_update_u22)));
  62. #ifdef STARPU_USE_CUDA
  63. void chol_cublas_codelet_update_u22(const float *left, const float *right, float *center, unsigned dx, unsigned dy, unsigned dz,
  64. unsigned ld21, unsigned ld12, unsigned ld22)
  65. __attribute__ ((task_implementation ("cuda", chol_codelet_update_u22)));
  66. #endif
  67. void chol_cpu_codelet_update_u22(const float *left, const float *right, float *center, unsigned dx, unsigned dy, unsigned dz,
  68. unsigned ld21, unsigned ld12, unsigned ld22)
  69. {
  70. chol_common_cpu_codelet_update_u22(left, right, center, dx, dx, dz, ld21, ld12, ld22, 0);
  71. }
  72. #ifdef STARPU_USE_CUDA
  73. void chol_cublas_codelet_update_u22(const float *left, const float *right, float *center, unsigned dx, unsigned dy, unsigned dz,
  74. unsigned ld21, unsigned ld12, unsigned ld22)
  75. {
  76. chol_common_cpu_codelet_update_u22(left, right, center, dx, dx, dz, ld21, ld12, ld22, 1);
  77. }
  78. #endif// STARPU_USE_CUDA
  79. /*
  80. * U21
  81. */
  82. static inline void chol_common_codelet_update_u21(const float *sub11, float *sub21, unsigned ld11, unsigned ld21, unsigned nx21, unsigned ny21, int s)
  83. {
  84. switch (s) {
  85. case 0:
  86. STRSM("R", "L", "T", "N", nx21, ny21, 1.0f, sub11, ld11, sub21, ld21);
  87. break;
  88. #ifdef STARPU_USE_CUDA
  89. case 1:
  90. cublasStrsm('R', 'L', 'T', 'N', nx21, ny21, 1.0f, sub11, ld11, sub21, ld21);
  91. cudaThreadSynchronize();
  92. break;
  93. #endif
  94. default:
  95. STARPU_ABORT();
  96. break;
  97. }
  98. }
  99. void chol_cpu_codelet_update_u21(const float *sub11, float *sub21, unsigned ld11, unsigned ld21, unsigned nx21, unsigned ny21)
  100. __attribute__ ((task_implementation ("cpu", chol_codelet_update_u21)));
  101. #ifdef STARPU_USE_CUDA
  102. void chol_cublas_codelet_update_u21(const float *sub11, float *sub21, unsigned ld11, unsigned ld21, unsigned nx21, unsigned ny21)
  103. __attribute__ ((task_implementation ("cuda", chol_codelet_update_u21)));
  104. #endif
  105. void chol_cpu_codelet_update_u21(const float *sub11, float *sub21, unsigned ld11, unsigned ld21, unsigned nx21, unsigned ny21)
  106. {
  107. chol_common_codelet_update_u21(sub11, sub21, ld11, ld21, nx21, ny21, 0);
  108. }
  109. #ifdef STARPU_USE_CUDA
  110. void chol_cublas_codelet_update_u21(const float *sub11, float *sub21, unsigned ld11, unsigned ld21, unsigned nx21, unsigned ny21)
  111. {
  112. chol_common_codelet_update_u21(sub11, sub21, ld11, ld21, nx21, ny21, 1);
  113. }
  114. #endif
  115. /*
  116. * U11
  117. */
  118. static inline void chol_common_codelet_update_u11(float *sub11, unsigned nx, unsigned ld, int s)
  119. {
  120. unsigned z;
  121. switch (s) {
  122. case 0:
  123. /*
  124. * - alpha 11 <- lambda 11 = sqrt(alpha11)
  125. * - alpha 21 <- l 21 = alpha 21 / lambda 11
  126. * - A22 <- A22 - l21 trans(l21)
  127. */
  128. for (z = 0; z < nx; z++)
  129. {
  130. float lambda11;
  131. lambda11 = sqrt(sub11[z+z*ld]);
  132. sub11[z+z*ld] = lambda11;
  133. STARPU_ASSERT(lambda11 != 0.0f);
  134. SSCAL(nx - z - 1, 1.0f/lambda11, &sub11[(z+1)+z*ld], 1);
  135. SSYR("L", nx - z - 1, -1.0f,
  136. &sub11[(z+1)+z*ld], 1,
  137. &sub11[(z+1)+(z+1)*ld], ld);
  138. }
  139. break;
  140. #ifdef STARPU_USE_CUDA
  141. case 1:
  142. #ifdef STARPU_HAVE_MAGMA
  143. {
  144. int ret;
  145. int info;
  146. ret = magma_spotrf_gpu('L', nx, sub11, ld, &info);
  147. if (ret != MAGMA_SUCCESS) {
  148. fprintf(stderr, "Error in Magma: %d\n", ret);
  149. STARPU_ABORT();
  150. }
  151. cudaError_t cures = cudaThreadSynchronize();
  152. STARPU_ASSERT(!cures);
  153. }
  154. #else
  155. for (z = 0; z < nx; z++)
  156. {
  157. float lambda11;
  158. cudaMemcpyAsync(&lambda11, &sub11[z+z*ld], sizeof(float), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  159. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  160. STARPU_ASSERT(lambda11 != 0.0f);
  161. lambda11 = sqrt(lambda11);
  162. cublasSetVector(1, sizeof(float), &lambda11, sizeof(float), &sub11[z+z*ld], sizeof(float));
  163. cublasSscal(nx - z - 1, 1.0f/lambda11, &sub11[(z+1)+z*ld], 1);
  164. cublasSsyr('U', nx - z - 1, -1.0f,
  165. &sub11[(z+1)+z*ld], 1,
  166. &sub11[(z+1)+(z+1)*ld], ld);
  167. }
  168. cudaThreadSynchronize();
  169. #endif
  170. break;
  171. #endif
  172. default:
  173. STARPU_ABORT();
  174. break;
  175. }
  176. }
  177. void chol_cpu_codelet_update_u11(float *mat, unsigned nx, unsigned ld)
  178. __attribute__ ((task_implementation ("cpu", chol_codelet_update_u11)));
  179. #ifdef STARPU_USE_CUDA
  180. void chol_cublas_codelet_update_u11(float *mat, unsigned nx, unsigned ld)
  181. __attribute__ ((task_implementation ("cuda", chol_codelet_update_u11)));
  182. #endif
  183. void chol_cpu_codelet_update_u11(float *mat, unsigned nx, unsigned ld)
  184. {
  185. chol_common_codelet_update_u11(mat, nx, ld, 0);
  186. }
  187. #ifdef STARPU_USE_CUDA
  188. void chol_cublas_codelet_update_u11(float *mat, unsigned nx, unsigned ld)
  189. {
  190. chol_common_codelet_update_u11(mat, nx, ld, 1);
  191. }
  192. #endif// STARPU_USE_CUDA