strassen2_kernels.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <stdio.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <math.h>
  20. #include <sys/types.h>
  21. #include <pthread.h>
  22. #include <signal.h>
  23. #include <semaphore.h>
  24. #include <starpu_config.h>
  25. #ifdef USE_CUDA
  26. #include <cublas.h>
  27. #endif
  28. #include "../common/blas.h"
  29. #include <starpu.h>
  30. static double cublas_flop = 0.0;
  31. static double cpus_flop = 0.0;
  32. void display_perf(double timing, unsigned size)
  33. {
  34. double total_flop_n3 = (2.0*size*size*size);
  35. double total_flop = cublas_flop + cpus_flop;
  36. fprintf(stderr, "Computation took (ms):\n");
  37. printf("%2.2f\n", timing/1000);
  38. fprintf(stderr, " GFlop : O(n3) -> %2.2f\n",
  39. (double)total_flop_n3/1000000000.0f);
  40. fprintf(stderr, " GFlop : real %2.2f\n",
  41. (double)total_flop/1000000000.0f);
  42. fprintf(stderr, " CPU : %2.2f (%2.2f%%)\n", (double)cpus_flop/1000000000.0, (100.0*cpus_flop)/(cpus_flop + cublas_flop));
  43. fprintf(stderr, " GPU : %2.2f (%2.2f%%)\n", (double)cublas_flop/1000000000.0, (100.0*cublas_flop)/(cpus_flop + cublas_flop));
  44. fprintf(stderr, " GFlop/s : %2.2f\n", (double)total_flop / (double)timing/1000);
  45. }
  46. static void mult_common_codelet(void *descr[], int s, __attribute__((unused)) void *arg)
  47. {
  48. float *center = (float *)GET_BLAS_PTR(descr[0]);
  49. float *left = (float *)GET_BLAS_PTR(descr[1]);
  50. float *right = (float *)GET_BLAS_PTR(descr[2]);
  51. unsigned n = GET_BLAS_NX(descr[0]);
  52. unsigned ld21 = GET_BLAS_LD(descr[1]);
  53. unsigned ld12 = GET_BLAS_LD(descr[2]);
  54. unsigned ld22 = GET_BLAS_LD(descr[0]);
  55. double flop = 2.0*n*n*n;
  56. #ifdef USE_CUDA
  57. cublasStatus cublasres;
  58. #endif
  59. switch (s) {
  60. case 0:
  61. cpus_flop += flop;
  62. SGEMM("N", "N", n, n, n, 1.0f, right, ld21, left, ld12, 0.0f, center, ld22);
  63. break;
  64. #ifdef USE_CUDA
  65. case 1:
  66. cublas_flop += flop;
  67. cublasSgemm('n', 'n', n, n, n, 1.0f, right, ld12, left, ld21, 0.0f, center, ld22);
  68. cublasres = cublasGetError();
  69. if (STARPU_UNLIKELY(cublasres))
  70. CUBLAS_REPORT_ERROR(cublasres);
  71. break;
  72. #endif
  73. default:
  74. STARPU_ABORT();
  75. break;
  76. }
  77. }
  78. void mult_core_codelet(void *descr[], void *_args)
  79. {
  80. mult_common_codelet(descr, 0, _args);
  81. }
  82. #ifdef USE_CUDA
  83. void mult_cublas_codelet(void *descr[], void *_args)
  84. {
  85. mult_common_codelet(descr, 1, _args);
  86. }
  87. #endif
  88. static void add_sub_common_codelet(void *descr[], int s, __attribute__((unused)) void *arg, float alpha)
  89. {
  90. /* C = A op B */
  91. float *C = (float *)GET_BLAS_PTR(descr[0]);
  92. float *A = (float *)GET_BLAS_PTR(descr[1]);
  93. float *B = (float *)GET_BLAS_PTR(descr[2]);
  94. unsigned n = GET_BLAS_NX(descr[0]);
  95. unsigned ldA = GET_BLAS_LD(descr[1]);
  96. unsigned ldB = GET_BLAS_LD(descr[2]);
  97. unsigned ldC = GET_BLAS_LD(descr[0]);
  98. double flop = 2.0*n*n;
  99. // TODO check dim ...
  100. unsigned line;
  101. #ifdef USE_CUDA
  102. cublasStatus cublasres;
  103. #endif
  104. switch (s) {
  105. case 0:
  106. cpus_flop += flop;
  107. for (line = 0; line < n; line++)
  108. {
  109. /* copy line A into C */
  110. SAXPY(n, 1.0f, &A[line*ldA], 1, &C[line*ldC], 1);
  111. /* add line B to C = A */
  112. SAXPY(n, alpha, &B[line*ldB], 1, &C[line*ldC], 1);
  113. }
  114. break;
  115. #ifdef USE_CUDA
  116. case 1:
  117. cublas_flop += flop;
  118. for (line = 0; line < n; line++)
  119. {
  120. /* copy line A into C */
  121. cublasSaxpy(n, 1.0f, &A[line*ldA], 1, &C[line*ldC], 1);
  122. cublasres = cublasGetError();
  123. if (STARPU_UNLIKELY(cublasres))
  124. CUBLAS_REPORT_ERROR(cublasres);
  125. /* add line B to C = A */
  126. cublasSaxpy(n, alpha, &B[line*ldB], 1, &C[line*ldC], 1);
  127. cublasres = cublasGetError();
  128. if (STARPU_UNLIKELY(cublasres))
  129. CUBLAS_REPORT_ERROR(cublasres);
  130. }
  131. break;
  132. #endif
  133. default:
  134. STARPU_ABORT();
  135. break;
  136. }
  137. }
  138. void sub_core_codelet(void *descr[], __attribute__((unused)) void *arg)
  139. {
  140. add_sub_common_codelet(descr, 0, arg, -1.0f);
  141. }
  142. void add_core_codelet(void *descr[], __attribute__((unused)) void *arg)
  143. {
  144. add_sub_common_codelet(descr, 0, arg, 1.0f);
  145. }
  146. #ifdef USE_CUDA
  147. void sub_cublas_codelet(void *descr[], __attribute__((unused)) void *arg)
  148. {
  149. add_sub_common_codelet(descr, 1, arg, -1.0f);
  150. }
  151. void add_cublas_codelet(void *descr[], __attribute__((unused)) void *arg)
  152. {
  153. add_sub_common_codelet(descr, 1, arg, 1.0f);
  154. }
  155. #endif
  156. static void self_add_sub_common_codelet(void *descr[], int s, __attribute__((unused)) void *arg, float alpha)
  157. {
  158. /* C +=/-= A */
  159. float *C = (float *)GET_BLAS_PTR(descr[0]);
  160. float *A = (float *)GET_BLAS_PTR(descr[1]);
  161. unsigned n = GET_BLAS_NX(descr[0]);
  162. unsigned ldA = GET_BLAS_LD(descr[1]);
  163. unsigned ldC = GET_BLAS_LD(descr[0]);
  164. double flop = 1.0*n*n;
  165. // TODO check dim ...
  166. unsigned line;
  167. #ifdef USE_CUDA
  168. cublasStatus cublasres;
  169. #endif
  170. switch (s) {
  171. case 0:
  172. cpus_flop += flop;
  173. for (line = 0; line < n; line++)
  174. {
  175. /* add line A to C */
  176. SAXPY(n, alpha, &A[line*ldA], 1, &C[line*ldC], 1);
  177. }
  178. break;
  179. #ifdef USE_CUDA
  180. case 1:
  181. cublas_flop += flop;
  182. for (line = 0; line < n; line++)
  183. {
  184. /* add line A to C */
  185. cublasSaxpy(n, alpha, &A[line*ldA], 1, &C[line*ldC], 1);
  186. cublasres = cublasGetError();
  187. if (STARPU_UNLIKELY(cublasres))
  188. CUBLAS_REPORT_ERROR(cublasres);
  189. }
  190. break;
  191. #endif
  192. default:
  193. STARPU_ABORT();
  194. break;
  195. }
  196. }
  197. void self_add_core_codelet(void *descr[], __attribute__((unused)) void *arg)
  198. {
  199. self_add_sub_common_codelet(descr, 0, arg, 1.0f);
  200. }
  201. void self_sub_core_codelet(void *descr[], __attribute__((unused)) void *arg)
  202. {
  203. self_add_sub_common_codelet(descr, 0, arg, -1.0f);
  204. }
  205. #ifdef USE_CUDA
  206. void self_add_cublas_codelet(void *descr[], __attribute__((unused)) void *arg)
  207. {
  208. self_add_sub_common_codelet(descr, 1, arg, 1.0f);
  209. }
  210. void self_sub_cublas_codelet(void *descr[], __attribute__((unused)) void *arg)
  211. {
  212. self_add_sub_common_codelet(descr, 1, arg, -1.0f);
  213. }
  214. #endif
  215. /* this codelet does nothing */
  216. void null_codelet(__attribute__((unused)) void *descr[],
  217. __attribute__((unused)) void *arg)
  218. {
  219. }