data_implicit_deps.c 6.9 KB

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