multiformat_handle_conversion.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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)
  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. struct starpu_task *task = starpu_task_create();
  49. task->synchronous = SYNCHRONOUS;
  50. task->cl = &cl;
  51. task->handles[0] = handles[0];
  52. ret = starpu_task_submit(task);
  53. assert(ret == 0);
  54. #ifdef STARPU_USE_CPU
  55. FPRINTF(stderr, "***** Starting Task 2\n");
  56. struct starpu_codelet cl2 =
  57. {
  58. .modes = { STARPU_RW },
  59. .cpu_funcs = {cpu_func},
  60. .nbuffers = 1,
  61. .where = STARPU_CPU,
  62. };
  63. struct starpu_task *task2 = starpu_task_create();
  64. task2->synchronous = SYNCHRONOUS;
  65. task2->cl = &cl2;
  66. task2->handles[0] = handles[1];
  67. ret = starpu_task_submit(task2);
  68. assert(ret == 0);
  69. #endif /* !STARPU_USE_CPU */
  70. FPRINTF(stderr, "***** Starting Task 3\n");
  71. struct starpu_codelet cl3 =
  72. {
  73. .modes = { STARPU_RW, STARPU_RW },
  74. .nbuffers = 2,
  75. .where = where
  76. };
  77. #ifdef STARPU_USE_CUDA
  78. if (where & STARPU_CUDA)
  79. cl3.cuda_funcs[0] = cuda_func;
  80. #endif
  81. #ifdef STARPU_USE_OPENCL
  82. if (where & STARPU_OPENCL)
  83. cl3.opencl_funcs[0] = opencl_func;
  84. #endif
  85. struct starpu_task *task3 = starpu_task_create();
  86. task3->synchronous = SYNCHRONOUS;
  87. task3->cl = &cl3;
  88. task3->handles[0] = handles[0];
  89. task3->handles[1] = handles[1];
  90. ret = starpu_task_submit(task3);
  91. assert(ret == 0);
  92. ret = starpu_task_wait_for_all();
  93. assert(ret == 0);
  94. FPRINTF(stderr, "***** End of all tasks\n");
  95. return;
  96. }
  97. #endif
  98. /* XXX Just a little bit of copy/pasta here... */
  99. #ifdef STARPU_USE_CUDA
  100. static int
  101. test_cuda(void)
  102. {
  103. int i;
  104. int vector1[NX];
  105. int vector2[NX];
  106. starpu_data_handle_t handles[2];
  107. for (i = 0; i < NX; i++)
  108. {
  109. vector1[i] = i;
  110. vector2[i] = i;
  111. }
  112. starpu_multiformat_data_register(&handles[0], STARPU_MAIN_RAM, vector1, NX, &ops);
  113. starpu_multiformat_data_register(&handles[1], STARPU_MAIN_RAM, vector2, NX, &ops);
  114. memset(&global_stats, 0, sizeof(global_stats));
  115. create_and_submit_tasks(STARPU_CUDA, handles);
  116. starpu_data_unregister(handles[0]);
  117. starpu_data_unregister(handles[1]);
  118. #if DEBUG
  119. print_stats(&global_stats);
  120. #endif
  121. struct stats expected_stats;
  122. #ifdef STARPU_USE_CPU
  123. expected_stats.cpu = 1;
  124. #endif /* !STARPU_USE_CPU */
  125. #ifdef STARPU_USE_OPENCL
  126. expected_stats.opencl = 0;
  127. expected_stats.cpu_to_opencl = 0;
  128. expected_stats.opencl_to_cpu = 0;
  129. #endif /* !STARPU_USE_OPENCL */
  130. expected_stats.cuda = 2;
  131. expected_stats.cpu_to_cuda = 2;
  132. expected_stats.cuda_to_cpu = 2;
  133. return compare_stats(&expected_stats, &global_stats);
  134. }
  135. #endif /* !STARPU_USE_CUDA */
  136. #ifdef STARPU_USE_OPENCL
  137. static int
  138. test_opencl(void)
  139. {
  140. int i;
  141. int vector1[NX];
  142. int vector2[NX];
  143. starpu_data_handle_t handles[2];
  144. for (i = 0; i < NX; i++)
  145. {
  146. vector1[i] = i;
  147. vector2[i] = i;
  148. }
  149. starpu_multiformat_data_register(&handles[0], STARPU_MAIN_RAM, vector1, NX, &ops);
  150. starpu_multiformat_data_register(&handles[1], STARPU_MAIN_RAM, vector2, NX, &ops);
  151. memset(&global_stats, 0, sizeof(global_stats));
  152. create_and_submit_tasks(STARPU_OPENCL, handles);
  153. starpu_data_unregister(handles[0]);
  154. starpu_data_unregister(handles[1]);
  155. #if DEBUG
  156. print_stats(&global_stats);
  157. #endif
  158. struct stats expected_stats;
  159. #ifdef STARPU_USE_CPU
  160. expected_stats.cpu = 1;
  161. #endif /* !STARPU_USE_CPU */
  162. #ifdef STARPU_USE_CUDA
  163. expected_stats.cuda = 0;
  164. expected_stats.cpu_to_cuda = 0;
  165. expected_stats.cuda_to_cpu = 0;
  166. #endif /* !STARPU_USE_CUDA */
  167. expected_stats.opencl = 2;
  168. expected_stats.cpu_to_opencl = 2;
  169. expected_stats.opencl_to_cpu = 2;
  170. return compare_stats(&expected_stats, &global_stats);
  171. }
  172. #endif /* !STARPU_USE_OPENCL */
  173. int
  174. main(int argc, char **argv)
  175. {
  176. #ifdef STARPU_USE_CPU
  177. int ret;
  178. struct starpu_conf conf;
  179. starpu_conf_init(&conf);
  180. conf.ncuda = 2;
  181. conf.nopencl = 1;
  182. ret = starpu_initialize(&conf, &argc, &argv);
  183. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  184. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  185. unsigned int ncpu = starpu_cpu_worker_get_count();
  186. if (ncpu == 0)
  187. {
  188. FPRINTF(stderr, "No CPUS, cannot run this test.\n");
  189. return STARPU_TEST_SKIPPED;
  190. }
  191. unsigned int ncuda = starpu_cuda_worker_get_count();
  192. unsigned int nopencl = starpu_opencl_worker_get_count();
  193. #ifdef STARPU_USE_OPENCL
  194. if (nopencl > 0 && test_opencl() != 0)
  195. {
  196. FPRINTF(stderr, "OPENCL FAILED\n");
  197. return EXIT_FAILURE;
  198. }
  199. #endif
  200. #ifdef STARPU_USE_CUDA
  201. if (ncuda > 0 && test_cuda() != 0)
  202. {
  203. FPRINTF(stderr, "CUDA FAILED \n");
  204. return EXIT_FAILURE;
  205. }
  206. #endif
  207. starpu_shutdown();
  208. if (ncuda == 0 && nopencl == 0)
  209. return STARPU_TEST_SKIPPED;
  210. else
  211. return EXIT_SUCCESS;
  212. #else /* !STARPU_USE_CPU */
  213. /* Without the CPU, there is no point in using the multiformat
  214. * interface, so this test is pointless. */
  215. return STARPU_TEST_SKIPPED;
  216. #endif
  217. }