multiformat.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_codelet cl = {
  62. .where = STARPU_CUDA | STARPU_OPENCL,
  63. .cpu_funcs = {multiformat_scal_cpu_func, NULL},
  64. #ifdef STARPU_USE_CUDA
  65. .cuda_funcs = {multiformat_scal_cuda_func, NULL},
  66. #endif
  67. #ifdef STARPU_USE_OPENCL
  68. .opencl_funcs = {multiformat_scal_opencl_func, NULL},
  69. #endif
  70. .nbuffers = 1,
  71. .name = "codelet_real"
  72. };
  73. /*
  74. * Main functions
  75. */
  76. static void
  77. init_problem_data(void)
  78. {
  79. int i;
  80. for (i = 0; i < N_ELEMENTS; i++) {
  81. array_of_structs[i].x = 1.0 + i;
  82. array_of_structs[i].y = 42.0;
  83. }
  84. }
  85. static void
  86. register_data(void)
  87. {
  88. starpu_multiformat_data_register(&array_of_structs_handle,
  89. 0,
  90. &array_of_structs,
  91. N_ELEMENTS,
  92. &format_ops);
  93. }
  94. static void
  95. create_and_submit_tasks(void)
  96. {
  97. int err;
  98. #ifdef STARPU_USE_CUDA
  99. struct starpu_task *task = starpu_task_create();
  100. cl.where = STARPU_CUDA;
  101. task->cl = &cl;
  102. task->synchronous = 1;
  103. task->buffers[0].handle = array_of_structs_handle;
  104. task->buffers[0].mode = STARPU_RW;
  105. task->cl_arg = NULL;
  106. task->cl_arg_size = 0;
  107. err = starpu_task_submit(task);
  108. if (err != 0)
  109. {
  110. FPRINTF(stderr, "Err : %s\n", strerror(-err));
  111. return;
  112. }
  113. #endif
  114. struct starpu_task *task2 = starpu_task_create();
  115. cl.where = STARPU_CPU;
  116. task2->cl = &cl;
  117. task2->synchronous = 1;
  118. task2->buffers[0].handle = array_of_structs_handle;
  119. task2->buffers[0].mode = STARPU_RW;
  120. task2->cl_arg = NULL;
  121. cl.where = STARPU_CPU;
  122. task2->cl_arg_size = 0;
  123. err = starpu_task_submit(task2);
  124. if (err != 0)
  125. {
  126. FPRINTF(stderr, "Err : %s\n", strerror(-err));
  127. return;
  128. }
  129. }
  130. static void
  131. unregister_data(void)
  132. {
  133. starpu_data_unregister(array_of_structs_handle);
  134. }
  135. static void
  136. print_it(void)
  137. {
  138. int i;
  139. for (i = 0; i < N_ELEMENTS; i++) {
  140. FPRINTF(stderr, "(%.2f %.2f) ",
  141. array_of_structs[i].x,
  142. array_of_structs[i].y);
  143. }
  144. FPRINTF(stderr, "\n");
  145. }
  146. static int
  147. check_it(void)
  148. {
  149. int i;
  150. for (i = 0; i < N_ELEMENTS; i++) {
  151. float expected_value = i + 1.0;
  152. #if STARPU_USE_CUDA
  153. expected_value *= array_of_structs[i].y;
  154. #endif
  155. expected_value *= array_of_structs[i].y;
  156. if (array_of_structs[i].x != expected_value)
  157. return EXIT_FAILURE;
  158. }
  159. return EXIT_SUCCESS;
  160. }
  161. #ifdef STARPU_USE_OPENCL
  162. struct starpu_opencl_program opencl_program;
  163. struct starpu_opencl_program opencl_conversion_program;
  164. #endif
  165. int
  166. main(void)
  167. {
  168. #ifdef STARPU_USE_CPU
  169. starpu_init(NULL);
  170. #ifdef STARPU_USE_OPENCL
  171. starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_opencl_kernel.cl",
  172. &opencl_program, NULL);
  173. starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_conversion_codelets_opencl_kernel.cl",
  174. &opencl_conversion_program, NULL);
  175. #endif
  176. init_problem_data();
  177. print_it();
  178. register_data();
  179. create_and_submit_tasks();
  180. unregister_data();
  181. print_it();
  182. #ifdef STARPU_USE_OPENCL
  183. starpu_opencl_unload_opencl(&opencl_program);
  184. starpu_opencl_unload_opencl(&opencl_conversion_program);
  185. #endif
  186. starpu_shutdown();
  187. return check_it();
  188. #else
  189. /* Without the CPU, there is no point in using the multiformat
  190. * interface, so this test is pointless. */
  191. return EXIT_SUCCESS;
  192. #endif
  193. }