data_implicit_deps.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 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. static struct starpu_codelet cl_g =
  70. {
  71. .modes = { STARPU_RW, STARPU_R, STARPU_RW },
  72. .cpu_funcs = {g},
  73. #ifdef STARPU_USE_CUDA
  74. .cuda_funcs = {g_cuda},
  75. #endif
  76. // TODO
  77. //.opencl_funcs = {g},
  78. .cpu_funcs_name = {"g"},
  79. .nbuffers = 3,
  80. };
  81. void h(void *descr[], void *arg)
  82. {
  83. (void)arg;
  84. STARPU_SKIP_IF_VALGRIND;
  85. unsigned *val = (unsigned *) STARPU_VARIABLE_GET_PTR(descr[0]);
  86. FPRINTF(stderr, "VAR %u (should be 42)\n", *val);
  87. STARPU_ASSERT(*val == 42);
  88. }
  89. #ifdef STARPU_USE_CUDA
  90. void h_cuda(void *descr[], void *arg)
  91. {
  92. (void)arg;
  93. STARPU_SKIP_IF_VALGRIND;
  94. unsigned *val = (unsigned *) STARPU_VARIABLE_GET_PTR(descr[0]);
  95. unsigned value;
  96. cudaMemcpyAsync(&value, val, sizeof(value), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  97. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  98. FPRINTF(stderr, "VAR %u (should be 42)\n", value);
  99. STARPU_ASSERT(value == 42);
  100. }
  101. #endif
  102. static struct starpu_codelet cl_h =
  103. {
  104. .modes = { STARPU_RW, STARPU_R, STARPU_RW },
  105. .cpu_funcs = {h},
  106. #ifdef STARPU_USE_CUDA
  107. .cuda_funcs = {h_cuda},
  108. #endif
  109. // TODO
  110. //.opencl_funcs = {h},
  111. .cpu_funcs_name = {"h"},
  112. .nbuffers = 3
  113. };
  114. int main(int argc, char **argv)
  115. {
  116. int ret;
  117. ret = starpu_initialize(NULL, &argc, &argv);
  118. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  119. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  120. A = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  121. B = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  122. C = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  123. D = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  124. starpu_vector_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A, VECTORSIZE, sizeof(unsigned));
  125. starpu_vector_data_register(&B_handle, STARPU_MAIN_RAM, (uintptr_t)B, VECTORSIZE, sizeof(unsigned));
  126. starpu_vector_data_register(&C_handle, STARPU_MAIN_RAM, (uintptr_t)C, VECTORSIZE, sizeof(unsigned));
  127. starpu_vector_data_register(&D_handle, STARPU_MAIN_RAM, (uintptr_t)D, VECTORSIZE, sizeof(unsigned));
  128. starpu_variable_data_register(&var_handle, STARPU_MAIN_RAM, (uintptr_t)(&var), sizeof(var));
  129. #if 0
  130. starpu_data_set_sequential_consistency_flag(A_handle, 0);
  131. starpu_data_set_sequential_consistency_flag(B_handle, 0);
  132. starpu_data_set_sequential_consistency_flag(C_handle, 0);
  133. starpu_data_set_sequential_consistency_flag(D_handle, 0);
  134. #endif
  135. /* f(Ar, Brw): sleep
  136. * g(Br; Crw); sleep, var = 42
  137. * h(Cr; Drw); check that var == 42
  138. */
  139. struct starpu_task *task_f = starpu_task_create();
  140. task_f->cl = &cl_f;
  141. task_f->handles[0] = var_handle;
  142. task_f->handles[1] = A_handle;
  143. task_f->handles[2] = B_handle;
  144. ret = starpu_task_submit(task_f);
  145. if (ret == -ENODEV) goto enodev;
  146. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  147. struct starpu_task *task_g = starpu_task_create();
  148. task_g->cl = &cl_g;
  149. task_g->handles[0] = var_handle;
  150. task_g->handles[1] = B_handle;
  151. task_g->handles[2] = C_handle;
  152. ret = starpu_task_submit(task_g);
  153. if (ret == -ENODEV) goto enodev;
  154. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  155. struct starpu_task *task_h = starpu_task_create();
  156. task_h->cl = &cl_h;
  157. task_h->handles[0] = var_handle;
  158. task_h->handles[1] = C_handle;
  159. task_h->handles[2] = D_handle;
  160. ret = starpu_task_submit(task_h);
  161. if (ret == -ENODEV) goto enodev;
  162. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  163. ret = starpu_task_wait_for_all();
  164. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  165. starpu_data_unregister(A_handle);
  166. starpu_data_unregister(B_handle);
  167. starpu_data_unregister(C_handle);
  168. starpu_data_unregister(D_handle);
  169. starpu_data_unregister(var_handle);
  170. free(A);
  171. free(B);
  172. free(C);
  173. free(D);
  174. starpu_shutdown();
  175. return EXIT_SUCCESS;
  176. enodev:
  177. starpu_data_unregister(A_handle);
  178. starpu_data_unregister(B_handle);
  179. starpu_data_unregister(C_handle);
  180. starpu_data_unregister(D_handle);
  181. starpu_data_unregister(var_handle);
  182. free(A);
  183. free(B);
  184. free(C);
  185. free(D);
  186. fprintf(stderr, "WARNING: No one can execute this task\n");
  187. /* yes, we do not perform the computation but we did detect that no one
  188. * could perform the kernel, so this is not an error from StarPU */
  189. starpu_shutdown();
  190. return STARPU_TEST_SKIPPED;
  191. }