async_tasks_overhead.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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, 2015, 2016, 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. /*
  22. * Measure the cost of submitting asynchronous tasks
  23. */
  24. static unsigned ntasks = 65536;
  25. //static unsigned finished = 0;
  26. static double cumulated = 0.0;
  27. static double cumulated_push = 0.0;
  28. static double cumulated_pop = 0.0;
  29. void dummy_func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg STARPU_ATTRIBUTE_UNUSED)
  30. {
  31. }
  32. static struct starpu_codelet dummy_codelet =
  33. {
  34. .cpu_funcs = {dummy_func},
  35. .cuda_funcs = {dummy_func},
  36. .opencl_funcs = {dummy_func},
  37. .cpu_funcs_name = {"dummy_func"},
  38. .model = NULL,
  39. .nbuffers = 0
  40. };
  41. //static void inject_one_task(void)
  42. //{
  43. // struct starpu_task *task = starpu_task_create();
  44. //
  45. // task->cl = &dummy_codelet;
  46. // task->cl_arg = NULL;
  47. // task->detach = 0;
  48. //
  49. // int ret = starpu_task_submit(task);
  50. // STARPU_ASSERT(!ret);
  51. //}
  52. static void usage(char **argv)
  53. {
  54. fprintf(stderr, "%s [-i ntasks] [-p sched_policy] [-h]\n", argv[0]);
  55. exit(-1);
  56. }
  57. static void parse_args(int argc, char **argv, struct starpu_conf *conf)
  58. {
  59. int c;
  60. while ((c = getopt(argc, argv, "i:p:h")) != -1)
  61. switch(c)
  62. {
  63. case 'i':
  64. ntasks = atoi(optarg);
  65. break;
  66. case 'p':
  67. conf->sched_policy_name = optarg;
  68. break;
  69. case 'h':
  70. usage(argv);
  71. break;
  72. }
  73. }
  74. int main(int argc, char **argv)
  75. {
  76. int ret;
  77. unsigned i;
  78. double timing;
  79. double start;
  80. double end;
  81. struct starpu_conf conf;
  82. starpu_conf_init(&conf);
  83. conf.ncpus = 2;
  84. #ifdef STARPU_QUICK_CHECK
  85. ntasks = 128;
  86. #endif
  87. parse_args(argc, argv, &conf);
  88. ret = starpu_initialize(&conf, &argc, &argv);
  89. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  90. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  91. starpu_profiling_status_set(STARPU_PROFILING_ENABLE);
  92. fprintf(stderr, "#tasks : %u\n", ntasks);
  93. /* Create an array of tasks */
  94. struct starpu_task **tasks = (struct starpu_task **) malloc(ntasks*sizeof(struct starpu_task *));
  95. for (i = 0; i < ntasks; i++)
  96. {
  97. struct starpu_task *task = starpu_task_create();
  98. task->cl = &dummy_codelet;
  99. task->cl_arg = NULL;
  100. task->detach = 0;
  101. tasks[i] = task;
  102. }
  103. start = starpu_timing_now();
  104. for (i = 0; i < ntasks; i++)
  105. {
  106. ret = starpu_task_submit(tasks[i]);
  107. if (ret == -ENODEV) goto enodev;
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  109. }
  110. ret = starpu_task_wait_for_all();
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  112. end = starpu_timing_now();
  113. /* Read profiling feedback */
  114. for (i = 0; i < ntasks; i++)
  115. {
  116. struct starpu_profiling_task_info *info;
  117. info = tasks[i]->profiling_info;
  118. double queued = starpu_timing_timespec_delay_us(&info->push_end_time, &info->pop_end_time);
  119. double length = starpu_timing_timespec_delay_us(&info->submit_time, &info->end_time);
  120. double push_duration = starpu_timing_timespec_delay_us(&info->push_start_time, &info->push_end_time);
  121. double pop_duration = starpu_timing_timespec_delay_us(&info->pop_start_time, &info->pop_end_time);
  122. starpu_task_destroy(tasks[i]);
  123. cumulated += (length - queued);
  124. cumulated_push += push_duration;
  125. cumulated_pop += pop_duration;
  126. }
  127. timing = end - start;
  128. fprintf(stderr, "Total: %f secs\n", timing/1000000);
  129. fprintf(stderr, "Per task: %f usecs\n", timing/ntasks);
  130. fprintf(stderr, "Per task (except scheduler): %f usecs\n", cumulated/ntasks);
  131. fprintf(stderr, "Per task (push): %f usecs\n", cumulated_push/ntasks);
  132. fprintf(stderr, "Per task (pop): %f usecs\n", cumulated_pop/ntasks);
  133. {
  134. char *output_dir = getenv("STARPU_BENCH_DIR");
  135. char *bench_id = getenv("STARPU_BENCH_ID");
  136. if (output_dir && bench_id)
  137. {
  138. char file[1024];
  139. FILE *f;
  140. snprintf(file, sizeof(file), "%s/async_tasks_overhead_total.dat", output_dir);
  141. f = fopen(file, "a");
  142. fprintf(f, "%s\t%f\n", bench_id, timing/1000000);
  143. fclose(f);
  144. snprintf(file, sizeof(file), "%s/async_tasks_overhead_per_task.dat", output_dir);
  145. f = fopen(file, "a");
  146. fprintf(f, "%s\t%f\n", bench_id, timing/ntasks);
  147. fclose(f);
  148. }
  149. }
  150. starpu_shutdown();
  151. free(tasks);
  152. return EXIT_SUCCESS;
  153. enodev:
  154. fprintf(stderr, "WARNING: No one can execute this task\n");
  155. /* yes, we do not perform the computation but we did detect that no one
  156. * could perform the kernel, so this is not an error from StarPU */
  157. starpu_shutdown();
  158. free(tasks);
  159. return STARPU_TEST_SKIPPED;
  160. }