subgraph_repeat_regenerate.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <common/thread.h>
  18. #include "increment_codelet.h"
  19. #include "../helper.h"
  20. /*
  21. * Test that one can let a whole task graph repeatedly regenerate itself
  22. */
  23. #ifdef STARPU_QUICK_CHECK
  24. static unsigned niter = 64;
  25. #else
  26. static unsigned niter = 16384;
  27. #endif
  28. /*
  29. *
  30. * /-->B--\
  31. * | |
  32. * -----> A D---\--->
  33. * ^ | | |
  34. * | \-->C--/ |
  35. * | |
  36. * \--------------/
  37. *
  38. * - {B, C} depend on A
  39. * - D depends on {B, C}
  40. * - A, B, C and D are resubmitted at the end of the loop (or not)
  41. */
  42. static struct starpu_task taskA, taskB, taskC, taskD;
  43. static unsigned loop_cntB = 0;
  44. static unsigned loop_cntC = 0;
  45. static unsigned loop_cntD = 0;
  46. static unsigned *check_cnt;
  47. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  48. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  49. extern void cuda_host_increment(void *descr[], void *_args);
  50. static void callback_task_B(void *arg)
  51. {
  52. (void)arg;
  53. if (++loop_cntB == niter)
  54. taskB.regenerate = 0;
  55. }
  56. static void callback_task_C(void *arg)
  57. {
  58. (void)arg;
  59. if (++loop_cntC == niter)
  60. taskC.regenerate = 0;
  61. }
  62. static void callback_task_D(void *arg)
  63. {
  64. (void)arg;
  65. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  66. loop_cntD++;
  67. if (loop_cntD == niter)
  68. {
  69. /* We are done */
  70. taskD.regenerate = 0;
  71. STARPU_PTHREAD_COND_SIGNAL(&cond);
  72. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  73. }
  74. else
  75. {
  76. int ret;
  77. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  78. /* Let's go for another iteration */
  79. ret = starpu_task_submit(&taskA);
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  81. }
  82. }
  83. int main(int argc, char **argv)
  84. {
  85. // unsigned i;
  86. // double timing;
  87. // double start;
  88. // double end;
  89. int ret;
  90. ret = starpu_initialize(NULL, &argc, &argv);
  91. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  92. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  93. /* Implicit data dependencies and regeneratable tasks are not compatible */
  94. starpu_data_set_default_sequential_consistency_flag(0);
  95. starpu_malloc((void**)&check_cnt, sizeof(*check_cnt));
  96. *check_cnt = 0;
  97. starpu_data_handle_t check_data;
  98. starpu_variable_data_register(&check_data, STARPU_MAIN_RAM, (uintptr_t)check_cnt, sizeof(*check_cnt));
  99. starpu_task_init(&taskA);
  100. taskA.cl = &increment_codelet;
  101. taskA.regenerate = 0; /* this task will be explicitely resubmitted if needed */
  102. taskA.handles[0] = check_data;
  103. starpu_task_init(&taskB);
  104. taskB.cl = &increment_codelet;
  105. taskB.callback_func = callback_task_B;
  106. taskB.regenerate = 1;
  107. taskB.handles[0] = check_data;
  108. starpu_task_init(&taskC);
  109. taskC.cl = &increment_codelet;
  110. taskC.callback_func = callback_task_C;
  111. taskC.regenerate = 1;
  112. taskC.handles[0] = check_data;
  113. starpu_task_init(&taskD);
  114. taskD.cl = &increment_codelet;
  115. taskD.callback_func = callback_task_D;
  116. taskD.regenerate = 1;
  117. taskD.handles[0] = check_data;
  118. starpu_task_declare_deps(&taskB, 1, &taskA);
  119. starpu_task_declare_deps(&taskC, 1, &taskA);
  120. starpu_task_declare_deps(&taskD, 2, &taskB, &taskC);
  121. ret = starpu_task_submit(&taskA); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  122. ret = starpu_task_submit(&taskB); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  123. ret = starpu_task_submit(&taskC); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  124. ret = starpu_task_submit(&taskD); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  125. starpu_do_schedule();
  126. /* Wait for the termination of all loops */
  127. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  128. while (loop_cntD < niter)
  129. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  130. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  131. starpu_data_acquire(check_data, STARPU_R);
  132. starpu_data_release(check_data);
  133. STARPU_ASSERT(*check_cnt == (4*niter));
  134. starpu_free_noflag(check_cnt, sizeof(*check_cnt));
  135. starpu_data_unregister(check_data);
  136. starpu_task_wait_for_all();
  137. starpu_task_clean(&taskA);
  138. starpu_task_clean(&taskB);
  139. starpu_task_clean(&taskC);
  140. starpu_task_clean(&taskD);
  141. starpu_shutdown();
  142. return EXIT_SUCCESS;
  143. enodev:
  144. fprintf(stderr, "WARNING: No one can execute this task\n");
  145. /* yes, we do not perform the computation but we did detect that no one
  146. * could perform the kernel, so this is not an error from StarPU */
  147. starpu_data_unregister(check_data);
  148. starpu_shutdown();
  149. return STARPU_TEST_SKIPPED;
  150. }