multiformat_handle_conversion.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include "generic.h"
  18. #include "../../../../helper.h"
  19. #define DEBUG 0
  20. #if DEBUG
  21. #define SYNCHRONOUS 1 /* Easier to debug with synchronous tasks */
  22. #define ENTER() do { FPRINTF(stderr, "Entering %s\n", __starpu_func__); } while (0)
  23. #else
  24. #define SYNCHRONOUS 0
  25. #define ENTER()
  26. #endif
  27. extern struct stats global_stats;
  28. #if defined(STARPU_USE_CUDA) || defined(STARPU_USE_OPENCL) || defined(STARPU_USE_MIC)
  29. static void
  30. create_and_submit_tasks(int where, starpu_data_handle_t handles[])
  31. {
  32. FPRINTF(stderr, "***** Starting Task 1\n");
  33. struct starpu_codelet cl =
  34. {
  35. .modes = { STARPU_RW },
  36. .nbuffers = 1,
  37. .where = where
  38. };
  39. #ifdef STARPU_USE_CUDA
  40. if (where & STARPU_CUDA)
  41. cl.cuda_funcs[0] = cuda_func;
  42. #endif
  43. #ifdef STARPU_USE_OPENCL
  44. if (where & STARPU_OPENCL)
  45. cl.opencl_funcs[0] = opencl_func;
  46. #endif
  47. #ifdef STARPU_USE_MIC
  48. if (where & STARPU_MIC)
  49. cl.mic_funcs[0] = mic_func;
  50. #endif
  51. struct starpu_task *task = starpu_task_create();
  52. task->synchronous = SYNCHRONOUS;
  53. task->cl = &cl;
  54. task->handles[0] = handles[0];
  55. assert(starpu_task_submit(task) == 0);
  56. #ifdef STARPU_USE_CPU
  57. FPRINTF(stderr, "***** Starting Task 2\n");
  58. struct starpu_codelet cl2 =
  59. {
  60. .modes = { STARPU_RW },
  61. .cpu_funcs = {cpu_func},
  62. .nbuffers = 1,
  63. .where = STARPU_CPU,
  64. };
  65. struct starpu_task *task2 = starpu_task_create();
  66. task2->synchronous = SYNCHRONOUS;
  67. task2->cl = &cl2;
  68. task2->handles[0] = handles[1];
  69. assert(starpu_task_submit(task2) == 0);
  70. #endif /* !STARPU_USE_CPU */
  71. FPRINTF(stderr, "***** Starting Task 3\n");
  72. struct starpu_codelet cl3 =
  73. {
  74. .modes = { STARPU_RW, STARPU_RW },
  75. .nbuffers = 2,
  76. .where = where
  77. };
  78. #ifdef STARPU_USE_CUDA
  79. if (where & STARPU_CUDA)
  80. cl3.cuda_funcs[0] = cuda_func;
  81. #endif
  82. #ifdef STARPU_USE_OPENCL
  83. if (where & STARPU_OPENCL)
  84. cl3.opencl_funcs[0] = opencl_func;
  85. #endif
  86. #ifdef STARPU_USE_MIC
  87. if (where & STARPU_MIC)
  88. cl3.mic_funcs[0] = mic_func;
  89. #endif
  90. struct starpu_task *task3 = starpu_task_create();
  91. task3->synchronous = SYNCHRONOUS;
  92. task3->cl = &cl3;
  93. task3->handles[0] = handles[0];
  94. task3->handles[1] = handles[1];
  95. assert(starpu_task_submit(task3) == 0);
  96. assert(starpu_task_wait_for_all() == 0);
  97. FPRINTF(stderr, "***** End of all tasks\n");
  98. return;
  99. }
  100. #endif
  101. /* XXX Just a little bit of copy/pasta here... */
  102. #ifdef STARPU_USE_CUDA
  103. static int
  104. test_cuda(void)
  105. {
  106. int i;
  107. int vector1[NX];
  108. int vector2[NX];
  109. starpu_data_handle_t handles[2];
  110. for (i = 0; i < NX; i++)
  111. {
  112. vector1[i] = i;
  113. vector2[i] = i;
  114. }
  115. starpu_multiformat_data_register(&handles[0], STARPU_MAIN_RAM, vector1, NX, &ops);
  116. starpu_multiformat_data_register(&handles[1], STARPU_MAIN_RAM, vector2, NX, &ops);
  117. memset(&global_stats, 0, sizeof(global_stats));
  118. create_and_submit_tasks(STARPU_CUDA, handles);
  119. starpu_data_unregister(handles[0]);
  120. starpu_data_unregister(handles[1]);
  121. #if DEBUG
  122. print_stats(&global_stats);
  123. #endif
  124. struct stats expected_stats;
  125. #ifdef STARPU_USE_CPU
  126. expected_stats.cpu = 1;
  127. #endif /* !STARPU_USE_CPU */
  128. #ifdef STARPU_USE_OPENCL
  129. expected_stats.opencl = 0;
  130. expected_stats.cpu_to_opencl = 0;
  131. expected_stats.opencl_to_cpu = 0;
  132. #endif /* !STARPU_USE_OPENCL */
  133. #ifdef STARPU_USE_MIC
  134. expected_stats.mic = 0;
  135. expected_stats.cpu_to_mic = 0;
  136. expected_stats.mic_to_cpu = 0;
  137. #endif
  138. expected_stats.cuda = 2;
  139. expected_stats.cpu_to_cuda = 2;
  140. expected_stats.cuda_to_cpu = 2;
  141. return compare_stats(&expected_stats, &global_stats);
  142. }
  143. #endif /* !STARPU_USE_CUDA */
  144. #ifdef STARPU_USE_OPENCL
  145. static int
  146. test_opencl(void)
  147. {
  148. int i;
  149. int vector1[NX];
  150. int vector2[NX];
  151. starpu_data_handle_t handles[2];
  152. for (i = 0; i < NX; i++)
  153. {
  154. vector1[i] = i;
  155. vector2[i] = i;
  156. }
  157. starpu_multiformat_data_register(&handles[0], STARPU_MAIN_RAM, vector1, NX, &ops);
  158. starpu_multiformat_data_register(&handles[1], STARPU_MAIN_RAM, vector2, NX, &ops);
  159. memset(&global_stats, 0, sizeof(global_stats));
  160. create_and_submit_tasks(STARPU_OPENCL, handles);
  161. starpu_data_unregister(handles[0]);
  162. starpu_data_unregister(handles[1]);
  163. #if DEBUG
  164. print_stats(&global_stats);
  165. #endif
  166. struct stats expected_stats;
  167. #ifdef STARPU_USE_CPU
  168. expected_stats.cpu = 1;
  169. #endif /* !STARPU_USE_CPU */
  170. #ifdef STARPU_USE_CUDA
  171. expected_stats.cuda = 0;
  172. expected_stats.cpu_to_cuda = 0;
  173. expected_stats.cuda_to_cpu = 0;
  174. #endif /* !STARPU_USE_CUDA */
  175. #ifdef STARPU_USE_MIC
  176. expected_stats.mic = 0;
  177. expected_stats.cpu_to_mic = 0;
  178. expected_stats.mic_to_cpu = 0;
  179. #endif
  180. expected_stats.opencl = 2;
  181. expected_stats.cpu_to_opencl = 2;
  182. expected_stats.opencl_to_cpu = 2;
  183. return compare_stats(&expected_stats, &global_stats);
  184. }
  185. #endif /* !STARPU_USE_OPENCL */
  186. #ifdef STARPU_USE_MIC
  187. static int
  188. test_mic(void)
  189. {
  190. int i;
  191. int vector1[NX];
  192. int vector2[NX];
  193. starpu_data_handle_t handles[2];
  194. for (i = 0; i < NX; i++)
  195. {
  196. vector1[i] = i;
  197. vector2[i] = i;
  198. }
  199. starpu_multiformat_data_register(&handles[0], STARPU_MAIN_RAM, vector1, NX, &ops);
  200. starpu_multiformat_data_register(&handles[1], STARPU_MAIN_RAM, vector2, NX, &ops);
  201. memset(&global_stats, 0, sizeof(global_stats));
  202. create_and_submit_tasks(STARPU_MIC, handles);
  203. starpu_data_unregister(handles[0]);
  204. starpu_data_unregister(handles[1]);
  205. #if DEBUG
  206. print_stats(&global_stats);
  207. #endif
  208. struct stats expected_stats;
  209. #ifdef STARPU_USE_CPU
  210. expected_stats.cpu = 1;
  211. #endif /* !STARPU_USE_CPU */
  212. #ifdef STARPU_USE_OPENCL
  213. expected_stats.opencl = 0;
  214. expected_stats.cpu_to_opencl = 0;
  215. expected_stats.opencl_to_cpu = 0;
  216. #endif /* !STARPU_USE_OPENCL */
  217. #ifdef STARPU_USE_CUDA
  218. expected_stats.cuda = 0;
  219. expected_stats.cpu_to_cuda = 0;
  220. expected_stats.cuda_to_cpu = 0;
  221. #endif
  222. expected_stats.mic = 2;
  223. expected_stats.cpu_to_mic = 2;
  224. expected_stats.mic_to_cpu = 2;
  225. return compare_stats(&expected_stats, &global_stats);
  226. }
  227. #endif /* !STARPU_USE_CUDA */
  228. int
  229. main(int argc, char **argv)
  230. {
  231. #ifdef STARPU_USE_CPU
  232. int ret;
  233. struct starpu_conf conf;
  234. starpu_conf_init(&conf);
  235. conf.ncuda = 2;
  236. conf.nopencl = 1;
  237. conf.nmic = 1;
  238. ret = starpu_initialize(&conf, &argc, &argv);
  239. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  240. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  241. unsigned int ncpu = starpu_cpu_worker_get_count();
  242. if (ncpu == 0)
  243. {
  244. FPRINTF(stderr, "No CPUS, cannot run this test.\n");
  245. return STARPU_TEST_SKIPPED;
  246. }
  247. unsigned int ncuda = starpu_cuda_worker_get_count();
  248. unsigned int nopencl = starpu_opencl_worker_get_count();
  249. unsigned int nmic = starpu_mic_worker_get_count();
  250. #ifdef STARPU_USE_OPENCL
  251. if (nopencl > 0 && test_opencl() != 0)
  252. {
  253. FPRINTF(stderr, "OPENCL FAILED\n");
  254. return EXIT_FAILURE;
  255. }
  256. #endif
  257. #ifdef STARPU_USE_CUDA
  258. if (ncuda > 0 && test_cuda() != 0)
  259. {
  260. FPRINTF(stderr, "CUDA FAILED \n");
  261. return EXIT_FAILURE;
  262. }
  263. #endif
  264. #ifdef STARPU_USE_MIC
  265. if (nmic > 0 && test_mic() != 0)
  266. {
  267. FPRINTF(stderr, "MIC FAILED \n");
  268. return EXIT_FAILURE;
  269. }
  270. #endif
  271. starpu_shutdown();
  272. if (ncuda == 0 && nopencl == 0 && nmic == 0)
  273. return STARPU_TEST_SKIPPED;
  274. else
  275. return EXIT_SUCCESS;
  276. #else /* !STARPU_USE_CPU */
  277. /* Without the CPU, there is no point in using the multiformat
  278. * interface, so this test is pointless. */
  279. return STARPU_TEST_SKIPPED;
  280. #endif
  281. }