data_implicit_deps.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 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 <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. #include "../helper.h"
  22. /*
  23. * Test that implicit dependencies get properly computed
  24. */
  25. #define VECTORSIZE 1024
  26. static unsigned *A, *B, *C, *D;
  27. starpu_data_handle_t A_handle, B_handle, C_handle, D_handle;
  28. static unsigned var = 0;
  29. starpu_data_handle_t var_handle;
  30. void f(void *descr[], void *arg)
  31. {
  32. (void)descr;
  33. (void)arg;
  34. STARPU_SKIP_IF_VALGRIND;
  35. usleep(200000);
  36. }
  37. static struct starpu_codelet cl_f =
  38. {
  39. .modes = { STARPU_RW, STARPU_R, STARPU_RW },
  40. .cpu_funcs = {f},
  41. .cuda_funcs = {f},
  42. .opencl_funcs = {f},
  43. .cpu_funcs_name = {"f"},
  44. .nbuffers = 3,
  45. };
  46. void g(void *descr[], void *arg)
  47. {
  48. (void)descr;
  49. (void)arg;
  50. STARPU_SKIP_IF_VALGRIND;
  51. unsigned *val = (unsigned *) STARPU_VARIABLE_GET_PTR(descr[0]);
  52. usleep(100000);
  53. *val = 42;
  54. }
  55. #ifdef STARPU_USE_CUDA
  56. void g_cuda(void *descr[], void *arg)
  57. {
  58. (void)arg;
  59. STARPU_SKIP_IF_VALGRIND;
  60. unsigned *val = (unsigned *) STARPU_VARIABLE_GET_PTR(descr[0]);
  61. unsigned value = 42;
  62. usleep(100000);
  63. cudaMemcpyAsync(val, &value, sizeof(value), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  64. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  65. }
  66. #endif
  67. #ifdef STARPU_USE_OPENCL
  68. void g_opencl(void *descr[], void *arg)
  69. {
  70. (void)arg;
  71. STARPU_SKIP_IF_VALGRIND;
  72. cl_mem val = (cl_mem) STARPU_VARIABLE_GET_PTR(descr[0]);
  73. unsigned value = 42;
  74. usleep(100000);
  75. cl_command_queue queue;
  76. starpu_opencl_get_current_queue(&queue);
  77. clEnqueueWriteBuffer(queue, val, CL_TRUE, 0, sizeof(unsigned), (void *)&value, 0, NULL, NULL);
  78. clFinish(queue);
  79. }
  80. #endif
  81. static struct starpu_codelet cl_g =
  82. {
  83. .modes = { STARPU_RW, STARPU_R, STARPU_RW },
  84. .cpu_funcs = {g},
  85. #ifdef STARPU_USE_CUDA
  86. .cuda_funcs = {g_cuda},
  87. #endif
  88. #ifdef STARPU_USE_OPENCL
  89. .opencl_funcs = {g_opencl},
  90. #endif
  91. .cpu_funcs_name = {"g"},
  92. .nbuffers = 3,
  93. };
  94. void h(void *descr[], void *arg)
  95. {
  96. (void)arg;
  97. STARPU_SKIP_IF_VALGRIND;
  98. unsigned *val = (unsigned *) STARPU_VARIABLE_GET_PTR(descr[0]);
  99. FPRINTF(stderr, "VAR %u (should be 42)\n", *val);
  100. STARPU_ASSERT(*val == 42);
  101. }
  102. #ifdef STARPU_USE_CUDA
  103. void h_cuda(void *descr[], void *arg)
  104. {
  105. (void)arg;
  106. STARPU_SKIP_IF_VALGRIND;
  107. unsigned *val = (unsigned *) STARPU_VARIABLE_GET_PTR(descr[0]);
  108. unsigned value;
  109. cudaMemcpyAsync(&value, val, sizeof(value), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  110. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  111. FPRINTF(stderr, "VAR %u (should be 42)\n", value);
  112. STARPU_ASSERT(value == 42);
  113. }
  114. #endif
  115. #ifdef STARPU_USE_OPENCL
  116. void h_opencl(void *descr[], void *arg)
  117. {
  118. (void)arg;
  119. STARPU_SKIP_IF_VALGRIND;
  120. cl_mem val = (cl_mem) STARPU_VARIABLE_GET_PTR(descr[0]);
  121. unsigned value = 0;
  122. cl_command_queue queue;
  123. starpu_opencl_get_current_queue(&queue);
  124. clEnqueueReadBuffer(queue, val, CL_TRUE, 0, sizeof(unsigned), (void *)&value, 0, NULL, NULL);
  125. clFinish(queue);
  126. FPRINTF(stderr, "VAR %u (should be 42)\n", value);
  127. STARPU_ASSERT(value == 42);
  128. }
  129. #endif
  130. static struct starpu_codelet cl_h =
  131. {
  132. .modes = { STARPU_RW, STARPU_R, STARPU_RW },
  133. .cpu_funcs = {h},
  134. #ifdef STARPU_USE_CUDA
  135. .cuda_funcs = {h_cuda},
  136. #endif
  137. #ifdef STARPU_USE_OPENCL
  138. .opencl_funcs = {h_opencl},
  139. #endif
  140. .cpu_funcs_name = {"h"},
  141. .nbuffers = 3
  142. };
  143. int main(int argc, char **argv)
  144. {
  145. int ret;
  146. ret = starpu_initialize(NULL, &argc, &argv);
  147. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  148. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  149. A = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  150. B = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  151. C = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  152. D = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  153. starpu_vector_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A, VECTORSIZE, sizeof(unsigned));
  154. starpu_vector_data_register(&B_handle, STARPU_MAIN_RAM, (uintptr_t)B, VECTORSIZE, sizeof(unsigned));
  155. starpu_vector_data_register(&C_handle, STARPU_MAIN_RAM, (uintptr_t)C, VECTORSIZE, sizeof(unsigned));
  156. starpu_vector_data_register(&D_handle, STARPU_MAIN_RAM, (uintptr_t)D, VECTORSIZE, sizeof(unsigned));
  157. starpu_variable_data_register(&var_handle, STARPU_MAIN_RAM, (uintptr_t)(&var), sizeof(var));
  158. #if 0
  159. starpu_data_set_sequential_consistency_flag(A_handle, 0);
  160. starpu_data_set_sequential_consistency_flag(B_handle, 0);
  161. starpu_data_set_sequential_consistency_flag(C_handle, 0);
  162. starpu_data_set_sequential_consistency_flag(D_handle, 0);
  163. #endif
  164. /* f(Ar, Brw): sleep
  165. * g(Br; Crw); sleep, var = 42
  166. * h(Cr; Drw); check that var == 42
  167. */
  168. struct starpu_task *task_f = starpu_task_create();
  169. task_f->cl = &cl_f;
  170. task_f->handles[0] = var_handle;
  171. task_f->handles[1] = A_handle;
  172. task_f->handles[2] = B_handle;
  173. ret = starpu_task_submit(task_f);
  174. if (ret == -ENODEV) goto enodev;
  175. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  176. struct starpu_task *task_g = starpu_task_create();
  177. task_g->cl = &cl_g;
  178. task_g->handles[0] = var_handle;
  179. task_g->handles[1] = B_handle;
  180. task_g->handles[2] = C_handle;
  181. ret = starpu_task_submit(task_g);
  182. if (ret == -ENODEV) goto enodev;
  183. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  184. struct starpu_task *task_h = starpu_task_create();
  185. task_h->cl = &cl_h;
  186. task_h->handles[0] = var_handle;
  187. task_h->handles[1] = C_handle;
  188. task_h->handles[2] = D_handle;
  189. ret = starpu_task_submit(task_h);
  190. if (ret == -ENODEV) goto enodev;
  191. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  192. ret = starpu_task_wait_for_all();
  193. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  194. starpu_data_unregister(A_handle);
  195. starpu_data_unregister(B_handle);
  196. starpu_data_unregister(C_handle);
  197. starpu_data_unregister(D_handle);
  198. starpu_data_unregister(var_handle);
  199. free(A);
  200. free(B);
  201. free(C);
  202. free(D);
  203. starpu_shutdown();
  204. return EXIT_SUCCESS;
  205. enodev:
  206. starpu_data_unregister(A_handle);
  207. starpu_data_unregister(B_handle);
  208. starpu_data_unregister(C_handle);
  209. starpu_data_unregister(D_handle);
  210. starpu_data_unregister(var_handle);
  211. free(A);
  212. free(B);
  213. free(C);
  214. free(D);
  215. fprintf(stderr, "WARNING: No one can execute this task\n");
  216. /* yes, we do not perform the computation but we did detect that no one
  217. * could perform the kernel, so this is not an error from StarPU */
  218. starpu_shutdown();
  219. return STARPU_TEST_SKIPPED;
  220. }