multiformat.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 = 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. .cpu_funcs = {multiformat_scal_cpu_func, NULL},
  70. .nbuffers = 1,
  71. .modes = { STARPU_RW },
  72. .name = "codelet_real"
  73. };
  74. #endif /* !STARPU_USE_CPU */
  75. #ifdef STARPU_USE_CUDA
  76. static struct starpu_codelet cuda_cl =
  77. {
  78. .cuda_funcs = { multiformat_scal_cuda_func, NULL },
  79. .nbuffers = 1,
  80. .modes = { STARPU_RW },
  81. .name = "cuda_codelet"
  82. };
  83. #endif /* !STARPU_USE_CUDA */
  84. #ifdef STARPU_USE_OPENCL
  85. static struct starpu_codelet opencl_cl =
  86. {
  87. .opencl_funcs = { multiformat_scal_opencl_func, NULL },
  88. .nbuffers = 1,
  89. .modes = { STARPU_RW },
  90. .name = "opencl_codelet"
  91. };
  92. #endif /* !STARPU_USE_OPENCL */
  93. /*
  94. * Main functions
  95. */
  96. static void
  97. init_problem_data(void)
  98. {
  99. int i;
  100. for (i = 0; i < N_ELEMENTS; i++)
  101. {
  102. array_of_structs[i].x = 1.0 + i;
  103. array_of_structs[i].y = 42.0;
  104. }
  105. }
  106. static void
  107. register_data(void)
  108. {
  109. starpu_multiformat_data_register(&array_of_structs_handle,
  110. 0,
  111. &array_of_structs,
  112. N_ELEMENTS,
  113. &format_ops);
  114. }
  115. static int
  116. create_and_submit_task(unsigned int dev)
  117. {
  118. struct starpu_task *task = starpu_task_create();
  119. switch (dev)
  120. {
  121. case STARPU_CPU:
  122. task->cl = &cpu_cl;
  123. break;
  124. case STARPU_CUDA:
  125. task->cl = &cuda_cl;
  126. break;
  127. case STARPU_OPENCL:
  128. task->cl = &opencl_cl;
  129. break;
  130. default:
  131. assert(0);
  132. }
  133. task->synchronous = 1;
  134. task->handles[0] = array_of_structs_handle;
  135. task->cl_arg = NULL;
  136. task->cl_arg_size = 0;
  137. return starpu_task_submit(task);
  138. }
  139. static void
  140. create_and_submit_tasks(void)
  141. {
  142. int err;
  143. #ifdef STARPU_USE_CUDA
  144. if (ncuda > 0)
  145. {
  146. err = create_and_submit_task(STARPU_CUDA);
  147. if (err != 0)
  148. {
  149. FPRINTF(stderr, "Cuda : %s\n", strerror(-err));
  150. return;
  151. }
  152. }
  153. #endif
  154. #ifdef STARPU_USE_CPU
  155. if (ncpu > 0)
  156. {
  157. err = create_and_submit_task(STARPU_CPU);
  158. if (err != 0)
  159. {
  160. FPRINTF(stderr, "CPU : %s\n", strerror(-err));
  161. return;
  162. }
  163. }
  164. #endif
  165. #ifdef STARPU_USE_OPENCL
  166. if (nopencl > 0)
  167. {
  168. err = create_and_submit_task(STARPU_OPENCL);
  169. if (err != 0)
  170. {
  171. FPRINTF(stderr, "OpenCL : %s\n", strerror(-err));
  172. return;
  173. }
  174. }
  175. #endif /* !STARPU_USE_OPENCL */
  176. }
  177. static void
  178. unregister_data(void)
  179. {
  180. starpu_data_unregister(array_of_structs_handle);
  181. }
  182. static void
  183. print_it(void)
  184. {
  185. int i;
  186. for (i = 0; i < N_ELEMENTS; i++)
  187. {
  188. FPRINTF(stderr, "(%.2f %.2f) ",
  189. array_of_structs[i].x,
  190. array_of_structs[i].y);
  191. }
  192. FPRINTF(stderr, "\n");
  193. }
  194. static int
  195. check_it(void)
  196. {
  197. int i;
  198. for (i = 0; i < N_ELEMENTS; i++)
  199. {
  200. float expected_value = i + 1.0;
  201. #if STARPU_USE_CUDA
  202. if (ncuda > 0)
  203. expected_value *= array_of_structs[i].y;
  204. #endif
  205. #if STARPU_USE_OPENCL
  206. if (nopencl > 0)
  207. expected_value *= array_of_structs[i].y;
  208. #endif
  209. expected_value *= array_of_structs[i].y;
  210. if (array_of_structs[i].x != expected_value)
  211. return EXIT_FAILURE;
  212. }
  213. return EXIT_SUCCESS;
  214. }
  215. #ifdef STARPU_USE_OPENCL
  216. struct starpu_opencl_program opencl_program;
  217. struct starpu_opencl_program opencl_conversion_program;
  218. #endif
  219. static int
  220. gpus_available()
  221. {
  222. #ifdef STARPU_USE_CUDA
  223. if (ncuda > 0)
  224. return 1;
  225. #endif
  226. #ifdef STARPU_USE_OPENCL
  227. if (nopencl > 0)
  228. return 1;
  229. #endif
  230. return 0;
  231. }
  232. int
  233. main(void)
  234. {
  235. #ifdef STARPU_USE_CPU
  236. starpu_init(NULL);
  237. ncpu = starpu_cpu_worker_get_count();
  238. #ifdef STARPU_USE_CUDA
  239. ncuda = starpu_cuda_worker_get_count();
  240. #endif
  241. #ifdef STARPU_USE_OPENCL
  242. nopencl = starpu_opencl_worker_get_count();
  243. #endif
  244. if (ncpu == 0 || !gpus_available())
  245. return 77;
  246. #ifdef STARPU_USE_OPENCL
  247. starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_opencl_kernel.cl",
  248. &opencl_program, NULL);
  249. starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_conversion_codelets_opencl_kernel.cl",
  250. &opencl_conversion_program, NULL);
  251. #endif
  252. init_problem_data();
  253. print_it();
  254. register_data();
  255. create_and_submit_tasks();
  256. unregister_data();
  257. print_it();
  258. #ifdef STARPU_USE_OPENCL
  259. starpu_opencl_unload_opencl(&opencl_program);
  260. starpu_opencl_unload_opencl(&opencl_conversion_program);
  261. #endif
  262. starpu_shutdown();
  263. return check_it();
  264. #else
  265. /* Without the CPU, there is no point in using the multiformat
  266. * interface, so this test is pointless. */
  267. return EXIT_SUCCESS;
  268. #endif
  269. }