regenerate.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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-2014,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. * Run one task with regenerate=1, and thus completes several times
  25. * before we reset regenerate to 0 in the callback
  26. */
  27. #ifdef STARPU_QUICK_CHECK
  28. static unsigned ntasks = 64;
  29. #else
  30. static unsigned ntasks = 65536;
  31. #endif
  32. static unsigned cnt = 0;
  33. static unsigned completed = 0;
  34. static starpu_pthread_mutex_t mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  35. static starpu_pthread_cond_t cond = STARPU_PTHREAD_COND_INITIALIZER;
  36. static
  37. void callback(void *arg)
  38. {
  39. (void)arg;
  40. struct starpu_task *task = starpu_task_get_current();
  41. cnt++;
  42. if (cnt == ntasks)
  43. {
  44. task->regenerate = 0;
  45. FPRINTF(stderr, "Stop !\n");
  46. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  47. completed = 1;
  48. STARPU_PTHREAD_COND_SIGNAL(&cond);
  49. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  50. }
  51. }
  52. void dummy_func(void *descr[], void *arg)
  53. {
  54. (void)descr;
  55. (void)arg;
  56. }
  57. static struct starpu_codelet dummy_codelet =
  58. {
  59. .cpu_funcs = {dummy_func},
  60. .cuda_funcs = {dummy_func},
  61. .opencl_funcs = {dummy_func},
  62. .cpu_funcs_name = {"dummy_func"},
  63. .model = NULL,
  64. .nbuffers = 0
  65. };
  66. static void parse_args(int argc, char **argv)
  67. {
  68. int c;
  69. while ((c = getopt(argc, argv, "i:")) != -1)
  70. switch(c)
  71. {
  72. case 'i':
  73. ntasks = atoi(optarg);
  74. break;
  75. }
  76. }
  77. int main(int argc, char **argv)
  78. {
  79. // unsigned i;
  80. double timing;
  81. double start;
  82. double end;
  83. int ret;
  84. parse_args(argc, argv);
  85. ret = starpu_initialize(NULL, &argc, &argv);
  86. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  87. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  88. struct starpu_task task;
  89. starpu_task_init(&task);
  90. task.cl = &dummy_codelet;
  91. task.regenerate = 1;
  92. task.detach = 1;
  93. task.callback_func = callback;
  94. FPRINTF(stderr, "#tasks : %u\n", ntasks);
  95. start = starpu_timing_now();
  96. ret = starpu_task_submit(&task);
  97. if (ret == -ENODEV) goto enodev;
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  99. starpu_do_schedule();
  100. STARPU_PTHREAD_MUTEX_LOCK(&mutex);
  101. if (!completed)
  102. STARPU_PTHREAD_COND_WAIT(&cond, &mutex);
  103. STARPU_PTHREAD_MUTEX_UNLOCK(&mutex);
  104. end = starpu_timing_now();
  105. timing = end - start;
  106. FPRINTF(stderr, "Total: %f secs\n", timing/1000000);
  107. FPRINTF(stderr, "Per task: %f usecs\n", timing/ntasks);
  108. starpu_task_wait_for_all();
  109. starpu_task_clean(&task);
  110. starpu_shutdown();
  111. /* Cleanup the statically allocated tasks after shutdown, as StarPU is still working on it after the callback */
  112. starpu_task_clean(&task);
  113. return EXIT_SUCCESS;
  114. enodev:
  115. fprintf(stderr, "WARNING: No one can execute this task\n");
  116. /* yes, we do not perform the computation but we did detect that no one
  117. * could perform the kernel, so this is not an error from StarPU */
  118. starpu_shutdown();
  119. return STARPU_TEST_SKIPPED;
  120. }