subgraph_repeat_regenerate.c 5.4 KB

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