subgraph_repeat_regenerate.c 3.5 KB

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