data_implicit_deps.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2013, 2015-2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <config.h>
  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[] STARPU_ATTRIBUTE_UNUSED, void *_args STARPU_ATTRIBUTE_UNUSED)
  33. {
  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[], STARPU_ATTRIBUTE_UNUSED void *_args)
  47. {
  48. STARPU_SKIP_IF_VALGRIND;
  49. unsigned *val = (unsigned *) STARPU_VARIABLE_GET_PTR(descr[0]);
  50. usleep(100000);
  51. *val = 42;
  52. }
  53. #ifdef STARPU_USE_CUDA
  54. void g_cuda(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  55. {
  56. STARPU_SKIP_IF_VALGRIND;
  57. unsigned *val = (unsigned *) STARPU_VARIABLE_GET_PTR(descr[0]);
  58. unsigned value = 42;
  59. usleep(100000);
  60. cudaMemcpy(val, &value, sizeof(value), cudaMemcpyHostToDevice);
  61. }
  62. #endif
  63. static struct starpu_codelet cl_g =
  64. {
  65. .modes = { STARPU_RW, STARPU_R, STARPU_RW },
  66. .cpu_funcs = {g},
  67. #ifdef STARPU_USE_CUDA
  68. .cuda_funcs = {g_cuda},
  69. #endif
  70. // TODO
  71. //.opencl_funcs = {g},
  72. .cpu_funcs_name = {"g"},
  73. .nbuffers = 3,
  74. };
  75. void h(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  76. {
  77. STARPU_SKIP_IF_VALGRIND;
  78. unsigned *val = (unsigned *) STARPU_VARIABLE_GET_PTR(descr[0]);
  79. FPRINTF(stderr, "VAR %u (should be 42)\n", *val);
  80. STARPU_ASSERT(*val == 42);
  81. }
  82. #ifdef STARPU_USE_CUDA
  83. void h_cuda(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  84. {
  85. STARPU_SKIP_IF_VALGRIND;
  86. unsigned *val = (unsigned *) STARPU_VARIABLE_GET_PTR(descr[0]);
  87. unsigned value;
  88. cudaMemcpy(&value, val, sizeof(value), cudaMemcpyDeviceToHost);
  89. FPRINTF(stderr, "VAR %u (should be 42)\n", value);
  90. STARPU_ASSERT(value == 42);
  91. }
  92. #endif
  93. static struct starpu_codelet cl_h =
  94. {
  95. .modes = { STARPU_RW, STARPU_R, STARPU_RW },
  96. .cpu_funcs = {h},
  97. #ifdef STARPU_USE_CUDA
  98. .cuda_funcs = {h_cuda},
  99. #endif
  100. // TODO
  101. //.opencl_funcs = {h},
  102. .cpu_funcs_name = {"h"},
  103. .nbuffers = 3
  104. };
  105. int main(int argc, char **argv)
  106. {
  107. int ret;
  108. ret = starpu_initialize(NULL, &argc, &argv);
  109. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  110. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  111. A = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  112. B = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  113. C = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  114. D = (unsigned *) malloc(VECTORSIZE*sizeof(unsigned));
  115. starpu_vector_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A, VECTORSIZE, sizeof(unsigned));
  116. starpu_vector_data_register(&B_handle, STARPU_MAIN_RAM, (uintptr_t)B, VECTORSIZE, sizeof(unsigned));
  117. starpu_vector_data_register(&C_handle, STARPU_MAIN_RAM, (uintptr_t)C, VECTORSIZE, sizeof(unsigned));
  118. starpu_vector_data_register(&D_handle, STARPU_MAIN_RAM, (uintptr_t)D, VECTORSIZE, sizeof(unsigned));
  119. starpu_variable_data_register(&var_handle, STARPU_MAIN_RAM, (uintptr_t)(&var), sizeof(var));
  120. #if 0
  121. starpu_data_set_sequential_consistency_flag(A_handle, 0);
  122. starpu_data_set_sequential_consistency_flag(B_handle, 0);
  123. starpu_data_set_sequential_consistency_flag(C_handle, 0);
  124. starpu_data_set_sequential_consistency_flag(D_handle, 0);
  125. #endif
  126. /* f(Ar, Brw): sleep
  127. * g(Br; Crw); sleep, var = 42
  128. * h(Cr; Drw); check that var == 42
  129. */
  130. struct starpu_task *task_f = starpu_task_create();
  131. task_f->cl = &cl_f;
  132. task_f->handles[0] = var_handle;
  133. task_f->handles[1] = A_handle;
  134. task_f->handles[2] = B_handle;
  135. ret = starpu_task_submit(task_f);
  136. if (ret == -ENODEV) goto enodev;
  137. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  138. struct starpu_task *task_g = starpu_task_create();
  139. task_g->cl = &cl_g;
  140. task_g->handles[0] = var_handle;
  141. task_g->handles[1] = B_handle;
  142. task_g->handles[2] = C_handle;
  143. ret = starpu_task_submit(task_g);
  144. if (ret == -ENODEV) goto enodev;
  145. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  146. struct starpu_task *task_h = starpu_task_create();
  147. task_h->cl = &cl_h;
  148. task_h->handles[0] = var_handle;
  149. task_h->handles[1] = C_handle;
  150. task_h->handles[2] = D_handle;
  151. ret = starpu_task_submit(task_h);
  152. if (ret == -ENODEV) goto enodev;
  153. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  154. ret = starpu_task_wait_for_all();
  155. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  156. starpu_data_unregister(A_handle);
  157. starpu_data_unregister(B_handle);
  158. starpu_data_unregister(C_handle);
  159. starpu_data_unregister(D_handle);
  160. starpu_data_unregister(var_handle);
  161. free(A);
  162. free(B);
  163. free(C);
  164. free(D);
  165. starpu_shutdown();
  166. return EXIT_SUCCESS;
  167. enodev:
  168. starpu_data_unregister(A_handle);
  169. starpu_data_unregister(B_handle);
  170. starpu_data_unregister(C_handle);
  171. starpu_data_unregister(D_handle);
  172. starpu_data_unregister(var_handle);
  173. free(A);
  174. free(B);
  175. free(C);
  176. free(D);
  177. fprintf(stderr, "WARNING: No one can execute this task\n");
  178. /* yes, we do not perform the computation but we did detect that no one
  179. * could perform the kernel, so this is not an error from StarPU */
  180. starpu_shutdown();
  181. return STARPU_TEST_SKIPPED;
  182. }