tasks_overhead.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011, 2013-2014, 2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2015, 2016 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 submission time and execution time of asynchronous tasks
  23. */
  24. starpu_data_handle_t data_handles[8];
  25. float *buffers[8];
  26. #ifdef STARPU_QUICK_CHECK
  27. static unsigned ntasks = 128;
  28. #else
  29. static unsigned ntasks = 65536;
  30. #endif
  31. static unsigned nbuffers = 0;
  32. struct starpu_task *tasks;
  33. void dummy_func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg STARPU_ATTRIBUTE_UNUSED)
  34. {
  35. }
  36. static struct starpu_codelet dummy_codelet =
  37. {
  38. .cpu_funcs = {dummy_func},
  39. .cuda_funcs = {dummy_func},
  40. .opencl_funcs = {dummy_func},
  41. .cpu_funcs_name = {"dummy_func"},
  42. .model = NULL,
  43. .nbuffers = 0,
  44. .modes = {STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW}
  45. };
  46. static
  47. int inject_one_task(void)
  48. {
  49. struct starpu_task *task = starpu_task_create();
  50. task->cl = &dummy_codelet;
  51. task->cl_arg = NULL;
  52. task->callback_func = NULL;
  53. task->synchronous = 1;
  54. int ret;
  55. ret = starpu_task_submit(task);
  56. return ret;
  57. }
  58. static void parse_args(int argc, char **argv)
  59. {
  60. int c;
  61. while ((c = getopt(argc, argv, "i:b:h")) != -1)
  62. switch(c)
  63. {
  64. case 'i':
  65. ntasks = atoi(optarg);
  66. break;
  67. case 'b':
  68. nbuffers = atoi(optarg);
  69. dummy_codelet.nbuffers = nbuffers;
  70. break;
  71. case 'h':
  72. fprintf(stderr, "Usage: %s [-i ntasks] [-b nbuffers] [-h]\n", argv[0]);
  73. break;
  74. }
  75. }
  76. int main(int argc, char **argv)
  77. {
  78. int ret;
  79. unsigned i;
  80. double timing_submit;
  81. double start_submit;
  82. double end_submit;
  83. double timing_exec;
  84. double start_exec;
  85. double end_exec;
  86. struct starpu_conf conf;
  87. starpu_conf_init(&conf);
  88. conf.ncpus = 2;
  89. parse_args(argc, argv);
  90. ret = starpu_initialize(&conf, &argc, &argv);
  91. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  92. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  93. unsigned buffer;
  94. for (buffer = 0; buffer < nbuffers; buffer++)
  95. {
  96. starpu_malloc((void**)&buffers[buffer], 16*sizeof(float));
  97. starpu_vector_data_register(&data_handles[buffer], STARPU_MAIN_RAM, (uintptr_t)buffers[buffer], 16, sizeof(float));
  98. }
  99. fprintf(stderr, "#tasks : %u\n#buffers : %u\n", ntasks, nbuffers);
  100. /* submit tasks (but don't execute them yet !) */
  101. tasks = (struct starpu_task *) calloc(1, ntasks*sizeof(struct starpu_task));
  102. for (i = 0; i < ntasks; i++)
  103. {
  104. starpu_task_init(&tasks[i]);
  105. tasks[i].callback_func = NULL;
  106. tasks[i].cl = &dummy_codelet;
  107. tasks[i].cl_arg = NULL;
  108. tasks[i].synchronous = 0;
  109. tasks[i].use_tag = 1;
  110. tasks[i].tag_id = (starpu_tag_t)i;
  111. /* we have 8 buffers at most */
  112. for (buffer = 0; buffer < nbuffers; buffer++)
  113. {
  114. tasks[i].handles[buffer] = data_handles[buffer];
  115. }
  116. }
  117. tasks[ntasks-1].detach = 0;
  118. start_submit = starpu_timing_now();
  119. for (i = 1; i < ntasks; i++)
  120. {
  121. starpu_tag_declare_deps((starpu_tag_t)i, 1, (starpu_tag_t)(i-1));
  122. ret = starpu_task_submit(&tasks[i]);
  123. if (ret == -ENODEV) goto enodev;
  124. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  125. }
  126. /* submit the first task */
  127. ret = starpu_task_submit(&tasks[0]);
  128. if (ret == -ENODEV) goto enodev;
  129. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  130. end_submit = starpu_timing_now();
  131. /* wait for the execution of the tasks */
  132. start_exec = starpu_timing_now();
  133. ret = starpu_task_wait(&tasks[ntasks-1]);
  134. STARPU_CHECK_RETURN_VALUE(ret, "starpu_tag_wait");
  135. end_exec = starpu_timing_now();
  136. starpu_task_wait_for_all();
  137. for (i = 0; i < ntasks; i++)
  138. starpu_task_clean(&tasks[i]);
  139. for (buffer = 0; buffer < nbuffers; buffer++)
  140. starpu_data_unregister(data_handles[buffer]);
  141. timing_submit = end_submit - start_submit;
  142. timing_exec = end_exec - start_exec;
  143. fprintf(stderr, "Total submit: %f secs\n", timing_submit/1000000);
  144. fprintf(stderr, "Per task submit: %f usecs\n", timing_submit/ntasks);
  145. fprintf(stderr, "\n");
  146. fprintf(stderr, "Total execution: %f secs\n", timing_exec/1000000);
  147. fprintf(stderr, "Per task execution: %f usecs\n", timing_exec/ntasks);
  148. fprintf(stderr, "\n");
  149. fprintf(stderr, "Total: %f secs\n", (timing_submit+timing_exec)/1000000);
  150. fprintf(stderr, "Per task: %f usecs\n", (timing_submit+timing_exec)/ntasks);
  151. {
  152. char *output_dir = getenv("STARPU_BENCH_DIR");
  153. char *bench_id = getenv("STARPU_BENCH_ID");
  154. if (output_dir && bench_id)
  155. {
  156. char file[1024];
  157. FILE *f;
  158. snprintf(file, 1024, "%s/tasks_overhead_total_submit.dat", output_dir);
  159. f = fopen(file, "a");
  160. fprintf(f, "%s\t%f\n", bench_id, timing_submit/1000000);
  161. fclose(f);
  162. snprintf(file, 1024, "%s/tasks_overhead_per_task_submit.dat", output_dir);
  163. f = fopen(file, "a");
  164. fprintf(f, "%s\t%f\n", bench_id, timing_submit/ntasks);
  165. fclose(f);
  166. snprintf(file, 1024, "%s/tasks_overhead_total_execution.dat", output_dir);
  167. f = fopen(file, "a");
  168. fprintf(f, "%s\t%f\n", bench_id, timing_exec/1000000);
  169. fclose(f);
  170. snprintf(file, 1024, "%s/tasks_overhead_per_task_execution.dat", output_dir);
  171. f = fopen(file, "a");
  172. fprintf(f, "%s\t%f\n", bench_id, timing_exec/ntasks);
  173. fclose(f);
  174. snprintf(file, 1024, "%s/tasks_overhead_total_submit_execution.dat", output_dir);
  175. f = fopen(file, "a");
  176. fprintf(f, "%s\t%f\n", bench_id, (timing_submit+timing_exec)/1000000);
  177. fclose(f);
  178. snprintf(file, 1024, "%s/tasks_overhead_per_task_submit_execution.dat", output_dir);
  179. f = fopen(file, "a");
  180. fprintf(f, "%s\t%f\n", bench_id, (timing_submit+timing_exec)/ntasks);
  181. fclose(f);
  182. }
  183. }
  184. starpu_shutdown();
  185. free(tasks);
  186. return EXIT_SUCCESS;
  187. enodev:
  188. fprintf(stderr, "WARNING: No one can execute this task\n");
  189. /* yes, we do not perform the computation but we did detect that no one
  190. * could perform the kernel, so this is not an error from StarPU */
  191. starpu_shutdown();
  192. free(tasks);
  193. return STARPU_TEST_SKIPPED;
  194. }