complex.c 7.2 KB

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