multiformat.c 7.0 KB

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