data_implicit_deps.c 5.8 KB

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