regenerate_pipeline.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Inria
  4. * Copyright (C) 2010-2013,2015,2017 CNRS
  5. * Copyright (C) 2010-2016 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <starpu.h>
  21. #include "../helper.h"
  22. #include <common/thread.h>
  23. /*
  24. * Create a pipeline of regenerated tasks, i.e. a sort of data flow graph
  25. */
  26. #ifdef STARPU_QUICK_CHECK
  27. static unsigned ntasks = 64;
  28. #else
  29. static unsigned ntasks = 65536;
  30. #endif
  31. static unsigned cntA = 0;
  32. static unsigned cntB = 0;
  33. static unsigned cntC = 0;
  34. static unsigned completed = 0;
  35. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  36. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  37. static
  38. void callback(void *arg)
  39. {
  40. struct starpu_task *task = starpu_task_get_current();
  41. unsigned *cnt = arg;
  42. unsigned res;
  43. res = STARPU_ATOMIC_ADD(cnt, 1);
  44. ANNOTATE_HAPPENS_BEFORE(&cnt);
  45. if (res == ntasks)
  46. {
  47. ANNOTATE_HAPPENS_AFTER(&cnt);
  48. task->regenerate = 0;
  49. FPRINTF(stderr, "Stop !\n");
  50. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  51. completed++;
  52. STARPU_PTHREAD_COND_SIGNAL(&cond);
  53. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  54. }
  55. }
  56. void dummy_func(void *descr[], void *arg)
  57. {
  58. (void)descr;
  59. (void)arg;
  60. }
  61. static struct starpu_codelet dummy_codelet =
  62. {
  63. .cpu_funcs = {dummy_func},
  64. .cuda_funcs = {dummy_func},
  65. .opencl_funcs = {dummy_func},
  66. .cpu_funcs_name = {"dummy_func"},
  67. .model = NULL,
  68. .nbuffers = 0
  69. };
  70. static void parse_args(int argc, char **argv)
  71. {
  72. int c;
  73. while ((c = getopt(argc, argv, "i:")) != -1)
  74. switch(c)
  75. {
  76. case 'i':
  77. ntasks = atoi(optarg);
  78. break;
  79. }
  80. }
  81. int main(int argc, char **argv)
  82. {
  83. // unsigned i;
  84. double timing;
  85. double start;
  86. double end;
  87. int ret;
  88. parse_args(argc, argv);
  89. ret = starpu_initialize(NULL, &argc, &argv);
  90. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  92. struct starpu_task taskA, taskB, taskC;
  93. struct starpu_task *taskAp = &taskA;
  94. struct starpu_task *taskBp = &taskB;
  95. starpu_task_init(&taskA);
  96. taskA.cl = &dummy_codelet;
  97. taskA.regenerate = 1;
  98. taskA.detach = 1;
  99. taskA.callback_func = callback;
  100. taskA.callback_arg = &cntA;
  101. starpu_task_init(&taskB);
  102. taskB.cl = &dummy_codelet;
  103. taskB.regenerate = 1;
  104. taskB.detach = 1;
  105. taskB.callback_func = callback;
  106. taskB.callback_arg = &cntB;
  107. starpu_task_declare_deps_array(&taskB, 1, &taskAp);
  108. starpu_task_init(&taskC);
  109. taskC.cl = &dummy_codelet;
  110. taskC.regenerate = 1;
  111. taskC.detach = 1;
  112. taskC.callback_func = callback;
  113. taskC.callback_arg = &cntC;
  114. starpu_task_declare_deps_array(&taskC, 1, &taskBp);
  115. FPRINTF(stderr, "#tasks : %u\n", ntasks);
  116. start = starpu_timing_now();
  117. ret = starpu_task_submit(&taskA);
  118. if (ret == -ENODEV) goto enodev;
  119. ret = starpu_task_submit(&taskB);
  120. if (ret == -ENODEV) goto enodev;
  121. ret = starpu_task_submit(&taskC);
  122. if (ret == -ENODEV) goto enodev;
  123. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  124. starpu_do_schedule();
  125. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  126. while (completed < 3)
  127. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  128. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  129. end = starpu_timing_now();
  130. timing = end - start;
  131. FPRINTF(stderr, "cntA : %u\n", cntA);
  132. FPRINTF(stderr, "cntB : %u\n", cntB);
  133. FPRINTF(stderr, "cntC : %u\n", cntC);
  134. STARPU_ASSERT(cntA == ntasks);
  135. STARPU_ASSERT(cntB == ntasks);
  136. STARPU_ASSERT(cntC == ntasks);
  137. FPRINTF(stderr, "Total: %f secs\n", timing/1000000);
  138. FPRINTF(stderr, "Per task: %f usecs\n", timing/(ntasks*3));
  139. starpu_task_wait_for_all();
  140. starpu_task_clean(&taskA);
  141. starpu_task_clean(&taskB);
  142. starpu_task_clean(&taskC);
  143. starpu_shutdown();
  144. return EXIT_SUCCESS;
  145. enodev:
  146. fprintf(stderr, "WARNING: No one can execute this task\n");
  147. /* yes, we do not perform the computation but we did detect that no one
  148. * could perform the kernel, so this is not an error from StarPU */
  149. starpu_shutdown();
  150. return STARPU_TEST_SKIPPED;
  151. }