multiformat.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 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 struct point array_of_structs[N_ELEMENTS];
  22. static starpu_data_handle_t array_of_structs_handle;
  23. static void
  24. multiformat_scal_cpu_func(void *buffers[], void *args)
  25. {
  26. struct point *aos;
  27. unsigned int n, i;
  28. aos = STARPU_MULTIFORMAT_GET_PTR(buffers[0]);
  29. n = STARPU_MULTIFORMAT_GET_NX(buffers[0]);
  30. for (i = 0; i < n; i++)
  31. {
  32. aos[i].x *= aos[i].y;
  33. }
  34. }
  35. #ifdef STARPU_USE_CUDA
  36. extern struct starpu_codelet cpu_to_cuda_cl;
  37. extern struct starpu_codelet cuda_to_cpu_cl;
  38. #endif
  39. #ifdef STARPU_USE_OPENCL
  40. extern struct starpu_codelet cpu_to_opencl_cl;
  41. extern struct starpu_codelet opencl_to_cpu_cl;
  42. #endif
  43. static struct starpu_multiformat_data_interface_ops format_ops =
  44. {
  45. #ifdef STARPU_USE_CUDA
  46. .cuda_elemsize = 2* sizeof(float),
  47. .cpu_to_cuda_cl = &cpu_to_cuda_cl,
  48. .cuda_to_cpu_cl = &cuda_to_cpu_cl,
  49. #endif
  50. #ifdef STARPU_USE_OPENCL
  51. .opencl_elemsize = 2 * sizeof(float),
  52. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  53. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  54. #endif
  55. .cpu_elemsize = sizeof(struct point),
  56. };
  57. #ifdef STARPU_USE_CUDA
  58. extern void multiformat_scal_cuda_func(void *buffers[], void *arg);
  59. #endif
  60. #ifdef STARPU_USE_OPENCL
  61. extern void multiformat_scal_opencl_func(void *buffers[], void *arg);
  62. #endif
  63. static struct starpu_codelet cl =
  64. {
  65. .where = STARPU_CUDA | STARPU_OPENCL,
  66. .cpu_funcs = {multiformat_scal_cpu_func, NULL},
  67. #ifdef STARPU_USE_CUDA
  68. .cuda_funcs = {multiformat_scal_cuda_func, NULL},
  69. #endif
  70. #ifdef STARPU_USE_OPENCL
  71. .opencl_funcs = {multiformat_scal_opencl_func, NULL},
  72. #endif
  73. .nbuffers = 1,
  74. .name = "codelet_real"
  75. };
  76. /*
  77. * Main functions
  78. */
  79. static void
  80. init_problem_data(void)
  81. {
  82. int i;
  83. for (i = 0; i < N_ELEMENTS; i++)
  84. {
  85. array_of_structs[i].x = 1.0 + i;
  86. array_of_structs[i].y = 42.0;
  87. }
  88. }
  89. static void
  90. register_data(void)
  91. {
  92. starpu_multiformat_data_register(&array_of_structs_handle,
  93. 0,
  94. &array_of_structs,
  95. N_ELEMENTS,
  96. &format_ops);
  97. }
  98. static void
  99. create_and_submit_tasks(void)
  100. {
  101. int err;
  102. #ifdef STARPU_USE_CUDA
  103. struct starpu_task *task = starpu_task_create();
  104. cl.where = STARPU_CUDA;
  105. task->cl = &cl;
  106. task->synchronous = 1;
  107. task->buffers[0].handle = array_of_structs_handle;
  108. task->buffers[0].mode = STARPU_RW;
  109. task->cl_arg = NULL;
  110. task->cl_arg_size = 0;
  111. err = starpu_task_submit(task);
  112. if (err != 0)
  113. {
  114. FPRINTF(stderr, "Err : %s\n", strerror(-err));
  115. return;
  116. }
  117. #endif
  118. struct starpu_task *task2 = starpu_task_create();
  119. cl.where = STARPU_CPU;
  120. task2->cl = &cl;
  121. task2->synchronous = 1;
  122. task2->buffers[0].handle = array_of_structs_handle;
  123. task2->buffers[0].mode = STARPU_RW;
  124. task2->cl_arg = NULL;
  125. cl.where = STARPU_CPU;
  126. task2->cl_arg_size = 0;
  127. err = starpu_task_submit(task2);
  128. if (err != 0)
  129. {
  130. FPRINTF(stderr, "Err : %s\n", strerror(-err));
  131. return;
  132. }
  133. }
  134. static void
  135. unregister_data(void)
  136. {
  137. starpu_data_unregister(array_of_structs_handle);
  138. }
  139. static void
  140. print_it(void)
  141. {
  142. int i;
  143. for (i = 0; i < N_ELEMENTS; i++)
  144. {
  145. FPRINTF(stderr, "(%.2f %.2f) ",
  146. array_of_structs[i].x,
  147. array_of_structs[i].y);
  148. }
  149. FPRINTF(stderr, "\n");
  150. }
  151. static int
  152. check_it(void)
  153. {
  154. int i;
  155. for (i = 0; i < N_ELEMENTS; i++)
  156. {
  157. float expected_value = i + 1.0;
  158. #if STARPU_USE_CUDA
  159. expected_value *= array_of_structs[i].y;
  160. #endif
  161. expected_value *= array_of_structs[i].y;
  162. if (array_of_structs[i].x != expected_value)
  163. return EXIT_FAILURE;
  164. }
  165. return EXIT_SUCCESS;
  166. }
  167. #ifdef STARPU_USE_OPENCL
  168. struct starpu_opencl_program opencl_program;
  169. struct starpu_opencl_program opencl_conversion_program;
  170. #endif
  171. int
  172. main(void)
  173. {
  174. #ifdef STARPU_USE_CPU
  175. starpu_init(NULL);
  176. #ifdef STARPU_USE_OPENCL
  177. starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_opencl_kernel.cl",
  178. &opencl_program, NULL);
  179. starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_conversion_codelets_opencl_kernel.cl",
  180. &opencl_conversion_program, NULL);
  181. #endif
  182. init_problem_data();
  183. print_it();
  184. register_data();
  185. create_and_submit_tasks();
  186. unregister_data();
  187. print_it();
  188. #ifdef STARPU_USE_OPENCL
  189. starpu_opencl_unload_opencl(&opencl_program);
  190. starpu_opencl_unload_opencl(&opencl_conversion_program);
  191. #endif
  192. starpu_shutdown();
  193. return check_it();
  194. #else
  195. /* Without the CPU, there is no point in using the multiformat
  196. * interface, so this test is pointless. */
  197. return EXIT_SUCCESS;
  198. #endif
  199. }