multiformat_handle_conversion.c 7.1 KB

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