pipeline.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2015,2017,2019 CNRS
  4. * Copyright (C) 2012,2014-2017 Université de Bordeaux
  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. /*
  18. * This examples shows how to submit a pipeline to StarPU with limited buffer
  19. * use, and avoiding submitted all the tasks at once.
  20. *
  21. * This is a dumb example pipeline, depicted here:
  22. *
  23. * x--\
  24. * >==axpy-->sum
  25. * y--/
  26. *
  27. * x and y produce vectors full of x and y values, axpy multiplies them, and sum
  28. * sums it up. We thus have 3 temporary buffers
  29. */
  30. #include <starpu.h>
  31. #include <stdint.h>
  32. #include <semaphore.h>
  33. #include <common/blas.h>
  34. #ifdef STARPU_USE_CUDA
  35. #include <starpu_cublas_v2.h>
  36. #endif
  37. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  38. /* Vector size */
  39. #ifdef STARPU_QUICK_CHECK
  40. #define N 16
  41. #else
  42. #define N 1048576
  43. #endif
  44. /* Number of iteration buffers, and thus overlapped pipeline iterations */
  45. #define K 16
  46. /* Number of concurrently submitted pipeline iterations */
  47. #define C 64
  48. /* Number of iterations */
  49. #define L 256
  50. /* X / Y codelets */
  51. void pipeline_cpu_x(void *descr[], void *args)
  52. {
  53. float x;
  54. float *val = (float *) STARPU_VECTOR_GET_PTR(descr[0]);
  55. int n = STARPU_VECTOR_GET_NX(descr[0]);
  56. int i;
  57. starpu_codelet_unpack_args(args, &x);
  58. for (i = 0; i < n ; i++)
  59. val[i] = x;
  60. }
  61. static struct starpu_perfmodel pipeline_model_x =
  62. {
  63. .type = STARPU_HISTORY_BASED,
  64. .symbol = "pipeline_model_x"
  65. };
  66. static struct starpu_codelet pipeline_codelet_x =
  67. {
  68. .cpu_funcs = {pipeline_cpu_x},
  69. .cpu_funcs_name = {"pipeline_cpu_x"},
  70. .nbuffers = 1,
  71. .modes = {STARPU_W},
  72. .model = &pipeline_model_x
  73. };
  74. /* axpy codelets */
  75. void pipeline_cpu_axpy(void *descr[], void *arg)
  76. {
  77. (void)arg;
  78. float *x = (float *) STARPU_VECTOR_GET_PTR(descr[0]);
  79. float *y = (float *) STARPU_VECTOR_GET_PTR(descr[1]);
  80. int n = STARPU_VECTOR_GET_NX(descr[0]);
  81. STARPU_SAXPY(n, 1., x, 1, y, 1);
  82. }
  83. #ifdef STARPU_USE_CUDA
  84. void pipeline_cublas_axpy(void *descr[], void *arg)
  85. {
  86. (void)arg;
  87. float *x = (float *) STARPU_VECTOR_GET_PTR(descr[0]);
  88. float *y = (float *) STARPU_VECTOR_GET_PTR(descr[1]);
  89. int n = STARPU_VECTOR_GET_NX(descr[0]);
  90. float alpha = 1.;
  91. cublasStatus_t status = cublasSaxpy(starpu_cublas_get_local_handle(), n, &alpha, x, 1, y, 1);
  92. if (status != CUBLAS_STATUS_SUCCESS)
  93. STARPU_CUBLAS_REPORT_ERROR(status);
  94. }
  95. #endif
  96. static struct starpu_perfmodel pipeline_model_axpy =
  97. {
  98. .type = STARPU_HISTORY_BASED,
  99. .symbol = "pipeline_model_axpy"
  100. };
  101. static struct starpu_codelet pipeline_codelet_axpy =
  102. {
  103. .cpu_funcs = {pipeline_cpu_axpy},
  104. .cpu_funcs_name = {"pipeline_cpu_axpy"},
  105. #ifdef STARPU_USE_CUDA
  106. .cuda_funcs = {pipeline_cublas_axpy},
  107. .cuda_flags = {STARPU_CUDA_ASYNC},
  108. #endif
  109. .nbuffers = 2,
  110. .modes = {STARPU_R, STARPU_RW},
  111. .model = &pipeline_model_axpy
  112. };
  113. /* sum codelet */
  114. void pipeline_cpu_sum(void *descr[], void *arg)
  115. {
  116. (void)arg;
  117. float *x = (float *) STARPU_VECTOR_GET_PTR(descr[0]);
  118. int n = STARPU_VECTOR_GET_NX(descr[0]);
  119. float y;
  120. y = STARPU_SASUM(n, x, 1);
  121. FPRINTF(stderr,"CPU finished with %f\n", y);
  122. }
  123. #ifdef STARPU_USE_CUDA
  124. void pipeline_cublas_sum(void *descr[], void *arg)
  125. {
  126. (void)arg;
  127. float *x = (float *) STARPU_VECTOR_GET_PTR(descr[0]);
  128. int n = STARPU_VECTOR_GET_NX(descr[0]);
  129. float y;
  130. cublasStatus_t status = cublasSasum(starpu_cublas_get_local_handle(), n, x, 1, &y);
  131. if (status != CUBLAS_STATUS_SUCCESS)
  132. STARPU_CUBLAS_REPORT_ERROR(status);
  133. FPRINTF(stderr,"CUBLAS finished with %f\n", y);
  134. }
  135. #endif
  136. static struct starpu_perfmodel pipeline_model_sum =
  137. {
  138. .type = STARPU_HISTORY_BASED,
  139. .symbol = "pipeline_model_sum"
  140. };
  141. static struct starpu_codelet pipeline_codelet_sum =
  142. {
  143. .cpu_funcs = {pipeline_cpu_sum},
  144. .cpu_funcs_name = {"pipeline_cpu_sum"},
  145. #ifdef STARPU_USE_CUDA
  146. .cuda_funcs = {pipeline_cublas_sum},
  147. .cuda_flags = {STARPU_CUDA_ASYNC},
  148. #endif
  149. .nbuffers = 1,
  150. .modes = {STARPU_R},
  151. .model = &pipeline_model_sum
  152. };
  153. int main(void)
  154. {
  155. int ret = 0;
  156. int k, l, c;
  157. starpu_data_handle_t buffersX[K], buffersY[K], buffersP[K];
  158. sem_t sems[C];
  159. ret = starpu_init(NULL);
  160. if (ret == -ENODEV)
  161. exit(77);
  162. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  163. starpu_cublas_init();
  164. /* Initialize the K temporary buffers. No need to allocate it ourselves
  165. * Since it's the X and Y kernels which will fill the initial values. */
  166. for (k = 0; k < K; k++)
  167. {
  168. starpu_vector_data_register(&buffersX[k], -1, 0, N, sizeof(float));
  169. starpu_vector_data_register(&buffersY[k], -1, 0, N, sizeof(float));
  170. starpu_vector_data_register(&buffersP[k], -1, 0, N, sizeof(float));
  171. }
  172. /* Initialize way to wait for the C previous concurrent stages */
  173. for (c = 0; c < C; c++)
  174. sem_init(&sems[c], 0, 0);
  175. /* Submits the l pipeline stages */
  176. for (l = 0; l < L; l++)
  177. {
  178. float x = l;
  179. float y = 2*l;
  180. /* First wait for the C previous concurrent stages */
  181. if (l >= C)
  182. {
  183. starpu_do_schedule();
  184. sem_wait(&sems[l%C]);
  185. }
  186. /* Now submit the next stage */
  187. ret = starpu_task_insert(&pipeline_codelet_x,
  188. STARPU_W, buffersX[l%K],
  189. STARPU_VALUE, &x, sizeof(x),
  190. STARPU_TAG_ONLY, (starpu_tag_t) (100*l),
  191. 0);
  192. if (ret == -ENODEV) goto enodev;
  193. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert x");
  194. ret = starpu_task_insert(&pipeline_codelet_x,
  195. STARPU_W, buffersY[l%K],
  196. STARPU_VALUE, &y, sizeof(y),
  197. STARPU_TAG_ONLY, (starpu_tag_t) (100*l+1),
  198. 0);
  199. if (ret == -ENODEV) goto enodev;
  200. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert y");
  201. ret = starpu_task_insert(&pipeline_codelet_axpy,
  202. STARPU_R, buffersX[l%K],
  203. STARPU_RW, buffersY[l%K],
  204. STARPU_TAG_ONLY, (starpu_tag_t) l,
  205. 0);
  206. if (ret == -ENODEV) goto enodev;
  207. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert axpy");
  208. ret = starpu_task_insert(&pipeline_codelet_sum,
  209. STARPU_R, buffersY[l%K],
  210. STARPU_CALLBACK_WITH_ARG_NFREE, (void (*)(void*))sem_post, &sems[l%C],
  211. STARPU_TAG_ONLY, (starpu_tag_t) l,
  212. 0);
  213. if (ret == -ENODEV) goto enodev;
  214. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert sum");
  215. }
  216. starpu_task_wait_for_all();
  217. enodev:
  218. for (k = 0; k < K; k++)
  219. {
  220. starpu_data_unregister(buffersX[k]);
  221. starpu_data_unregister(buffersY[k]);
  222. starpu_data_unregister(buffersP[k]);
  223. }
  224. starpu_shutdown();
  225. return (ret == -ENODEV ? 77 : 0);
  226. }