cholesky_kernels.c 5.5 KB

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