subgraph_repeat_regenerate.c 5.3 KB

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