multiformat.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. aos[i].x *= aos[i].y;
  32. }
  33. }
  34. #ifdef STARPU_USE_CUDA
  35. extern struct starpu_codelet cpu_to_cuda_cl;
  36. extern struct starpu_codelet cuda_to_cpu_cl;
  37. #endif
  38. #ifdef STARPU_USE_OPENCL
  39. extern struct starpu_codelet cpu_to_opencl_cl;
  40. extern struct starpu_codelet opencl_to_cpu_cl;
  41. #endif
  42. static struct starpu_multiformat_data_interface_ops format_ops = {
  43. #ifdef STARPU_USE_CUDA
  44. .cuda_elemsize = 2* sizeof(float),
  45. .cpu_to_cuda_cl = &cpu_to_cuda_cl,
  46. .cuda_to_cpu_cl = &cuda_to_cpu_cl,
  47. #endif
  48. #ifdef STARPU_USE_OPENCL
  49. .opencl_elemsize = 2 * sizeof(float),
  50. .cpu_to_opencl_cl = &cpu_to_opencl_cl,
  51. .opencl_to_cpu_cl = &opencl_to_cpu_cl,
  52. #endif
  53. .cpu_elemsize = sizeof(struct point),
  54. };
  55. #ifdef STARPU_USE_CUDA
  56. extern void multiformat_scal_cuda_func(void *buffers[], void *arg);
  57. #endif
  58. #ifdef STARPU_USE_OPENCL
  59. extern void multiformat_scal_opencl_func(void *buffers[], void *arg);
  60. #endif
  61. static struct starpu_perfmodel conversion_model = {
  62. .type = STARPU_HISTORY_BASED,
  63. .symbol = "multiformat_conversion_model"
  64. };
  65. static struct starpu_codelet cl = {
  66. .where = STARPU_CUDA | STARPU_OPENCL,
  67. .cpu_func = multiformat_scal_cpu_func,
  68. #ifdef STARPU_USE_CUDA
  69. .cuda_func = multiformat_scal_cuda_func,
  70. #endif
  71. #ifdef STARPU_USE_OPENCL
  72. .opencl_func = multiformat_scal_opencl_func,
  73. #endif
  74. .nbuffers = 1,
  75. .conversion_model = &conversion_model
  76. };
  77. /*
  78. * Main functions
  79. */
  80. static void
  81. init_problem_data(void)
  82. {
  83. int i;
  84. for (i = 0; i < N_ELEMENTS; i++) {
  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. fprintf(stderr, "(%.2f %.2f) ",
  145. array_of_structs[i].x,
  146. array_of_structs[i].y);
  147. }
  148. fprintf(stderr, "\n");
  149. }
  150. static int
  151. check_it(void)
  152. {
  153. int i;
  154. for (i = 0; i < N_ELEMENTS; i++) {
  155. float expected_value = i + 1.0;
  156. #if STARPU_USE_CUDA
  157. expected_value *= array_of_structs[i].y;
  158. #endif
  159. expected_value *= array_of_structs[i].y;
  160. if (array_of_structs[i].x != expected_value)
  161. return EXIT_FAILURE;
  162. }
  163. return EXIT_SUCCESS;
  164. }
  165. #ifdef STARPU_USE_OPENCL
  166. struct starpu_opencl_program opencl_program;
  167. struct starpu_opencl_program opencl_conversion_program;
  168. #endif
  169. int
  170. main(void)
  171. {
  172. #ifdef STARPU_USE_CPU
  173. starpu_init(NULL);
  174. #ifdef STARPU_USE_OPENCL
  175. starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_opencl_kernel.cl",
  176. &opencl_program, NULL);
  177. starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_conversion_codelets_opencl_kernel.cl",
  178. &opencl_conversion_program, NULL);
  179. #endif
  180. init_problem_data();
  181. print_it();
  182. register_data();
  183. create_and_submit_tasks();
  184. unregister_data();
  185. print_it();
  186. #ifdef STARPU_USE_OPENCL
  187. starpu_opencl_unload_opencl(&opencl_program);
  188. starpu_opencl_unload_opencl(&opencl_conversion_program);
  189. #endif
  190. starpu_shutdown();
  191. return check_it();
  192. #else
  193. /* Without the CPU, there is no point in using the multiformat
  194. * interface, so this test is pointless. */
  195. return EXIT_SUCCESS;
  196. #endif
  197. }