multiformat_handle_conversion.c 7.6 KB

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