regenerate.c 3.4 KB

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