complex.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1")+1, STARPU_R, handle1, 0);
  114. if (ret == -ENODEV) goto end;
  115. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  116. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle2", strlen("handle2")+1, STARPU_R, handle2, 0);
  117. if (ret == -ENODEV) goto end;
  118. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  119. /* Compare two different complexs. */
  120. ret = starpu_task_insert(&cl_compare,
  121. STARPU_R, handle1,
  122. STARPU_R, handle2,
  123. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  124. 0);
  125. if (ret == -ENODEV) goto end;
  126. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  127. starpu_task_wait_for_all();
  128. if (compare != 0)
  129. {
  130. FPRINTF(stderr, "Complex numbers should NOT be similar\n");
  131. goto end;
  132. }
  133. /* Copy one into the other. */
  134. ret = starpu_task_insert(&cl_copy,
  135. STARPU_R, handle1,
  136. STARPU_W, handle2,
  137. 0);
  138. if (ret == -ENODEV) goto end;
  139. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  140. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1")+1, STARPU_R, handle1, 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, "handle2", strlen("handle2")+1, STARPU_R, handle2, 0);
  144. if (ret == -ENODEV) goto end;
  145. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  146. /* And compare again. */
  147. ret = starpu_task_insert(&cl_compare,
  148. STARPU_R, handle1,
  149. STARPU_R, handle2,
  150. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  151. 0);
  152. if (ret == -ENODEV) goto end;
  153. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  154. starpu_task_wait_for_all();
  155. if (compare != 1)
  156. {
  157. FPRINTF(stderr, "Complex numbers should be similar\n");
  158. }
  159. /* Put another value again */
  160. starpu_data_acquire(handle2, STARPU_W);
  161. copy_real = 78.0;
  162. copy_imaginary = 77.0;
  163. starpu_data_release(handle2);
  164. /* Create a vector of two complexs. */
  165. starpu_complex_data_register(&handle3, -1, 0, 0, 2);
  166. /* Split it in two pieces (thus one complex each). */
  167. struct starpu_data_filter f =
  168. {
  169. .filter_func = starpu_complex_filter_block,
  170. .nchildren = 2,
  171. };
  172. starpu_data_partition(handle3, &f);
  173. /* Copy the two complexs into each part */
  174. ret = starpu_task_insert(&cl_copy,
  175. STARPU_R, handle1,
  176. STARPU_W, starpu_data_get_sub_data(handle3, 1, 0),
  177. 0);
  178. if (ret == -ENODEV) goto end;
  179. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  180. ret = starpu_task_insert(&cl_copy,
  181. STARPU_R, handle2,
  182. STARPU_W, starpu_data_get_sub_data(handle3, 1, 1),
  183. 0);
  184. if (ret == -ENODEV) goto end;
  185. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  186. /* Gather the two pieces. */
  187. starpu_data_unpartition(handle3, STARPU_MAIN_RAM);
  188. /* Show it. */
  189. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle3", strlen("handle3")+1, STARPU_R, handle3, 0);
  190. /* Get the real and imaginary vectors. */
  191. struct starpu_data_filter fcanon =
  192. {
  193. .filter_func = starpu_complex_filter_canonical,
  194. .nchildren = 2,
  195. .get_child_ops = starpu_complex_filter_canonical_child_ops,
  196. };
  197. starpu_data_partition(handle3, &fcanon);
  198. /* Check the corresponding data. */
  199. vectorh = starpu_data_get_sub_data(handle3, 1, 0);
  200. starpu_data_acquire(vectorh, STARPU_R);
  201. vectori = starpu_data_get_interface_on_node(vectorh, STARPU_MAIN_RAM);
  202. vector = (double*) vectori->ptr;
  203. STARPU_ASSERT_MSG(vector[0] == 45., "Bogus value: %f instead of %f", vector[0], 45.);
  204. STARPU_ASSERT_MSG(vector[1] == 78., "Bogus value: %f instead of %f", vector[1], 78.);
  205. starpu_data_release(vectorh);
  206. vectorh = starpu_data_get_sub_data(handle3, 1, 1);
  207. starpu_data_acquire(vectorh, STARPU_R);
  208. vectori = starpu_data_get_interface_on_node(vectorh, STARPU_MAIN_RAM);
  209. vector = (double*) vectori->ptr;
  210. STARPU_ASSERT_MSG(vector[0] == 12., "Bogus value: %f instead of %f", vector[0], 12.);
  211. STARPU_ASSERT_MSG(vector[1] == 77., "Bogus value: %f instead of %f", vector[1], 77.);
  212. starpu_data_release(vectorh);
  213. starpu_data_unpartition(handle3, STARPU_MAIN_RAM);
  214. /* Use helper starpu_data_cpy */
  215. starpu_complex_data_register(&handle4, -1, 0, 0, 1);
  216. starpu_data_cpy(handle4, handle1, 0, NULL, NULL);
  217. ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle4", strlen("handle4")+1, STARPU_R, handle4, 0);
  218. if (ret == -ENODEV) goto end;
  219. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  220. /* Compare two different complexs. */
  221. ret = starpu_task_insert(&cl_compare,
  222. STARPU_R, handle1,
  223. STARPU_R, handle4,
  224. STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
  225. 0);
  226. if (ret == -ENODEV) goto end;
  227. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  228. starpu_task_wait_for_all();
  229. if (compare != 1)
  230. {
  231. FPRINTF(stderr, "Complex numbers should be similar\n");
  232. goto end;
  233. }
  234. end:
  235. #ifdef STARPU_USE_OPENCL
  236. {
  237. int ret2 = starpu_opencl_unload_opencl(&opencl_program);
  238. STARPU_CHECK_RETURN_VALUE(ret2, "starpu_opencl_unload_opencl");
  239. }
  240. #endif
  241. starpu_data_unregister(handle1);
  242. starpu_data_unregister(handle2);
  243. starpu_data_unregister(handle3);
  244. starpu_data_unregister(handle4);
  245. starpu_shutdown();
  246. if (ret == -ENODEV) return 77; else return !compare;
  247. }