complex.c 7.1 KB

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