multiformat_data_release.c 6.2 KB

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