subgraph_repeat_regenerate.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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 <pthread.h>
  20. #include "../helper.h"
  21. static unsigned niter = 16384;
  22. /*
  23. *
  24. * /-->B--\
  25. * | |
  26. * -----> A D---\--->
  27. * ^ | | |
  28. * | \-->C--/ |
  29. * | |
  30. * \--------------/
  31. *
  32. * - {B, C} depend on A
  33. * - D depends on {B, C}
  34. * - A, B, C and D are resubmitted at the end of the loop (or not)
  35. */
  36. static struct starpu_task taskA, taskB, taskC, taskD;
  37. static unsigned loop_cnt = 0;
  38. static unsigned check_cnt = 0;
  39. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  40. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  41. static void dummy_func(void *descr[] __attribute__ ((unused)), void *arg __attribute__ ((unused)))
  42. {
  43. (void) STARPU_ATOMIC_ADD(&check_cnt, 1);
  44. }
  45. static struct starpu_codelet dummy_codelet =
  46. {
  47. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  48. .cpu_funcs = {dummy_func, NULL},
  49. .cuda_funcs = {dummy_func, NULL},
  50. .opencl_funcs = {dummy_func, NULL},
  51. .model = NULL,
  52. .nbuffers = 0
  53. };
  54. static void callback_task_D(void *arg __attribute__((unused)))
  55. {
  56. loop_cnt++;
  57. if (loop_cnt == niter)
  58. {
  59. /* We are done */
  60. taskD.regenerate = 0;
  61. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  62. _STARPU_PTHREAD_COND_SIGNAL(&cond);
  63. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  64. }
  65. else
  66. {
  67. int ret;
  68. /* Let's go for another iteration */
  69. ret = starpu_task_submit(&taskA);
  70. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  71. }
  72. }
  73. int main(int argc, char **argv)
  74. {
  75. // unsigned i;
  76. // double timing;
  77. // struct timeval start;
  78. // struct timeval end;
  79. int ret;
  80. ret = starpu_init(NULL);
  81. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  82. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  83. /* Implicit data dependencies and regeneratable tasks are not compatible */
  84. starpu_data_set_default_sequential_consistency_flag(0);
  85. starpu_task_init(&taskA);
  86. taskA.cl = &dummy_codelet;
  87. taskA.cl_arg = &taskA;
  88. taskA.regenerate = 0; /* this task will be explicitely resubmitted if needed */
  89. starpu_task_init(&taskB);
  90. taskB.cl = &dummy_codelet;
  91. taskB.cl_arg = &taskB;
  92. taskB.regenerate = 1;
  93. starpu_task_init(&taskC);
  94. taskC.cl = &dummy_codelet;
  95. taskC.cl_arg = &taskC;
  96. taskC.regenerate = 1;
  97. starpu_task_init(&taskD);
  98. taskD.cl = &dummy_codelet;
  99. taskD.cl_arg = &taskD;
  100. taskD.callback_func = callback_task_D;
  101. taskD.regenerate = 1;
  102. struct starpu_task *depsBC_array[1] = {&taskA};
  103. starpu_task_declare_deps_array(&taskB, 1, depsBC_array);
  104. starpu_task_declare_deps_array(&taskC, 1, depsBC_array);
  105. struct starpu_task *depsD_array[2] = {&taskB, &taskC};
  106. starpu_task_declare_deps_array(&taskD, 2, depsD_array);
  107. ret = starpu_task_submit(&taskA); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  108. ret = starpu_task_submit(&taskB); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  109. ret = starpu_task_submit(&taskC); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  110. ret = starpu_task_submit(&taskD); if (ret == -ENODEV) goto enodev; STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  111. /* Wait for the termination of all loops */
  112. _STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  113. if (loop_cnt < niter)
  114. _STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  115. _STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  116. STARPU_ASSERT(check_cnt == (4*loop_cnt));
  117. starpu_shutdown();
  118. /* Cleanup the statically allocated tasks after shutdown, as StarPU is still working on it after the callback */
  119. starpu_task_deinit(&taskA);
  120. starpu_task_deinit(&taskB);
  121. starpu_task_deinit(&taskC);
  122. starpu_task_deinit(&taskD);
  123. return EXIT_SUCCESS;
  124. enodev:
  125. fprintf(stderr, "WARNING: No one can execute this task\n");
  126. /* yes, we do not perform the computation but we did detect that no one
  127. * could perform the kernel, so this is not an error from StarPU */
  128. starpu_shutdown();
  129. return STARPU_TEST_SKIPPED;
  130. }