multiformat.c 5.0 KB

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