multiformat.c 6.6 KB

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