subgraph_repeat.c 5.0 KB

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