multiformat.c 6.5 KB

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