async-tasks-overhead.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <sys/time.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <starpu.h>
  21. static pthread_mutex_t mutex;
  22. static pthread_cond_t cond;
  23. static unsigned ntasks = 65536;
  24. static unsigned cnt;
  25. static unsigned finished = 0;
  26. static void *dummy_func(void *arg __attribute__ ((unused)))
  27. {
  28. return NULL;
  29. }
  30. static starpu_codelet dummy_codelet =
  31. {
  32. .where = ANY,
  33. .core_func = dummy_func,
  34. .cublas_func = dummy_func,
  35. #ifdef SPU_FUNC_NULL
  36. .gordon_func = SPU_FUNC_NULL,
  37. #endif
  38. .model = NULL,
  39. .nbuffers = 0
  40. };
  41. void callback(void *arg)
  42. {
  43. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  44. if (res == 0)
  45. {
  46. pthread_mutex_lock(&mutex);
  47. finished = 1;
  48. pthread_cond_signal(&cond);
  49. pthread_mutex_unlock(&mutex);
  50. }
  51. }
  52. static void inject_one_task(void)
  53. {
  54. struct starpu_task *task = starpu_task_create();
  55. task->cl = &dummy_codelet;
  56. task->cl_arg = NULL;
  57. task->callback_func = callback;
  58. task->callback_arg = NULL;
  59. starpu_submit_task(task);
  60. }
  61. static struct starpu_conf conf = {
  62. .sched_policy = NULL,
  63. .ncpus = -1,
  64. .ncuda = -1,
  65. .nspus = -1,
  66. .calibrate = 0
  67. };
  68. static void usage(char **argv)
  69. {
  70. fprintf(stderr, "%s [-i ntasks] [-p sched_policy] [-h]\n", argv[0]);
  71. exit(-1);
  72. }
  73. static void parse_args(int argc, char **argv)
  74. {
  75. int c;
  76. while ((c = getopt(argc, argv, "i:p:h")) != -1)
  77. switch(c) {
  78. case 'i':
  79. ntasks = atoi(optarg);
  80. break;
  81. case 'p':
  82. conf.sched_policy = optarg;
  83. break;
  84. case 'h':
  85. usage(argv);
  86. break;
  87. }
  88. }
  89. int main(int argc, char **argv)
  90. {
  91. unsigned i;
  92. double timing;
  93. struct timeval start;
  94. struct timeval end;
  95. pthread_mutex_init(&mutex, NULL);
  96. pthread_cond_init(&cond, NULL);
  97. parse_args(argc, argv);
  98. cnt = ntasks;
  99. starpu_init(&conf);
  100. fprintf(stderr, "#tasks : %d\n", ntasks);
  101. gettimeofday(&start, NULL);
  102. for (i = 0; i < ntasks; i++)
  103. {
  104. inject_one_task();
  105. }
  106. pthread_mutex_lock(&mutex);
  107. if (!finished)
  108. pthread_cond_wait(&cond, &mutex);
  109. pthread_mutex_unlock(&mutex);
  110. gettimeofday(&end, NULL);
  111. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  112. fprintf(stderr, "Total: %lf secs\n", timing/1000000);
  113. fprintf(stderr, "Per task: %lf usecs\n", timing/ntasks);
  114. starpu_shutdown();
  115. return 0;
  116. }