tasks_overhead.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011,2013,2014,2016,2020 Université de Bordeaux
  4. * Copyright (C) 2013 Inria
  5. * Copyright (C) 2010-2013,2015-2017 CNRS
  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. /*
  23. * Measure the submission time and execution time of asynchronous tasks
  24. */
  25. starpu_data_handle_t data_handles[8];
  26. float *buffers[8];
  27. #ifdef STARPU_QUICK_CHECK
  28. static unsigned ntasks = 128;
  29. #else
  30. static unsigned ntasks = 65536;
  31. #endif
  32. static unsigned nbuffers = 0;
  33. #define BUFFERSIZE 16
  34. struct starpu_task *tasks;
  35. void dummy_func(void *descr[], void *arg)
  36. {
  37. (void)descr;
  38. (void)arg;
  39. }
  40. static struct starpu_codelet dummy_codelet =
  41. {
  42. .cpu_funcs = {dummy_func},
  43. .cuda_funcs = {dummy_func},
  44. .opencl_funcs = {dummy_func},
  45. .cpu_funcs_name = {"dummy_func"},
  46. .model = NULL,
  47. .nbuffers = 0,
  48. .modes = {STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW}
  49. };
  50. static void usage(char **argv)
  51. {
  52. fprintf(stderr, "Usage: %s [-i ntasks] [-p sched_policy] [-b nbuffers] [-h]\n", argv[0]);
  53. exit(EXIT_FAILURE);
  54. }
  55. static void parse_args(int argc, char **argv, struct starpu_conf *conf)
  56. {
  57. int c;
  58. while ((c = getopt(argc, argv, "i:b:p:h")) != -1)
  59. switch(c)
  60. {
  61. case 'i':
  62. ntasks = atoi(optarg);
  63. break;
  64. case 'b':
  65. nbuffers = atoi(optarg);
  66. dummy_codelet.nbuffers = nbuffers;
  67. break;
  68. case 'p':
  69. conf->sched_policy_name = optarg;
  70. break;
  71. case 'h':
  72. usage(argv);
  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, &conf);
  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], BUFFERSIZE*sizeof(float));
  97. starpu_vector_data_register(&data_handles[buffer], STARPU_MAIN_RAM, (uintptr_t)buffers[buffer], BUFFERSIZE, 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].cl = &dummy_codelet;
  106. tasks[i].synchronous = 0;
  107. tasks[i].use_tag = 1;
  108. tasks[i].tag_id = (starpu_tag_t)i;
  109. /* we have 8 buffers at most */
  110. for (buffer = 0; buffer < nbuffers; buffer++)
  111. {
  112. tasks[i].handles[buffer] = data_handles[buffer];
  113. }
  114. }
  115. tasks[ntasks-1].detach = 0;
  116. start_submit = starpu_timing_now();
  117. if (nbuffers)
  118. {
  119. /* Data dependency, just submit them all */
  120. for (i = 0; i < ntasks; i++)
  121. {
  122. ret = starpu_task_submit(&tasks[i]);
  123. if (ret == -ENODEV) goto enodev;
  124. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  125. }
  126. }
  127. else
  128. {
  129. /* No data dependency, we have to introduce dependencies by hand */
  130. for (i = 1; i < ntasks; i++)
  131. {
  132. starpu_tag_declare_deps((starpu_tag_t)i, 1, (starpu_tag_t)(i-1));
  133. ret = starpu_task_submit(&tasks[i]);
  134. if (ret == -ENODEV) goto enodev;
  135. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  136. }
  137. /* submit the first task */
  138. ret = starpu_task_submit(&tasks[0]);
  139. if (ret == -ENODEV) goto enodev;
  140. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  141. }
  142. end_submit = starpu_timing_now();
  143. /* wait for the execution of the tasks */
  144. start_exec = starpu_timing_now();
  145. ret = starpu_task_wait(&tasks[ntasks-1]);
  146. STARPU_CHECK_RETURN_VALUE(ret, "starpu_tag_wait");
  147. end_exec = starpu_timing_now();
  148. starpu_task_wait_for_all();
  149. for (i = 0; i < ntasks; i++)
  150. starpu_task_clean(&tasks[i]);
  151. for (buffer = 0; buffer < nbuffers; buffer++)
  152. starpu_data_unregister(data_handles[buffer]);
  153. timing_submit = end_submit - start_submit;
  154. timing_exec = end_exec - start_exec;
  155. fprintf(stderr, "Total submit: %f secs\n", timing_submit/1000000);
  156. fprintf(stderr, "Per task submit: %f usecs\n", timing_submit/ntasks);
  157. fprintf(stderr, "\n");
  158. fprintf(stderr, "Total execution: %f secs\n", timing_exec/1000000);
  159. fprintf(stderr, "Per task execution: %f usecs\n", timing_exec/ntasks);
  160. fprintf(stderr, "\n");
  161. fprintf(stderr, "Total: %f secs\n", (timing_submit+timing_exec)/1000000);
  162. fprintf(stderr, "Per task: %f usecs\n", (timing_submit+timing_exec)/ntasks);
  163. {
  164. char *output_dir = getenv("STARPU_BENCH_DIR");
  165. char *bench_id = getenv("STARPU_BENCH_ID");
  166. if (output_dir && bench_id)
  167. {
  168. char number[1+sizeof(nbuffers)*3+1];
  169. const char *numberp;
  170. char file[1024];
  171. FILE *f;
  172. if (nbuffers)
  173. {
  174. snprintf(number, sizeof(number), "_%u", nbuffers);
  175. numberp = number;
  176. }
  177. else
  178. numberp = "";
  179. snprintf(file, sizeof(file), "%s/tasks_overhead_total_submit%s.dat", output_dir, numberp);
  180. f = fopen(file, "a");
  181. fprintf(f, "%s\t%f\n", bench_id, timing_submit/1000000);
  182. fclose(f);
  183. snprintf(file, sizeof(file), "%s/tasks_overhead_per_task_submit%s.dat", output_dir, numberp);
  184. f = fopen(file, "a");
  185. fprintf(f, "%s\t%f\n", bench_id, timing_submit/ntasks);
  186. fclose(f);
  187. snprintf(file, sizeof(file), "%s/tasks_overhead_total_execution%s.dat", output_dir, numberp);
  188. f = fopen(file, "a");
  189. fprintf(f, "%s\t%f\n", bench_id, timing_exec/1000000);
  190. fclose(f);
  191. snprintf(file, sizeof(file), "%s/tasks_overhead_per_task_execution%s.dat", output_dir, numberp);
  192. f = fopen(file, "a");
  193. fprintf(f, "%s\t%f\n", bench_id, timing_exec/ntasks);
  194. fclose(f);
  195. snprintf(file, sizeof(file), "%s/tasks_overhead_total_submit_execution%s.dat", output_dir, numberp);
  196. f = fopen(file, "a");
  197. fprintf(f, "%s\t%f\n", bench_id, (timing_submit+timing_exec)/1000000);
  198. fclose(f);
  199. snprintf(file, sizeof(file), "%s/tasks_overhead_per_task_submit_execution%s.dat", output_dir, numberp);
  200. f = fopen(file, "a");
  201. fprintf(f, "%s\t%f\n", bench_id, (timing_submit+timing_exec)/ntasks);
  202. fclose(f);
  203. }
  204. }
  205. starpu_shutdown();
  206. free(tasks);
  207. return EXIT_SUCCESS;
  208. enodev:
  209. fprintf(stderr, "WARNING: No one can execute this task\n");
  210. /* yes, we do not perform the computation but we did detect that no one
  211. * could perform the kernel, so this is not an error from StarPU */
  212. starpu_shutdown();
  213. free(tasks);
  214. return STARPU_TEST_SKIPPED;
  215. }