multiformat_data_release.c 6.1 KB

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