cholesky_kernels.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2012 Université de Bordeaux
  4. * Copyright (C) 2010, 2011 CNRS
  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. STARPU_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. if (STARPU_UNLIKELY(st != CUBLAS_STATUS_SUCCESS))
  51. STARPU_CUBLAS_REPORT_ERROR(st);
  52. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  53. break;
  54. #endif
  55. default:
  56. STARPU_ABORT();
  57. break;
  58. }
  59. }
  60. void chol_cpu_codelet_update_u22(const float *left, const float *right, float *center, unsigned dx, unsigned dy, unsigned dz,
  61. unsigned ld21, unsigned ld12, unsigned ld22)
  62. __attribute__ ((task_implementation ("cpu", chol_codelet_update_u22)));
  63. #ifdef STARPU_USE_CUDA
  64. void chol_cublas_codelet_update_u22(const float *left, const float *right, float *center, unsigned dx, unsigned dy, unsigned dz,
  65. unsigned ld21, unsigned ld12, unsigned ld22)
  66. __attribute__ ((task_implementation ("cuda", chol_codelet_update_u22)));
  67. #endif
  68. void chol_cpu_codelet_update_u22(const float *left, const float *right, float *center, unsigned dx, unsigned dy, unsigned dz,
  69. unsigned ld21, unsigned ld12, unsigned ld22)
  70. {
  71. chol_common_cpu_codelet_update_u22(left, right, center, dx, dx, dz, ld21, ld12, ld22, 0);
  72. }
  73. #ifdef STARPU_USE_CUDA
  74. void chol_cublas_codelet_update_u22(const float *left, const float *right, float *center, unsigned dx, unsigned dy, unsigned dz,
  75. unsigned ld21, unsigned ld12, unsigned ld22)
  76. {
  77. chol_common_cpu_codelet_update_u22(left, right, center, dx, dx, dz, ld21, ld12, ld22, 1);
  78. }
  79. #endif// STARPU_USE_CUDA
  80. /*
  81. * U21
  82. */
  83. static inline void chol_common_codelet_update_u21(const float *sub11, float *sub21, unsigned ld11, unsigned ld21, unsigned nx21, unsigned ny21, int s)
  84. {
  85. switch (s) {
  86. case 0:
  87. STARPU_STRSM("R", "L", "T", "N", nx21, ny21, 1.0f, sub11, ld11, sub21, ld21);
  88. break;
  89. #ifdef STARPU_USE_CUDA
  90. case 1:
  91. cublasStrsm('R', 'L', 'T', 'N', nx21, ny21, 1.0f, sub11, ld11, sub21, ld21);
  92. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  93. break;
  94. #endif
  95. default:
  96. STARPU_ABORT();
  97. break;
  98. }
  99. }
  100. void chol_cpu_codelet_update_u21(const float *sub11, float *sub21, unsigned ld11, unsigned ld21, unsigned nx21, unsigned ny21)
  101. __attribute__ ((task_implementation ("cpu", chol_codelet_update_u21)));
  102. #ifdef STARPU_USE_CUDA
  103. void chol_cublas_codelet_update_u21(const float *sub11, float *sub21, unsigned ld11, unsigned ld21, unsigned nx21, unsigned ny21)
  104. __attribute__ ((task_implementation ("cuda", chol_codelet_update_u21)));
  105. #endif
  106. void chol_cpu_codelet_update_u21(const float *sub11, float *sub21, unsigned ld11, unsigned ld21, unsigned nx21, unsigned ny21)
  107. {
  108. chol_common_codelet_update_u21(sub11, sub21, ld11, ld21, nx21, ny21, 0);
  109. }
  110. #ifdef STARPU_USE_CUDA
  111. void chol_cublas_codelet_update_u21(const float *sub11, float *sub21, unsigned ld11, unsigned ld21, unsigned nx21, unsigned ny21)
  112. {
  113. chol_common_codelet_update_u21(sub11, sub21, ld11, ld21, nx21, ny21, 1);
  114. }
  115. #endif
  116. /*
  117. * U11
  118. */
  119. static inline void chol_common_codelet_update_u11(float *sub11, unsigned nx, unsigned ld, int s)
  120. {
  121. unsigned z;
  122. switch (s) {
  123. case 0:
  124. /*
  125. * - alpha 11 <- lambda 11 = sqrt(alpha11)
  126. * - alpha 21 <- l 21 = alpha 21 / lambda 11
  127. * - A22 <- A22 - l21 trans(l21)
  128. */
  129. for (z = 0; z < nx; z++)
  130. {
  131. float lambda11;
  132. lambda11 = sqrt(sub11[z+z*ld]);
  133. sub11[z+z*ld] = lambda11;
  134. STARPU_ASSERT(lambda11 != 0.0f);
  135. STARPU_SSCAL(nx - z - 1, 1.0f/lambda11, &sub11[(z+1)+z*ld], 1);
  136. STARPU_SSYR("L", nx - z - 1, -1.0f,
  137. &sub11[(z+1)+z*ld], 1,
  138. &sub11[(z+1)+(z+1)*ld], ld);
  139. }
  140. break;
  141. #ifdef STARPU_USE_CUDA
  142. case 1:
  143. #ifdef STARPU_HAVE_MAGMA
  144. {
  145. int ret;
  146. int info;
  147. ret = magma_spotrf_gpu('L', nx, sub11, ld, &info);
  148. if (ret != MAGMA_SUCCESS) {
  149. fprintf(stderr, "Error in Magma: %d\n", ret);
  150. STARPU_ABORT();
  151. }
  152. cudaError_t cures = cudaStreamSynchronize(starpu_cuda_get_local_stream());
  153. STARPU_ASSERT(!cures);
  154. }
  155. #else
  156. for (z = 0; z < nx; z++)
  157. {
  158. float lambda11;
  159. cudaMemcpyAsync(&lambda11, &sub11[z+z*ld], sizeof(float), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  160. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  161. STARPU_ASSERT(lambda11 != 0.0f);
  162. lambda11 = sqrt(lambda11);
  163. cublasSetVector(1, sizeof(float), &lambda11, sizeof(float), &sub11[z+z*ld], sizeof(float));
  164. cublasSscal(nx - z - 1, 1.0f/lambda11, &sub11[(z+1)+z*ld], 1);
  165. cublasSsyr('U', nx - z - 1, -1.0f,
  166. &sub11[(z+1)+z*ld], 1,
  167. &sub11[(z+1)+(z+1)*ld], ld);
  168. }
  169. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  170. #endif
  171. break;
  172. #endif
  173. default:
  174. STARPU_ABORT();
  175. break;
  176. }
  177. }
  178. void chol_cpu_codelet_update_u11(float *mat, unsigned nx, unsigned ld)
  179. __attribute__ ((task_implementation ("cpu", chol_codelet_update_u11)));
  180. #ifdef STARPU_USE_CUDA
  181. void chol_cublas_codelet_update_u11(float *mat, unsigned nx, unsigned ld)
  182. __attribute__ ((task_implementation ("cuda", chol_codelet_update_u11)));
  183. #endif
  184. void chol_cpu_codelet_update_u11(float *mat, unsigned nx, unsigned ld)
  185. {
  186. chol_common_codelet_update_u11(mat, nx, ld, 0);
  187. }
  188. #ifdef STARPU_USE_CUDA
  189. void chol_cublas_codelet_update_u11(float *mat, unsigned nx, unsigned ld)
  190. {
  191. chol_common_codelet_update_u11(mat, nx, ld, 1);
  192. }
  193. #endif// STARPU_USE_CUDA