subgraph_repeat_regenerate.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  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 <starpu.h>
  18. #include <common/thread.h>
  19. #include "../helper.h"
  20. #ifdef STARPU_QUICK_CHECK
  21. static unsigned niter = 64;
  22. #else
  23. static unsigned niter = 16384;
  24. #endif
  25. /*
  26. *
  27. * /-->B--\
  28. * | |
  29. * -----> A D---\--->
  30. * ^ | | |
  31. * | \-->C--/ |
  32. * | |
  33. * \--------------/
  34. *
  35. * - {B, C} depend on A
  36. * - D depends on {B, C}
  37. * - A, B, C and D are resubmitted at the end of the loop (or not)
  38. */
  39. static struct starpu_task taskA, taskB, taskC, taskD;
  40. static unsigned loop_cntB = 0;
  41. static unsigned loop_cntC = 0;
  42. static unsigned loop_cntD = 0;
  43. static unsigned *check_cnt;
  44. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  45. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  46. extern void cuda_host_increment(void *descr[], void *_args);
  47. void cpu_increment(void *descr[], void *arg STARPU_ATTRIBUTE_UNUSED)
  48. {
  49. unsigned *var = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  50. (*var)++;
  51. }
  52. static struct starpu_codelet dummy_codelet =
  53. {
  54. .cpu_funcs = {cpu_increment},
  55. #ifdef STARPU_USE_CUDA
  56. .cuda_funcs = {cuda_host_increment},
  57. .cuda_flags = {STARPU_CUDA_ASYNC},
  58. #endif
  59. // TODO
  60. //.opencl_funcs = {dummy_func},
  61. .cpu_funcs_name = {"cpu_increment"},
  62. .model = NULL,
  63. .modes = { STARPU_RW },
  64. .nbuffers = 1
  65. };
  66. static void callback_task_B(void *arg STARPU_ATTRIBUTE_UNUSED)
  67. {
  68. if (++loop_cntB == niter)
  69. taskB.regenerate = 0;
  70. }
  71. static void callback_task_C(void *arg STARPU_ATTRIBUTE_UNUSED)
  72. {
  73. if (++loop_cntC == niter)
  74. taskC.regenerate = 0;
  75. }
  76. static void callback_task_D(void *arg STARPU_ATTRIBUTE_UNUSED)
  77. {
  78. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  79. loop_cntD++;
  80. if (loop_cntD == niter)
  81. {
  82. /* We are done */
  83. taskD.regenerate = 0;
  84. STARPU_PTHREAD_COND_SIGNAL(&cond);
  85. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  86. }
  87. else
  88. {
  89. int ret;
  90. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  91. /* Let's go for another iteration */
  92. ret = starpu_task_submit(&taskA);
  93. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  94. }
  95. }
  96. int main(int argc, char **argv)
  97. {
  98. // unsigned i;
  99. // double timing;
  100. // double start;
  101. // double end;
  102. int ret;
  103. ret = starpu_initialize(NULL, &argc, &argv);
  104. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  106. /* Implicit data dependencies and regeneratable tasks are not compatible */
  107. starpu_data_set_default_sequential_consistency_flag(0);
  108. starpu_malloc((void**)&check_cnt, sizeof(*check_cnt));
  109. *check_cnt = 0;
  110. starpu_data_handle_t check_data;
  111. starpu_variable_data_register(&check_data, STARPU_MAIN_RAM, (uintptr_t)check_cnt, sizeof(*check_cnt));
  112. starpu_task_init(&taskA);
  113. taskA.cl = &dummy_codelet;
  114. taskA.cl_arg = &taskA;
  115. taskA.cl_arg_size = sizeof(&taskA);
  116. taskA.regenerate = 0; /* this task will be explicitely resubmitted if needed */
  117. taskA.handles[0] = check_data;
  118. starpu_task_init(&taskB);
  119. taskB.cl = &dummy_codelet;
  120. taskB.cl_arg = &taskB;
  121. taskB.cl_arg_size = sizeof(&taskB);
  122. taskB.callback_func = callback_task_B;
  123. taskB.regenerate = 1;
  124. taskB.handles[0] = check_data;
  125. starpu_task_init(&taskC);
  126. taskC.cl = &dummy_codelet;
  127. taskC.cl_arg = &taskC;
  128. taskC.cl_arg_size = sizeof(&taskC);
  129. taskC.callback_func = callback_task_C;
  130. taskC.regenerate = 1;
  131. taskC.handles[0] = check_data;
  132. starpu_task_init(&taskD);
  133. taskD.cl = &dummy_codelet;
  134. taskD.cl_arg = &taskD;
  135. taskD.cl_arg_size = sizeof(&taskD);
  136. taskD.callback_func = callback_task_D;
  137. taskD.regenerate = 1;
  138. taskD.handles[0] = check_data;
  139. struct starpu_task *depsBC_array[1] = {&taskA};
  140. starpu_task_declare_deps_array(&taskB, 1, depsBC_array);
  141. starpu_task_declare_deps_array(&taskC, 1, depsBC_array);
  142. struct starpu_task *depsD_array[2] = {&taskB, &taskC};
  143. starpu_task_declare_deps_array(&taskD, 2, depsD_array);
  144. ret = starpu_task_submit(&taskA); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  145. ret = starpu_task_submit(&taskB); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  146. ret = starpu_task_submit(&taskC); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  147. ret = starpu_task_submit(&taskD); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  148. /* Wait for the termination of all loops */
  149. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  150. while (loop_cntD < niter)
  151. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  152. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  153. starpu_data_acquire(check_data, STARPU_R);
  154. starpu_data_release(check_data);
  155. STARPU_ASSERT(*check_cnt == (4*niter));
  156. starpu_free(check_cnt);
  157. starpu_data_unregister(check_data);
  158. starpu_shutdown();
  159. /* Cleanup the statically allocated tasks after shutdown, as StarPU is still working on it after the callback */
  160. starpu_task_clean(&taskA);
  161. starpu_task_clean(&taskB);
  162. starpu_task_clean(&taskC);
  163. starpu_task_clean(&taskD);
  164. return EXIT_SUCCESS;
  165. enodev:
  166. fprintf(stderr, "WARNING: No one can execute this task\n");
  167. /* yes, we do not perform the computation but we did detect that no one
  168. * could perform the kernel, so this is not an error from StarPU */
  169. starpu_data_unregister(check_data);
  170. starpu_shutdown();
  171. return STARPU_TEST_SKIPPED;
  172. }