multiformat_handle_conversion.c 7.7 KB

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