multiformat.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 "multiformat_types.h"
  18. static int ncpu = 0;
  19. #ifdef STARPU_USE_CUDA
  20. static int ncuda = 0;
  21. #endif
  22. #ifdef STARPU_USE_OPENCL
  23. static int nopencl = 0;
  24. #endif
  25. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  26. static struct point array_of_structs[N_ELEMENTS];
  27. static starpu_data_handle_t array_of_structs_handle;
  28. void
  29. multiformat_scal_cpu_func(void *buffers[], void *args)
  30. {
  31. struct point *aos;
  32. unsigned int n, i;
  33. (void)args;
  34. aos = (struct point *) STARPU_MULTIFORMAT_GET_CPU_PTR(buffers[0]);
  35. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  36. for (i = 0; i < n; i++)
  37. {
  38. aos[i].x *= aos[i].y;
  39. }
  40. }
  41. #ifdef STARPU_USE_CUDA
  42. extern struct starpu_codelet cpu_to_cuda_cl;
  43. extern struct starpu_codelet cuda_to_cpu_cl;
  44. #endif
  45. #ifdef STARPU_USE_OPENCL
  46. extern struct starpu_codelet cpu_to_opencl_cl;
  47. extern struct starpu_codelet opencl_to_cpu_cl;
  48. #endif
  49. static struct starpu_multiformat_data_interface_ops format_ops =
  50. {
  51. #ifdef STARPU_USE_CUDA
  52. .cuda_elemsize = 2* sizeof(float),
  53. .cpu_to_cuda_cl = &cpu_to_cuda_cl,
  54. .cuda_to_cpu_cl = &cuda_to_cpu_cl,
  55. #endif
  56. #ifdef STARPU_USE_OPENCL
  57. .opencl_elemsize = 2 * sizeof(float),
  58. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  59. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  60. #endif
  61. .cpu_elemsize = sizeof(struct point),
  62. };
  63. #ifdef STARPU_USE_CUDA
  64. extern void multiformat_scal_cuda_func(void *buffers[], void *arg);
  65. #endif
  66. #ifdef STARPU_USE_OPENCL
  67. extern void multiformat_scal_opencl_func(void *buffers[], void *arg);
  68. #endif
  69. #ifdef STARPU_USE_CPU
  70. static struct starpu_codelet cpu_cl =
  71. {
  72. .cpu_funcs = {multiformat_scal_cpu_func},
  73. .cpu_funcs_name = {"multiformat_scal_cpu_func"},
  74. .nbuffers = 1,
  75. .modes = { STARPU_RW },
  76. .name = "codelet_real"
  77. };
  78. #endif /* !STARPU_USE_CPU */
  79. #ifdef STARPU_USE_CUDA
  80. static struct starpu_codelet cuda_cl =
  81. {
  82. .cuda_funcs = { multiformat_scal_cuda_func },
  83. .nbuffers = 1,
  84. .modes = { STARPU_RW },
  85. .name = "cuda_codelet"
  86. };
  87. #endif /* !STARPU_USE_CUDA */
  88. #ifdef STARPU_USE_OPENCL
  89. static struct starpu_codelet opencl_cl =
  90. {
  91. .opencl_funcs = { multiformat_scal_opencl_func },
  92. .nbuffers = 1,
  93. .modes = { STARPU_RW },
  94. .name = "opencl_codelet"
  95. };
  96. #endif /* !STARPU_USE_OPENCL */
  97. /*
  98. * Main functions
  99. */
  100. static void
  101. init_problem_data(void)
  102. {
  103. int i;
  104. for (i = 0; i < N_ELEMENTS; i++)
  105. {
  106. array_of_structs[i].x = 1.0 + i;
  107. array_of_structs[i].y = 42.0;
  108. }
  109. }
  110. static void
  111. register_data(void)
  112. {
  113. starpu_multiformat_data_register(&array_of_structs_handle,
  114. STARPU_MAIN_RAM,
  115. &array_of_structs,
  116. N_ELEMENTS,
  117. &format_ops);
  118. }
  119. static int
  120. create_and_submit_task(unsigned int dev)
  121. {
  122. struct starpu_task *task = starpu_task_create();
  123. switch (dev)
  124. {
  125. #ifdef STARPU_USE_CPU
  126. case STARPU_CPU:
  127. task->cl = &cpu_cl;
  128. break;
  129. #endif
  130. #ifdef STARPU_USE_CUDA
  131. case STARPU_CUDA:
  132. task->cl = &cuda_cl;
  133. break;
  134. #endif
  135. #ifdef STARPU_USE_OPENCL
  136. case STARPU_OPENCL:
  137. task->cl = &opencl_cl;
  138. break;
  139. #endif
  140. default:
  141. assert(0);
  142. }
  143. task->synchronous = 1;
  144. task->handles[0] = array_of_structs_handle;
  145. task->cl_arg = NULL;
  146. task->cl_arg_size = 0;
  147. return starpu_task_submit(task);
  148. }
  149. static void
  150. create_and_submit_tasks(void)
  151. {
  152. #ifdef STARPU_USE_CUDA
  153. if (ncuda > 0)
  154. {
  155. int err;
  156. err = create_and_submit_task(STARPU_CUDA);
  157. if (err != 0)
  158. {
  159. FPRINTF(stderr, "Cuda : %s\n", strerror(-err));
  160. return;
  161. }
  162. }
  163. #endif
  164. #ifdef STARPU_USE_CPU
  165. if (ncpu > 0)
  166. {
  167. int err;
  168. err = create_and_submit_task(STARPU_CPU);
  169. if (err != 0)
  170. {
  171. FPRINTF(stderr, "CPU : %s\n", strerror(-err));
  172. return;
  173. }
  174. }
  175. #endif
  176. #ifdef STARPU_USE_OPENCL
  177. if (nopencl > 0)
  178. {
  179. int err;
  180. err = create_and_submit_task(STARPU_OPENCL);
  181. if (err != 0)
  182. {
  183. FPRINTF(stderr, "OpenCL : %s\n", strerror(-err));
  184. return;
  185. }
  186. }
  187. #endif /* !STARPU_USE_OPENCL */
  188. }
  189. static void
  190. unregister_data(void)
  191. {
  192. starpu_data_unregister(array_of_structs_handle);
  193. }
  194. static void
  195. print_it(void)
  196. {
  197. int i;
  198. for (i = 0; i < N_ELEMENTS; i++)
  199. {
  200. FPRINTF(stderr, "(%.2f %.2f) ",
  201. array_of_structs[i].x,
  202. array_of_structs[i].y);
  203. }
  204. FPRINTF(stderr, "\n");
  205. }
  206. static int
  207. check_it(void)
  208. {
  209. int i;
  210. for (i = 0; i < N_ELEMENTS; i++)
  211. {
  212. float expected_value = i + 1.0;
  213. #ifdef STARPU_USE_CUDA
  214. if (ncuda > 0)
  215. expected_value *= array_of_structs[i].y;
  216. #endif
  217. #ifdef STARPU_USE_OPENCL
  218. if (nopencl > 0)
  219. expected_value *= array_of_structs[i].y;
  220. #endif
  221. expected_value *= array_of_structs[i].y;
  222. if (array_of_structs[i].x != expected_value)
  223. return EXIT_FAILURE;
  224. }
  225. return EXIT_SUCCESS;
  226. }
  227. #ifdef STARPU_USE_OPENCL
  228. struct starpu_opencl_program opencl_program;
  229. struct starpu_opencl_program opencl_conversion_program;
  230. #endif
  231. static int
  232. gpus_available(void)
  233. {
  234. #ifdef STARPU_USE_CUDA
  235. if (ncuda > 0)
  236. return 1;
  237. #endif
  238. #ifdef STARPU_USE_OPENCL
  239. if (nopencl > 0)
  240. return 1;
  241. #endif
  242. return 0;
  243. }
  244. int
  245. main(void)
  246. {
  247. #ifdef STARPU_USE_CPU
  248. int ret;
  249. struct starpu_conf conf;
  250. starpu_conf_init(&conf);
  251. /* this example doesn't support MPI Master-Slave */
  252. conf.nmpi_ms = 0;
  253. ret = starpu_init(&conf);
  254. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  255. ncpu = starpu_cpu_worker_get_count();
  256. #ifdef STARPU_USE_CUDA
  257. ncuda = starpu_cuda_worker_get_count();
  258. #endif
  259. #ifdef STARPU_USE_OPENCL
  260. nopencl = starpu_opencl_worker_get_count();
  261. #endif
  262. if (ncpu == 0 || !gpus_available())
  263. {
  264. starpu_shutdown();
  265. return 77;
  266. }
  267. #ifdef STARPU_USE_OPENCL
  268. ret = starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_opencl_kernel.cl",
  269. &opencl_program, NULL);
  270. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  271. ret = starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_conversion_codelets_opencl_kernel.cl",
  272. &opencl_conversion_program, NULL);
  273. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  274. #endif
  275. init_problem_data();
  276. print_it();
  277. register_data();
  278. create_and_submit_tasks();
  279. unregister_data();
  280. print_it();
  281. #ifdef STARPU_USE_OPENCL
  282. ret = starpu_opencl_unload_opencl(&opencl_program);
  283. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  284. ret = starpu_opencl_unload_opencl(&opencl_conversion_program);
  285. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  286. #endif
  287. starpu_shutdown();
  288. return check_it();
  289. #else
  290. /* Without the CPU, there is no point in using the multiformat
  291. * interface, so this test is pointless. */
  292. return 77;
  293. #endif
  294. }