regenerate_pipeline.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <stdio.h>
  17. #include <unistd.h>
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. #include <common/thread.h>
  21. /*
  22. * Create a pipeline of regenerated tasks, i.e. a sort of data flow graph
  23. */
  24. #ifdef STARPU_QUICK_CHECK
  25. static unsigned ntasks = 64;
  26. #else
  27. static unsigned ntasks = 65536;
  28. #endif
  29. static unsigned cntA = 0;
  30. static unsigned cntB = 0;
  31. static unsigned cntC = 0;
  32. static unsigned completed = 0;
  33. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  34. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  35. static
  36. void callback(void *arg)
  37. {
  38. struct starpu_task *task = starpu_task_get_current();
  39. unsigned *cnt = arg;
  40. unsigned res;
  41. res = STARPU_ATOMIC_ADD(cnt, 1);
  42. ANNOTATE_HAPPENS_BEFORE(&cnt);
  43. if (res == ntasks)
  44. {
  45. ANNOTATE_HAPPENS_AFTER(&cnt);
  46. task->regenerate = 0;
  47. FPRINTF(stderr, "Stop !\n");
  48. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  49. completed++;
  50. STARPU_PTHREAD_COND_SIGNAL(&cond);
  51. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  52. }
  53. }
  54. void dummy_func(void *descr[], void *arg)
  55. {
  56. (void)descr;
  57. (void)arg;
  58. }
  59. static struct starpu_codelet dummy_codelet =
  60. {
  61. .cpu_funcs = {dummy_func},
  62. .cuda_funcs = {dummy_func},
  63. .opencl_funcs = {dummy_func},
  64. .cpu_funcs_name = {"dummy_func"},
  65. .model = NULL,
  66. .nbuffers = 0
  67. };
  68. static void parse_args(int argc, char **argv)
  69. {
  70. int c;
  71. while ((c = getopt(argc, argv, "i:")) != -1)
  72. switch(c)
  73. {
  74. case 'i':
  75. ntasks = atoi(optarg);
  76. break;
  77. }
  78. }
  79. int main(int argc, char **argv)
  80. {
  81. // unsigned i;
  82. double timing;
  83. double start;
  84. double end;
  85. int ret;
  86. parse_args(argc, argv);
  87. ret = starpu_initialize(NULL, &argc, &argv);
  88. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  89. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  90. struct starpu_task taskA, taskB, taskC;
  91. struct starpu_task *taskAp = &taskA;
  92. struct starpu_task *taskBp = &taskB;
  93. starpu_task_init(&taskA);
  94. taskA.cl = &dummy_codelet;
  95. taskA.regenerate = 1;
  96. taskA.detach = 1;
  97. taskA.callback_func = callback;
  98. taskA.callback_arg = &cntA;
  99. starpu_task_init(&taskB);
  100. taskB.cl = &dummy_codelet;
  101. taskB.regenerate = 1;
  102. taskB.detach = 1;
  103. taskB.callback_func = callback;
  104. taskB.callback_arg = &cntB;
  105. starpu_task_declare_deps_array(&taskB, 1, &taskAp);
  106. starpu_task_init(&taskC);
  107. taskC.cl = &dummy_codelet;
  108. taskC.regenerate = 1;
  109. taskC.detach = 1;
  110. taskC.callback_func = callback;
  111. taskC.callback_arg = &cntC;
  112. starpu_task_declare_deps_array(&taskC, 1, &taskBp);
  113. FPRINTF(stderr, "#tasks : %u\n", ntasks);
  114. start = starpu_timing_now();
  115. ret = starpu_task_submit(&taskA);
  116. if (ret == -ENODEV) goto enodev;
  117. ret = starpu_task_submit(&taskB);
  118. if (ret == -ENODEV) goto enodev;
  119. ret = starpu_task_submit(&taskC);
  120. if (ret == -ENODEV) goto enodev;
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  122. starpu_do_schedule();
  123. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  124. while (completed < 3)
  125. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  126. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  127. end = starpu_timing_now();
  128. timing = end - start;
  129. FPRINTF(stderr, "cntA : %u\n", cntA);
  130. FPRINTF(stderr, "cntB : %u\n", cntB);
  131. FPRINTF(stderr, "cntC : %u\n", cntC);
  132. STARPU_ASSERT(cntA == ntasks);
  133. STARPU_ASSERT(cntB == ntasks);
  134. STARPU_ASSERT(cntC == ntasks);
  135. FPRINTF(stderr, "Total: %f secs\n", timing/1000000);
  136. FPRINTF(stderr, "Per task: %f usecs\n", timing/(ntasks*3));
  137. starpu_task_wait_for_all();
  138. starpu_task_clean(&taskA);
  139. starpu_task_clean(&taskB);
  140. starpu_task_clean(&taskC);
  141. starpu_shutdown();
  142. return EXIT_SUCCESS;
  143. enodev:
  144. fprintf(stderr, "WARNING: No one can execute this task\n");
  145. /* yes, we do not perform the computation but we did detect that no one
  146. * could perform the kernel, so this is not an error from StarPU */
  147. starpu_shutdown();
  148. return STARPU_TEST_SKIPPED;
  149. }