multiformat_handle_conversion.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 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. static struct starpu_codelet cl =
  131. {
  132. .modes = { STARPU_RW },
  133. #ifdef STARPU_USE_CUDA
  134. .cuda_funcs = {cuda_func, NULL},
  135. #endif
  136. #if STARPU_USE_OPENCL
  137. .opencl_funcs = {opencl_func, NULL},
  138. #endif
  139. .nbuffers = 1
  140. };
  141. cl.where = where;
  142. struct starpu_task *task = starpu_task_create();
  143. task->synchronous = SYNCHRONOUS;
  144. task->cl = &cl;
  145. task->handles[0] = handles[0];
  146. starpu_task_submit(task);
  147. FPRINTF(stderr, "***** Starting Task 2\n");
  148. static struct starpu_codelet cl2 =
  149. {
  150. .modes = { STARPU_RW },
  151. .where = STARPU_CPU,
  152. .cpu_funcs = {cpu_func, NULL},
  153. .nbuffers = 1
  154. };
  155. struct starpu_task *task2 = starpu_task_create();
  156. task2->synchronous = SYNCHRONOUS;
  157. task2->cl = &cl2;
  158. task2->handles[0] = handles[1];
  159. starpu_task_submit(task2);
  160. FPRINTF(stderr, "***** Starting Task 3\n");
  161. static struct starpu_codelet cl3 =
  162. {
  163. .modes = { STARPU_RW, STARPU_RW },
  164. .cpu_funcs = {cpu_func, NULL},
  165. #ifdef STARPU_USE_CUDA
  166. .cuda_funcs = {cuda_func, NULL},
  167. #endif
  168. #ifdef STARPU_USE_OPENCL
  169. .opencl_funcs = {opencl_func, NULL},
  170. #endif
  171. .nbuffers = 2
  172. };
  173. cl3.where = where;
  174. struct starpu_task *task3 = starpu_task_create();
  175. task3->synchronous = SYNCHRONOUS;
  176. task3->cl = &cl3;
  177. task3->handles[0] = handles[0];
  178. task3->handles[1] = handles[1];
  179. starpu_task_submit(task3);
  180. starpu_task_wait_for_all();
  181. FPRINTF(stderr, "***** End of all tasks\n");
  182. return;
  183. }
  184. #if DEBUG
  185. static void
  186. print_stats(struct stats *s)
  187. {
  188. FPRINTF(stderr, "cpu : %d\n", s->cpu);
  189. #ifdef STARPU_USE_CUDA
  190. FPRINTF(stderr, "cuda : %d\n"
  191. "cpu->cuda : %d\n"
  192. "cuda->cpu : %d\n",
  193. s->cuda,
  194. s->cpu_to_cuda,
  195. s->cuda_to_cpu);
  196. #endif
  197. #ifdef STARPU_USE_OPENCL
  198. FPRINTF(stderr, "opencl : %d\n"
  199. "cpu->opencl : %d\n"
  200. "opencl->cpu : %d\n",
  201. s->opencl,
  202. s->cpu_to_opencl,
  203. s->opencl_to_cpu);
  204. #endif
  205. }
  206. #endif /* !DEBUG */
  207. /* XXX Just a little bit of copy/pasta here... */
  208. #ifdef STARPU_USE_CUDA
  209. static int
  210. test_cuda(void)
  211. {
  212. int i;
  213. int vector1[NX];
  214. int vector2[NX];
  215. starpu_data_handle_t handles[2];
  216. for (i = 0; i < NX; i++)
  217. {
  218. vector1[i] = i;
  219. vector2[i] = i;
  220. }
  221. starpu_multiformat_data_register(handles, 0, vector1, NX, &ops);
  222. starpu_multiformat_data_register(handles+1, 0, vector2, NX, &ops);
  223. memset(&global_stats, 0, sizeof(global_stats));
  224. create_and_submit_tasks(STARPU_CUDA, handles);
  225. starpu_data_unregister(handles[0]);
  226. starpu_data_unregister(handles[1]);
  227. #if DEBUG
  228. print_stats(&global_stats);
  229. #endif
  230. return !(global_stats.cpu == 1 &&
  231. global_stats.cpu_to_cuda == 2 &&
  232. global_stats.cuda_to_cpu == 2 &&
  233. global_stats.cuda == 2);
  234. }
  235. #endif /* !STARPU_USE_CUDA */
  236. #ifdef STARPU_USE_OPENCL
  237. static int
  238. test_opencl(void)
  239. {
  240. int i;
  241. int vector1[NX];
  242. int vector2[NX];
  243. starpu_data_handle_t handles[2];
  244. for (i = 0; i < NX; i++)
  245. {
  246. vector1[i] = i;
  247. vector2[i] = i;
  248. }
  249. starpu_multiformat_data_register(handles, 0, vector1, NX, &ops);
  250. starpu_multiformat_data_register(handles+1, 0, vector2, NX, &ops);
  251. memset(&global_stats, 0, sizeof(global_stats));
  252. create_and_submit_tasks(STARPU_OPENCL, handles);
  253. starpu_data_unregister(handles[0]);
  254. starpu_data_unregister(handles[1]);
  255. #if DEBUG
  256. print_stats(&global_stats);
  257. #endif
  258. return !(global_stats.cpu == 1 &&
  259. global_stats.cpu_to_opencl == 2 &&
  260. global_stats.opencl_to_cpu == 2 &&
  261. global_stats.opencl == 2);
  262. }
  263. #endif /* !STARPU_USE_OPENCL */
  264. int
  265. main(void)
  266. {
  267. #ifdef STARPU_USE_CPU
  268. int ret;
  269. struct starpu_conf conf =
  270. {
  271. .ncpus = -1,
  272. .ncuda = 2,
  273. .nopencl = 1
  274. };
  275. ret = starpu_init(&conf);
  276. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  277. unsigned int ncuda = starpu_cuda_worker_get_count();
  278. unsigned int nopencl = starpu_opencl_worker_get_count();
  279. #ifdef STARPU_USE_OPENCL
  280. if (nopencl > 0 && test_opencl() != 0)
  281. {
  282. FPRINTF(stderr, "OPENCL FAILED\n");
  283. return EXIT_FAILURE;
  284. }
  285. #endif
  286. #ifdef STARPU_USE_CUDA
  287. if (ncuda > 0 && test_cuda() != 0)
  288. {
  289. FPRINTF(stderr, "CUDA FAILED \n");
  290. return EXIT_FAILURE;
  291. }
  292. #endif
  293. starpu_shutdown();
  294. if (ncuda == 0 && nopencl == 0)
  295. return STARPU_TEST_SKIPPED;
  296. else
  297. return EXIT_SUCCESS;
  298. #else /* !STARPU_USE_CPU */
  299. /* Without the CPU, there is no point in using the multiformat
  300. * interface, so this test is pointless. */
  301. return STARPU_TEST_SKIPPED;
  302. #endif
  303. }