multiformat_handle_conversion.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2012 INRIA
  4. * Copyright (C) 2011, 2012 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 4
  20. #define DEBUG 0
  21. #if DEBUG
  22. #define SYNCHRONOUS 1 /* Easier to debug with synchronous tasks */
  23. #define ENTER() do { FPRINTF(stderr, "Entering %s\n", __func__); } while (0)
  24. #else
  25. #define SYNCHRONOUS 0
  26. #define ENTER()
  27. #endif
  28. /* Counting the calls to the codelets */
  29. struct stats
  30. {
  31. unsigned int cpu;
  32. #ifdef STARPU_USE_CUDA
  33. unsigned int cuda;
  34. unsigned int cpu_to_cuda;
  35. unsigned int cuda_to_cpu;
  36. #endif
  37. #ifdef STARPU_USE_OPENCL
  38. unsigned int opencl;
  39. unsigned int cpu_to_opencl;
  40. unsigned int opencl_to_cpu;
  41. #endif
  42. };
  43. struct stats global_stats;
  44. /* "Fake" conversion codelets */
  45. #ifdef STARPU_USE_CUDA
  46. static void cpu_to_cuda_func(void *buffers[], void *args)
  47. {
  48. ENTER();
  49. global_stats.cpu_to_cuda++;
  50. }
  51. static void cuda_to_cpu_func(void *buffers[], void *args)
  52. {
  53. ENTER();
  54. global_stats.cuda_to_cpu++;
  55. }
  56. struct starpu_codelet cpu_to_cuda_cl =
  57. {
  58. .where = STARPU_CUDA,
  59. .cuda_funcs = {cpu_to_cuda_func, NULL},
  60. .nbuffers = 1
  61. };
  62. struct starpu_codelet cuda_to_cpu_cl =
  63. {
  64. .where = STARPU_CPU,
  65. .cpu_funcs = {cuda_to_cpu_func, NULL},
  66. .nbuffers = 1
  67. };
  68. #endif /* !STARPU_USE_CUDA */
  69. #ifdef STARPU_USE_OPENCL
  70. static void cpu_to_opencl_func(void *buffers[], void *args)
  71. {
  72. ENTER();
  73. global_stats.cpu_to_opencl++;
  74. }
  75. static void opencl_to_cpu_func(void *buffers[], void *args)
  76. {
  77. ENTER();
  78. global_stats.opencl_to_cpu++;
  79. }
  80. struct starpu_codelet cpu_to_opencl_cl =
  81. {
  82. .where = STARPU_OPENCL,
  83. .opencl_funcs = {cpu_to_opencl_func, NULL},
  84. .nbuffers = 1
  85. };
  86. struct starpu_codelet opencl_to_cpu_cl =
  87. {
  88. .where = STARPU_CPU,
  89. .cpu_funcs = {opencl_to_cpu_func, NULL},
  90. .nbuffers = 1
  91. };
  92. #endif /* !STARPU_USE_OPENCL */
  93. static struct starpu_multiformat_data_interface_ops ops =
  94. {
  95. #ifdef STARPU_USE_CUDA
  96. .cuda_elemsize = sizeof(int),
  97. .cpu_to_cuda_cl = &cpu_to_cuda_cl,
  98. .cuda_to_cpu_cl = &cuda_to_cpu_cl,
  99. #endif
  100. #ifdef STARPU_USE_OPENCL
  101. .opencl_elemsize = sizeof(int),
  102. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  103. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  104. #endif
  105. .cpu_elemsize = sizeof(int)
  106. };
  107. static void cpu_func(void *buffers[], void *args)
  108. {
  109. ENTER();
  110. global_stats.cpu++;
  111. }
  112. #ifdef STARPU_USE_CUDA
  113. static void cuda_func(void *buffers[], void *args)
  114. {
  115. ENTER();
  116. global_stats.cuda++;
  117. }
  118. #endif /* !STARPU_USE_CUDA */
  119. #ifdef STARPU_USE_OPENCL
  120. static void opencl_func(void *buffers[], void *args)
  121. {
  122. ENTER();
  123. global_stats.opencl++;
  124. }
  125. #endif /* !STARPU_USE_OPENCL */
  126. static void
  127. create_and_submit_tasks(int where, starpu_data_handle_t handles[])
  128. {
  129. FPRINTF(stderr, "***** Starting Task 1\n");
  130. struct starpu_codelet cl =
  131. {
  132. .modes = { STARPU_RW },
  133. .nbuffers = 1,
  134. .where = where
  135. };
  136. #ifdef STARPU_USE_CUDA
  137. if (where & STARPU_CUDA)
  138. cl.cuda_funcs[0] = cuda_func;
  139. #endif
  140. #ifdef STARPU_USE_OPENCL
  141. if (where & STARPU_OPENCL)
  142. cl.opencl_funcs[0] = opencl_func;
  143. #endif
  144. struct starpu_task *task = starpu_task_create();
  145. task->synchronous = SYNCHRONOUS;
  146. task->cl = &cl;
  147. task->handles[0] = handles[0];
  148. assert(starpu_task_submit(task) == 0);
  149. FPRINTF(stderr, "***** Starting Task 2\n");
  150. struct starpu_codelet cl2 =
  151. {
  152. .modes = { STARPU_RW },
  153. .cpu_funcs = {cpu_func, NULL},
  154. .nbuffers = 1,
  155. .where = where
  156. };
  157. struct starpu_task *task2 = starpu_task_create();
  158. task2->synchronous = SYNCHRONOUS;
  159. task2->cl = &cl2;
  160. task2->handles[0] = handles[1];
  161. assert(starpu_task_submit(task2) == 0);
  162. FPRINTF(stderr, "***** Starting Task 3\n");
  163. struct starpu_codelet cl3 =
  164. {
  165. .modes = { STARPU_RW, STARPU_RW },
  166. .nbuffers = 2,
  167. .where = where
  168. };
  169. #ifdef STARPU_USE_CUDA
  170. if (where & STARPU_CUDA)
  171. cl3.cuda_funcs[0] = cuda_func;
  172. #endif
  173. #ifdef STARPU_USE_OPENCL
  174. if (where & STARPU_OPENCL)
  175. cl3.opencl_funcs[0] = opencl_func;
  176. #endif
  177. struct starpu_task *task3 = starpu_task_create();
  178. task3->synchronous = SYNCHRONOUS;
  179. task3->cl = &cl3;
  180. task3->handles[0] = handles[0];
  181. task3->handles[1] = handles[1];
  182. assert(starpu_task_submit(task3) == 0);
  183. assert(starpu_task_wait_for_all() == 0);
  184. FPRINTF(stderr, "***** End of all tasks\n");
  185. return;
  186. }
  187. #if DEBUG
  188. static void
  189. print_stats(struct stats *s)
  190. {
  191. FPRINTF(stderr, "cpu : %d\n", s->cpu);
  192. #ifdef STARPU_USE_CUDA
  193. FPRINTF(stderr, "cuda : %d\n"
  194. "cpu->cuda : %d\n"
  195. "cuda->cpu : %d\n",
  196. s->cuda,
  197. s->cpu_to_cuda,
  198. s->cuda_to_cpu);
  199. #endif
  200. #ifdef STARPU_USE_OPENCL
  201. FPRINTF(stderr, "opencl : %d\n"
  202. "cpu->opencl : %d\n"
  203. "opencl->cpu : %d\n",
  204. s->opencl,
  205. s->cpu_to_opencl,
  206. s->opencl_to_cpu);
  207. #endif
  208. }
  209. #endif /* !DEBUG */
  210. /* XXX Just a little bit of copy/pasta here... */
  211. #ifdef STARPU_USE_CUDA
  212. static int
  213. test_cuda(void)
  214. {
  215. int i;
  216. int vector1[NX];
  217. int vector2[NX];
  218. starpu_data_handle_t handles[2];
  219. for (i = 0; i < NX; i++)
  220. {
  221. vector1[i] = i;
  222. vector2[i] = i;
  223. }
  224. starpu_multiformat_data_register(handles, 0, vector1, NX, &ops);
  225. starpu_multiformat_data_register(handles+1, 0, vector2, NX, &ops);
  226. memset(&global_stats, 0, sizeof(global_stats));
  227. create_and_submit_tasks(STARPU_CUDA, handles);
  228. starpu_data_unregister(handles[0]);
  229. starpu_data_unregister(handles[1]);
  230. #if DEBUG
  231. print_stats(&global_stats);
  232. #endif
  233. return !(global_stats.cpu == 1 &&
  234. global_stats.cpu_to_cuda == 2 &&
  235. global_stats.cuda_to_cpu == 2 &&
  236. global_stats.cuda == 2);
  237. }
  238. #endif /* !STARPU_USE_CUDA */
  239. #ifdef STARPU_USE_OPENCL
  240. static int
  241. test_opencl(void)
  242. {
  243. int i;
  244. int vector1[NX];
  245. int vector2[NX];
  246. starpu_data_handle_t handles[2];
  247. for (i = 0; i < NX; i++)
  248. {
  249. vector1[i] = i;
  250. vector2[i] = i;
  251. }
  252. starpu_multiformat_data_register(handles, 0, vector1, NX, &ops);
  253. starpu_multiformat_data_register(handles+1, 0, vector2, NX, &ops);
  254. memset(&global_stats, 0, sizeof(global_stats));
  255. create_and_submit_tasks(STARPU_OPENCL, handles);
  256. starpu_data_unregister(handles[0]);
  257. starpu_data_unregister(handles[1]);
  258. #if DEBUG
  259. print_stats(&global_stats);
  260. #endif
  261. return !(global_stats.cpu == 1 &&
  262. global_stats.cpu_to_opencl == 2 &&
  263. global_stats.opencl_to_cpu == 2 &&
  264. global_stats.opencl == 2);
  265. }
  266. #endif /* !STARPU_USE_OPENCL */
  267. int
  268. main(void)
  269. {
  270. #ifdef STARPU_USE_CPU
  271. int ret;
  272. struct starpu_conf conf =
  273. {
  274. .ncpus = -1,
  275. .ncuda = 2,
  276. .nopencl = 1
  277. };
  278. ret = starpu_init(&conf);
  279. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  280. unsigned int ncuda = starpu_cuda_worker_get_count();
  281. unsigned int nopencl = starpu_opencl_worker_get_count();
  282. #ifdef STARPU_USE_OPENCL
  283. if (nopencl > 0 && test_opencl() != 0)
  284. {
  285. FPRINTF(stderr, "OPENCL FAILED\n");
  286. return EXIT_FAILURE;
  287. }
  288. #endif
  289. #ifdef STARPU_USE_CUDA
  290. if (ncuda > 0 && test_cuda() != 0)
  291. {
  292. FPRINTF(stderr, "CUDA FAILED \n");
  293. return EXIT_FAILURE;
  294. }
  295. #endif
  296. starpu_shutdown();
  297. if (ncuda == 0 && nopencl == 0)
  298. return STARPU_TEST_SKIPPED;
  299. else
  300. return EXIT_SUCCESS;
  301. #else /* !STARPU_USE_CPU */
  302. /* Without the CPU, there is no point in using the multiformat
  303. * interface, so this test is pointless. */
  304. return STARPU_TEST_SKIPPED;
  305. #endif
  306. }