subgraph_repeat_regenerate.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-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 <starpu.h>
  18. #include <common/thread.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. void cpu_increment(void *descr[], void *arg STARPU_ATTRIBUTE_UNUSED)
  51. {
  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 STARPU_ATTRIBUTE_UNUSED)
  70. {
  71. if (++loop_cntB == niter)
  72. taskB.regenerate = 0;
  73. }
  74. static void callback_task_C(void *arg STARPU_ATTRIBUTE_UNUSED)
  75. {
  76. if (++loop_cntC == niter)
  77. taskC.regenerate = 0;
  78. }
  79. static void callback_task_D(void *arg STARPU_ATTRIBUTE_UNUSED)
  80. {
  81. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  82. loop_cntD++;
  83. if (loop_cntD == niter)
  84. {
  85. /* We are done */
  86. taskD.regenerate = 0;
  87. STARPU_PTHREAD_COND_SIGNAL(&cond);
  88. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  89. }
  90. else
  91. {
  92. int ret;
  93. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  94. /* Let's go for another iteration */
  95. ret = starpu_task_submit(&taskA);
  96. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  97. }
  98. }
  99. int main(int argc, char **argv)
  100. {
  101. // unsigned i;
  102. // double timing;
  103. // double start;
  104. // double end;
  105. int ret;
  106. ret = starpu_initialize(NULL, &argc, &argv);
  107. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  109. /* Implicit data dependencies and regeneratable tasks are not compatible */
  110. starpu_data_set_default_sequential_consistency_flag(0);
  111. starpu_malloc((void**)&check_cnt, sizeof(*check_cnt));
  112. *check_cnt = 0;
  113. starpu_data_handle_t check_data;
  114. starpu_variable_data_register(&check_data, STARPU_MAIN_RAM, (uintptr_t)check_cnt, sizeof(*check_cnt));
  115. starpu_task_init(&taskA);
  116. taskA.cl = &dummy_codelet;
  117. taskA.regenerate = 0; /* this task will be explicitely resubmitted if needed */
  118. taskA.handles[0] = check_data;
  119. starpu_task_init(&taskB);
  120. taskB.cl = &dummy_codelet;
  121. taskB.callback_func = callback_task_B;
  122. taskB.regenerate = 1;
  123. taskB.handles[0] = check_data;
  124. starpu_task_init(&taskC);
  125. taskC.cl = &dummy_codelet;
  126. taskC.callback_func = callback_task_C;
  127. taskC.regenerate = 1;
  128. taskC.handles[0] = check_data;
  129. starpu_task_init(&taskD);
  130. taskD.cl = &dummy_codelet;
  131. taskD.callback_func = callback_task_D;
  132. taskD.regenerate = 1;
  133. taskD.handles[0] = check_data;
  134. struct starpu_task *depsBC_array[1] = {&taskA};
  135. starpu_task_declare_deps_array(&taskB, 1, depsBC_array);
  136. starpu_task_declare_deps_array(&taskC, 1, depsBC_array);
  137. struct starpu_task *depsD_array[2] = {&taskB, &taskC};
  138. starpu_task_declare_deps_array(&taskD, 2, depsD_array);
  139. ret = starpu_task_submit(&taskA); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  140. ret = starpu_task_submit(&taskB); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  141. ret = starpu_task_submit(&taskC); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  142. ret = starpu_task_submit(&taskD); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  143. /* Wait for the termination of all loops */
  144. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  145. while (loop_cntD < niter)
  146. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  147. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  148. starpu_data_acquire(check_data, STARPU_R);
  149. starpu_data_release(check_data);
  150. STARPU_ASSERT(*check_cnt == (4*niter));
  151. starpu_free(check_cnt);
  152. starpu_data_unregister(check_data);
  153. starpu_task_wait_for_all();
  154. starpu_task_clean(&taskA);
  155. starpu_task_clean(&taskB);
  156. starpu_task_clean(&taskC);
  157. starpu_task_clean(&taskD);
  158. starpu_shutdown();
  159. return EXIT_SUCCESS;
  160. enodev:
  161. fprintf(stderr, "WARNING: No one can execute this task\n");
  162. /* yes, we do not perform the computation but we did detect that no one
  163. * could perform the kernel, so this is not an error from StarPU */
  164. starpu_data_unregister(check_data);
  165. starpu_shutdown();
  166. return STARPU_TEST_SKIPPED;
  167. }