multiformat.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2012 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. aos = (struct point *) STARPU_MULTIFORMAT_GET_CPU_PTR(buffers[0]);
  34. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  35. for (i = 0; i < n; i++)
  36. {
  37. aos[i].x *= aos[i].y;
  38. }
  39. }
  40. #ifdef STARPU_USE_CUDA
  41. extern struct starpu_codelet cpu_to_cuda_cl;
  42. extern struct starpu_codelet cuda_to_cpu_cl;
  43. #endif
  44. #ifdef STARPU_USE_OPENCL
  45. extern struct starpu_codelet cpu_to_opencl_cl;
  46. extern struct starpu_codelet opencl_to_cpu_cl;
  47. #endif
  48. static struct starpu_multiformat_data_interface_ops format_ops =
  49. {
  50. #ifdef STARPU_USE_CUDA
  51. .cuda_elemsize = 2* sizeof(float),
  52. .cpu_to_cuda_cl = &cpu_to_cuda_cl,
  53. .cuda_to_cpu_cl = &cuda_to_cpu_cl,
  54. #endif
  55. #ifdef STARPU_USE_OPENCL
  56. .opencl_elemsize = 2 * sizeof(float),
  57. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  58. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  59. #endif
  60. .cpu_elemsize = sizeof(struct point),
  61. };
  62. #ifdef STARPU_USE_CUDA
  63. extern void multiformat_scal_cuda_func(void *buffers[], void *arg);
  64. #endif
  65. #ifdef STARPU_USE_OPENCL
  66. extern void multiformat_scal_opencl_func(void *buffers[], void *arg);
  67. #endif
  68. #ifdef STARPU_USE_CPU
  69. static struct starpu_codelet cpu_cl =
  70. {
  71. .cpu_funcs = {multiformat_scal_cpu_func},
  72. .cpu_funcs_name = {"multiformat_scal_cpu_func"},
  73. .nbuffers = 1,
  74. .modes = { STARPU_RW },
  75. .name = "codelet_real"
  76. };
  77. #endif /* !STARPU_USE_CPU */
  78. #ifdef STARPU_USE_CUDA
  79. static struct starpu_codelet cuda_cl =
  80. {
  81. .cuda_funcs = { multiformat_scal_cuda_func },
  82. .nbuffers = 1,
  83. .modes = { STARPU_RW },
  84. .name = "cuda_codelet"
  85. };
  86. #endif /* !STARPU_USE_CUDA */
  87. #ifdef STARPU_USE_OPENCL
  88. static struct starpu_codelet opencl_cl =
  89. {
  90. .opencl_funcs = { multiformat_scal_opencl_func },
  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. STARPU_MAIN_RAM,
  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. #ifdef STARPU_USE_CUDA
  152. if (ncuda > 0)
  153. {
  154. int err;
  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. int err;
  167. err = create_and_submit_task(STARPU_CPU);
  168. if (err != 0)
  169. {
  170. FPRINTF(stderr, "CPU : %s\n", strerror(-err));
  171. return;
  172. }
  173. }
  174. #endif
  175. #ifdef STARPU_USE_OPENCL
  176. if (nopencl > 0)
  177. {
  178. int err;
  179. err = create_and_submit_task(STARPU_OPENCL);
  180. if (err != 0)
  181. {
  182. FPRINTF(stderr, "OpenCL : %s\n", strerror(-err));
  183. return;
  184. }
  185. }
  186. #endif /* !STARPU_USE_OPENCL */
  187. }
  188. static void
  189. unregister_data(void)
  190. {
  191. starpu_data_unregister(array_of_structs_handle);
  192. }
  193. static void
  194. print_it(void)
  195. {
  196. int i;
  197. for (i = 0; i < N_ELEMENTS; i++)
  198. {
  199. FPRINTF(stderr, "(%.2f %.2f) ",
  200. array_of_structs[i].x,
  201. array_of_structs[i].y);
  202. }
  203. FPRINTF(stderr, "\n");
  204. }
  205. static int
  206. check_it(void)
  207. {
  208. int i;
  209. for (i = 0; i < N_ELEMENTS; i++)
  210. {
  211. float expected_value = i + 1.0;
  212. #ifdef STARPU_USE_CUDA
  213. if (ncuda > 0)
  214. expected_value *= array_of_structs[i].y;
  215. #endif
  216. #ifdef STARPU_USE_OPENCL
  217. if (nopencl > 0)
  218. expected_value *= array_of_structs[i].y;
  219. #endif
  220. expected_value *= array_of_structs[i].y;
  221. if (array_of_structs[i].x != expected_value)
  222. return EXIT_FAILURE;
  223. }
  224. return EXIT_SUCCESS;
  225. }
  226. #ifdef STARPU_USE_OPENCL
  227. struct starpu_opencl_program opencl_program;
  228. struct starpu_opencl_program opencl_conversion_program;
  229. #endif
  230. static int
  231. gpus_available(void)
  232. {
  233. #ifdef STARPU_USE_CUDA
  234. if (ncuda > 0)
  235. return 1;
  236. #endif
  237. #ifdef STARPU_USE_OPENCL
  238. if (nopencl > 0)
  239. return 1;
  240. #endif
  241. return 0;
  242. }
  243. int
  244. main(void)
  245. {
  246. #ifdef STARPU_USE_CPU
  247. int ret;
  248. ret = starpu_init(NULL);
  249. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  250. ncpu = starpu_cpu_worker_get_count();
  251. #ifdef STARPU_USE_CUDA
  252. ncuda = starpu_cuda_worker_get_count();
  253. #endif
  254. #ifdef STARPU_USE_OPENCL
  255. nopencl = starpu_opencl_worker_get_count();
  256. #endif
  257. if (ncpu == 0 || !gpus_available())
  258. {
  259. starpu_shutdown();
  260. return 77;
  261. }
  262. #ifdef STARPU_USE_OPENCL
  263. ret = starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_opencl_kernel.cl",
  264. &opencl_program, NULL);
  265. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  266. ret = starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_conversion_codelets_opencl_kernel.cl",
  267. &opencl_conversion_program, NULL);
  268. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  269. #endif
  270. init_problem_data();
  271. print_it();
  272. register_data();
  273. create_and_submit_tasks();
  274. unregister_data();
  275. print_it();
  276. #ifdef STARPU_USE_OPENCL
  277. ret = starpu_opencl_unload_opencl(&opencl_program);
  278. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  279. starpu_opencl_unload_opencl(&opencl_conversion_program);
  280. #endif
  281. starpu_shutdown();
  282. return check_it();
  283. #else
  284. /* Without the CPU, there is no point in using the multiformat
  285. * interface, so this test is pointless. */
  286. return 77;
  287. #endif
  288. }