multiformat_handle_conversion.c 7.7 KB

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