multiformat_handle_conversion.c 7.4 KB

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