multiformat_data_release.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include <starpu.h>
  2. #define NX 16
  3. static int vector[NX];
  4. static starpu_data_handle_t handle;
  5. #define ENTER() do { fprintf(stderr, "Entering %s\n", __func__); } while (0)
  6. /* Counting the calls to the codelets */
  7. struct stats {
  8. unsigned int cpu;
  9. #ifdef STARPU_USE_CUDA
  10. unsigned int cuda;
  11. unsigned int cpu_to_cuda;
  12. unsigned int cuda_to_cpu;
  13. #endif
  14. #ifdef STARPU_USE_OPENCL
  15. unsigned int opencl;
  16. unsigned int cpu_to_opencl;
  17. unsigned int opencl_to_cpu;
  18. #endif
  19. };
  20. static struct stats global_stats;
  21. /* "Fake" conversion codelets */
  22. #ifdef STARPU_USE_CUDA
  23. static void cpu_to_cuda_func(void *buffers[], void *args)
  24. {
  25. ENTER();
  26. global_stats.cpu_to_cuda++;
  27. }
  28. static void cuda_to_cpu_func(void *buffers[], void *args)
  29. {
  30. ENTER();
  31. global_stats.cuda_to_cpu++;
  32. }
  33. static struct starpu_codelet cpu_to_cuda_cl = {
  34. .where = STARPU_CUDA,
  35. .cuda_func = cpu_to_cuda_func,
  36. .nbuffers = 1
  37. };
  38. static struct starpu_codelet cuda_to_cpu_cl = {
  39. .where = STARPU_CPU,
  40. .cpu_func = cuda_to_cpu_func,
  41. .nbuffers = 1
  42. };
  43. #endif /* !STARPU_USE_CUDA */
  44. #ifdef STARPU_USE_OPENCL
  45. static void cpu_to_opencl_func(void *buffers[], void *args)
  46. {
  47. ENTER();
  48. global_stats.cpu_to_opencl++;
  49. }
  50. static void opencl_to_cpu_func(void *buffers[], void *args)
  51. {
  52. ENTER();
  53. global_stats.opencl_to_cpu++;
  54. }
  55. static struct starpu_codelet cpu_to_opencl_cl = {
  56. .where = STARPU_OPENCL,
  57. .opencl_func = cpu_to_opencl_func,
  58. .nbuffers = 1
  59. };
  60. static struct starpu_codelet opencl_to_cpu_cl = {
  61. .where = STARPU_CPU,
  62. .cpu_func = opencl_to_cpu_func,
  63. .nbuffers = 1
  64. };
  65. #endif /* !STARPU_USE_OPENCL */
  66. static struct starpu_multiformat_data_interface_ops ops = {
  67. #ifdef STARPU_USE_CUDA
  68. .cuda_elemsize = sizeof(int),
  69. .cpu_to_cuda_cl = &cpu_to_cuda_cl,
  70. .cuda_to_cpu_cl = &cuda_to_cpu_cl,
  71. #endif
  72. #ifdef STARPU_USE_OPENCL
  73. .opencl_elemsize = sizeof(int),
  74. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  75. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  76. #endif
  77. .cpu_elemsize = sizeof(int)
  78. };
  79. static void
  80. register_handle(void)
  81. {
  82. int i;
  83. for (i = 0; i < NX; i++)
  84. vector[i] = i;
  85. starpu_multiformat_data_register(&handle, 0, vector, NX, &ops);
  86. }
  87. static void
  88. unregister_handle(void)
  89. {
  90. starpu_data_unregister(handle);
  91. }
  92. #ifdef STARPU_USE_CUDA
  93. static void cuda_func(void *buffers[], void *args)
  94. {
  95. ENTER();
  96. global_stats.cuda++;
  97. }
  98. #endif /* !STARPU_USE_CUDA */
  99. #ifdef STARPU_USE_OPENCL
  100. static void opencl_func(void *buffers[], void *args)
  101. {
  102. ENTER();
  103. global_stats.opencl++;
  104. }
  105. #endif /* !STARPU_USE_OPENCL */
  106. static void
  107. create_and_submit(int where)
  108. {
  109. static struct starpu_codelet cl = {
  110. #ifdef STARPU_USE_CUDA
  111. .cuda_func = cuda_func,
  112. #endif
  113. #if STARPU_USE_OPENCL
  114. .opencl_func = opencl_func,
  115. #endif
  116. .nbuffers = 1
  117. };
  118. cl.where = where;
  119. struct starpu_task *task = starpu_task_create();
  120. task->cl = &cl;
  121. task->buffers[0].handle = handle;
  122. task->buffers[0].mode = STARPU_RW;
  123. /* We need to be sure the data has been copied to the GPU at the end
  124. * of this function */
  125. task->synchronous = 1;
  126. starpu_task_submit(task);
  127. }
  128. static void
  129. print_stats(struct stats *s)
  130. {
  131. fprintf(stderr, "cpu : %d\n", s->cpu);
  132. #ifdef STARPU_USE_CUDA
  133. fprintf(stderr, "cuda : %d\n"
  134. "cpu->cuda : %d\n"
  135. "cuda->cpu : %d\n",
  136. s->cuda,
  137. s->cpu_to_cuda,
  138. s->cuda_to_cpu);
  139. #endif
  140. #ifdef STARPU_USE_OPENCL
  141. fprintf(stderr, "opencl : %d\n"
  142. "cpu->opencl : %d\n"
  143. "opencl->cpu : %d\n",
  144. s->opencl,
  145. s->cpu_to_opencl,
  146. s->opencl_to_cpu);
  147. #endif
  148. }
  149. static int
  150. compare(struct stats *s1, struct stats *s2)
  151. {
  152. if (
  153. #ifdef STARPU_USE_CPU
  154. s1->cpu == s2->cpu &&
  155. #endif
  156. #ifdef STARPU_USE_CUDA
  157. s1->cuda == s2->cuda &&
  158. s1->cpu_to_cuda == s2->cpu_to_cuda &&
  159. s1->cuda_to_cpu == s2->cuda_to_cpu &&
  160. #endif
  161. #ifdef STARPU_USE_OPENCL
  162. s1->opencl == s2->opencl &&
  163. s1->cpu_to_opencl == s2->cpu_to_opencl &&
  164. s1->opencl_to_cpu == s2->opencl_to_cpu &&
  165. #endif
  166. 1 /* Just so the build does not fail if we disable EVERYTHING */
  167. )
  168. return 0;
  169. else
  170. return 1;
  171. }
  172. static int
  173. test(void)
  174. {
  175. struct stats expected_stats;
  176. memset(&expected_stats, 0, sizeof(expected_stats));
  177. #ifdef STARPU_USE_CUDA
  178. create_and_submit(STARPU_CUDA);
  179. starpu_data_acquire(handle, STARPU_RW);
  180. expected_stats.cuda = 1;
  181. expected_stats.cpu_to_cuda = 1;
  182. expected_stats.cuda_to_cpu = 1;
  183. starpu_data_release(handle);
  184. if (compare(&global_stats, &expected_stats) != 0)
  185. {
  186. fprintf(stderr, "CUDA failed\n");
  187. print_stats(&global_stats);
  188. fprintf(stderr ,"\n");
  189. print_stats(&expected_stats);
  190. return 1;
  191. }
  192. #endif /* !STARPU_USE_CUDA */
  193. #ifdef STARPU_USE_OPENCL
  194. create_and_submit(STARPU_OPENCL);
  195. starpu_data_acquire(handle, STARPU_RW);
  196. expected_stats.opencl = 1;
  197. expected_stats.cpu_to_opencl = 1;
  198. expected_stats.opencl_to_cpu = 1;
  199. starpu_data_release(handle);
  200. if (compare(&global_stats, &expected_stats) != 0)
  201. {
  202. fprintf(stderr, "OPENCL failed\n");
  203. print_stats(&global_stats);
  204. fprintf(stderr ,"\n");
  205. print_stats(&expected_stats);
  206. return 1;
  207. }
  208. #endif /* !STARPU_USE_OPENCL */
  209. return 0;
  210. }
  211. int
  212. main(void)
  213. {
  214. #ifdef STARPU_USE_CPU
  215. struct starpu_conf conf = {
  216. .ncpus = -1,
  217. .ncuda = 1,
  218. .nopencl = 1
  219. };
  220. memset(&global_stats, 0, sizeof(global_stats));
  221. starpu_init(&conf);
  222. register_handle();
  223. int err = test();
  224. unregister_handle();
  225. starpu_shutdown();
  226. return err?EXIT_FAILURE:EXIT_SUCCESS;
  227. #else /* ! STARPU_USE_CPU */
  228. /* Without the CPU, there is no point in using the multiformat
  229. * interface, so this test is pointless. */
  230. return EXIT_SUCCESS;
  231. #endif
  232. }