complex.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), 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 "complex_interface.h"
  18. #include "complex_codelet.h"
  19. void copy_complex_codelet_cpu(void *descr[], void *_args)
  20. {
  21. int i;
  22. int nx = STARPU_COMPLEX_GET_NX(descr[0]);
  23. double *i_real = STARPU_COMPLEX_GET_REAL(descr[0]);
  24. double *i_imaginary = STARPU_COMPLEX_GET_IMAGINARY(descr[0]);
  25. double *o_real = STARPU_COMPLEX_GET_REAL(descr[1]);
  26. double *o_imaginary = STARPU_COMPLEX_GET_IMAGINARY(descr[1]);
  27. for(i=0 ; i<nx ; i++)
  28. {
  29. o_real[i] = i_real[i];
  30. o_imaginary[i] = i_imaginary[i];
  31. }
  32. }
  33. static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
  34. {
  35. (void) task;
  36. (void) nimpl;
  37. if (starpu_worker_get_type(workerid) == STARPU_OPENCL_WORKER)
  38. return 1;
  39. #ifdef STARPU_USE_CUDA
  40. #ifdef STARPU_SIMGRID
  41. /* We don't know, let's assume it can */
  42. return 1;
  43. #else
  44. /* Cuda device */
  45. const struct cudaDeviceProp *props;
  46. props = starpu_cuda_get_device_properties(workerid);
  47. if (props->major >= 2 || props->minor >= 3)
  48. {
  49. /* At least compute capability 1.3, supports doubles */
  50. return 1;
  51. }
  52. else
  53. {
  54. /* Old card does not support doubles */
  55. return 0;
  56. }
  57. #endif
  58. #else
  59. return 1;
  60. #endif
  61. }
  62. #ifdef STARPU_USE_CUDA
  63. extern void copy_complex_codelet_cuda(void *descr[], void *_args);
  64. #endif
  65. #ifdef STARPU_USE_OPENCL
  66. extern void copy_complex_codelet_opencl(void *buffers[], void *args);
  67. #endif
  68. struct starpu_codelet cl_copy =
  69. {
  70. .cpu_funcs = {copy_complex_codelet_cpu},
  71. #ifdef STARPU_USE_CUDA
  72. .cuda_funcs = {copy_complex_codelet_cuda},
  73. .cuda_flags = {STARPU_CUDA_ASYNC},
  74. #endif
  75. #ifdef STARPU_USE_OPENCL
  76. .opencl_funcs = {copy_complex_codelet_opencl},
  77. .opencl_flags = {STARPU_OPENCL_ASYNC},
  78. #endif
  79. .nbuffers = 2,
  80. .modes = {STARPU_R, STARPU_W},
  81. .can_execute = can_execute,
  82. .name = "cl_copy"
  83. };
  84. #ifdef STARPU_USE_OPENCL
  85. struct starpu_opencl_program opencl_program;
  86. #endif
  87. int main(void)
  88. {
  89. int ret = 0;
  90. starpu_data_handle_t handle1;
  91. starpu_data_handle_t handle2;
  92. starpu_data_handle_t handle3;
  93. starpu_data_handle_t handle4;
  94. double real = 45.0;
  95. double imaginary = 12.0;
  96. double copy_real = 78.0;
  97. double copy_imaginary = 78.0;
  98. int compare;
  99. int *compare_ptr = &compare;
  100. starpu_data_handle_t vectorh;
  101. struct starpu_vector_interface *vectori;
  102. double *vector;
  103. ret = starpu_init(NULL);
  104. if (ret == -ENODEV) return 77;
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  106. #ifdef STARPU_USE_OPENCL
  107. ret = starpu_opencl_load_opencl_from_file("examples/interface/complex_kernels.cl",
  108. &opencl_program, NULL);
  109. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  110. #endif
  111. starpu_complex_data_register(&handle1, STARPU_MAIN_RAM, &real, &imaginary, 1);
  112. starpu_complex_data_register(&handle2, STARPU_MAIN_RAM, &copy_real, &copy_imaginary, 1);
  113. /* Create a vector of two complexs. */
  114. starpu_complex_data_register(&handle3, -1, 0, 0, 2);
  115. starpu_complex_data_register(&handle4, -1, 0, 0, 1);
  116. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1")+1, STARPU_R, handle1, 0);
  117. if (ret == -ENODEV) goto end;
  118. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  119. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle2", strlen("handle2")+1, STARPU_R, handle2, 0);
  120. if (ret == -ENODEV) goto end;
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  122. /* Compare two different complexs. */
  123. ret = starpu_task_insert(&cl_compare,
  124. STARPU_R, handle1,
  125. STARPU_R, handle2,
  126. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  127. 0);
  128. if (ret == -ENODEV) goto end;
  129. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  130. starpu_task_wait_for_all();
  131. if (compare != 0)
  132. {
  133. FPRINTF(stderr, "Complex numbers should NOT be similar\n");
  134. goto end;
  135. }
  136. /* Copy one into the other. */
  137. ret = starpu_task_insert(&cl_copy,
  138. STARPU_R, handle1,
  139. STARPU_W, handle2,
  140. 0);
  141. if (ret == -ENODEV) goto end;
  142. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  143. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1")+1, STARPU_R, handle1, 0);
  144. if (ret == -ENODEV) goto end;
  145. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  146. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle2", strlen("handle2")+1, STARPU_R, handle2, 0);
  147. if (ret == -ENODEV) goto end;
  148. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  149. /* And compare again. */
  150. ret = starpu_task_insert(&cl_compare,
  151. STARPU_R, handle1,
  152. STARPU_R, handle2,
  153. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  154. 0);
  155. if (ret == -ENODEV) goto end;
  156. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  157. starpu_task_wait_for_all();
  158. if (compare != 1)
  159. {
  160. FPRINTF(stderr, "Complex numbers should be similar\n");
  161. }
  162. /* Put another value again */
  163. starpu_data_acquire(handle2, STARPU_W);
  164. copy_real = 78.0;
  165. copy_imaginary = 77.0;
  166. starpu_data_release(handle2);
  167. /* Split it in two pieces (thus one complex each). */
  168. struct starpu_data_filter f =
  169. {
  170. .filter_func = starpu_complex_filter_block,
  171. .nchildren = 2,
  172. };
  173. starpu_data_partition(handle3, &f);
  174. /* Copy the two complexs into each part */
  175. ret = starpu_task_insert(&cl_copy,
  176. STARPU_R, handle1,
  177. STARPU_W, starpu_data_get_sub_data(handle3, 1, 0),
  178. 0);
  179. if (ret == -ENODEV) goto end;
  180. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  181. ret = starpu_task_insert(&cl_copy,
  182. STARPU_R, handle2,
  183. STARPU_W, starpu_data_get_sub_data(handle3, 1, 1),
  184. 0);
  185. if (ret == -ENODEV) goto end;
  186. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  187. /* Gather the two pieces. */
  188. starpu_data_unpartition(handle3, STARPU_MAIN_RAM);
  189. /* Show it. */
  190. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle3", strlen("handle3")+1, STARPU_R, handle3, 0);
  191. if (ret == -ENODEV) goto end;
  192. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  193. /* Get the real and imaginary vectors. */
  194. struct starpu_data_filter fcanon =
  195. {
  196. .filter_func = starpu_complex_filter_canonical,
  197. .nchildren = 2,
  198. .get_child_ops = starpu_complex_filter_canonical_child_ops,
  199. };
  200. starpu_data_partition(handle3, &fcanon);
  201. /* Check the corresponding data. */
  202. vectorh = starpu_data_get_sub_data(handle3, 1, 0);
  203. starpu_data_acquire(vectorh, STARPU_R);
  204. vectori = starpu_data_get_interface_on_node(vectorh, STARPU_MAIN_RAM);
  205. vector = (double*) vectori->ptr;
  206. STARPU_ASSERT_MSG(vector[0] == 45., "Bogus value: %f instead of %f", vector[0], 45.);
  207. STARPU_ASSERT_MSG(vector[1] == 78., "Bogus value: %f instead of %f", vector[1], 78.);
  208. starpu_data_release(vectorh);
  209. vectorh = starpu_data_get_sub_data(handle3, 1, 1);
  210. starpu_data_acquire(vectorh, STARPU_R);
  211. vectori = starpu_data_get_interface_on_node(vectorh, STARPU_MAIN_RAM);
  212. vector = (double*) vectori->ptr;
  213. STARPU_ASSERT_MSG(vector[0] == 12., "Bogus value: %f instead of %f", vector[0], 12.);
  214. STARPU_ASSERT_MSG(vector[1] == 77., "Bogus value: %f instead of %f", vector[1], 77.);
  215. starpu_data_release(vectorh);
  216. starpu_data_unpartition(handle3, STARPU_MAIN_RAM);
  217. /* Use helper starpu_data_cpy */
  218. starpu_data_cpy(handle4, handle1, 0, NULL, NULL);
  219. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle4", strlen("handle4")+1, STARPU_R, handle4, 0);
  220. if (ret == -ENODEV) goto end;
  221. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  222. /* Compare two different complexs. */
  223. ret = starpu_task_insert(&cl_compare,
  224. STARPU_R, handle1,
  225. STARPU_R, handle4,
  226. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  227. 0);
  228. if (ret == -ENODEV) goto end;
  229. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  230. starpu_task_wait_for_all();
  231. if (compare != 1)
  232. {
  233. FPRINTF(stderr, "Complex numbers should be similar\n");
  234. goto end;
  235. }
  236. end:
  237. #ifdef STARPU_USE_OPENCL
  238. {
  239. int ret2 = starpu_opencl_unload_opencl(&opencl_program);
  240. STARPU_CHECK_RETURN_VALUE(ret2, "starpu_opencl_unload_opencl");
  241. }
  242. #endif
  243. starpu_data_unregister(handle1);
  244. starpu_data_unregister(handle2);
  245. starpu_data_unregister(handle3);
  246. starpu_data_unregister(handle4);
  247. starpu_shutdown();
  248. if (ret == -ENODEV) return 77; else return !compare;
  249. }