subgraph_repeat_regenerate.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <pthread.h>
  18. static unsigned niter = 16384;
  19. /*
  20. *
  21. * /-->B--\
  22. * | |
  23. * -----> A D---\--->
  24. * ^ | | |
  25. * | \-->C--/ |
  26. * | |
  27. * \--------------/
  28. *
  29. * - {B, C} depend on A
  30. * - D depends on {B, C}
  31. * - A, B, C and D are resubmitted at the end of the loop (or not)
  32. */
  33. static struct starpu_task taskA, taskB, taskC, taskD;
  34. static unsigned loop_cnt = 0;
  35. static unsigned check_cnt = 0;
  36. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  37. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  38. static void dummy_func(void *descr[] __attribute__ ((unused)), void *arg __attribute__ ((unused)))
  39. {
  40. STARPU_ATOMIC_ADD(&check_cnt, 1);
  41. }
  42. static starpu_codelet dummy_codelet =
  43. {
  44. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  45. .cpu_func = dummy_func,
  46. .cuda_func = dummy_func,
  47. .opencl_func = dummy_func,
  48. .model = NULL,
  49. .nbuffers = 0
  50. };
  51. static void callback_task_D(void *arg __attribute__((unused)))
  52. {
  53. loop_cnt++;
  54. if (loop_cnt == niter)
  55. {
  56. /* We are done */
  57. pthread_mutex_lock(&mutex);
  58. pthread_cond_signal(&cond);
  59. pthread_mutex_unlock(&mutex);
  60. }
  61. else {
  62. /* Let's go for another iteration */
  63. starpu_task_submit(&taskA);
  64. }
  65. }
  66. int main(int argc, char **argv)
  67. {
  68. unsigned i;
  69. double timing;
  70. struct timeval start;
  71. struct timeval end;
  72. starpu_init(NULL);
  73. /* Implicit data dependencies and regeneratable tasks are not compatible */
  74. starpu_data_set_default_sequential_consistency_flag(0);
  75. starpu_task_init(&taskA);
  76. taskA.cl = &dummy_codelet;
  77. taskA.cl_arg = &taskA;
  78. taskA.regenerate = 0; /* this task will be explicitely resubmitted if needed */
  79. starpu_task_init(&taskB);
  80. taskB.cl = &dummy_codelet;
  81. taskB.cl_arg = &taskB;
  82. taskB.regenerate = 1;
  83. starpu_task_init(&taskC);
  84. taskC.cl = &dummy_codelet;
  85. taskC.cl_arg = &taskC;
  86. taskC.regenerate = 1;
  87. starpu_task_init(&taskD);
  88. taskD.cl = &dummy_codelet;
  89. taskD.cl_arg = &taskD;
  90. taskD.callback_func = callback_task_D;
  91. taskD.regenerate = 1;
  92. struct starpu_task *depsBC_array[1] = {&taskA};
  93. starpu_task_declare_deps_array(&taskB, 1, depsBC_array);
  94. starpu_task_declare_deps_array(&taskC, 1, depsBC_array);
  95. struct starpu_task *depsD_array[2] = {&taskB, &taskC};
  96. starpu_task_declare_deps_array(&taskD, 2, depsD_array);
  97. starpu_task_submit(&taskA);
  98. starpu_task_submit(&taskB);
  99. starpu_task_submit(&taskC);
  100. starpu_task_submit(&taskD);
  101. /* Wait for the termination of all loops */
  102. pthread_mutex_lock(&mutex);
  103. if (loop_cnt < niter)
  104. pthread_cond_wait(&cond, &mutex);
  105. pthread_mutex_unlock(&mutex);
  106. STARPU_ASSERT(check_cnt == (4*loop_cnt));
  107. /* Cleanup the statically allocated tasks */
  108. starpu_task_deinit(&taskA);
  109. starpu_task_deinit(&taskB);
  110. starpu_task_deinit(&taskC);
  111. starpu_task_deinit(&taskD);
  112. starpu_shutdown();
  113. return 0;
  114. }