subgraph_repeat_regenerate.c 5.5 KB

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