tasks_size_overhead.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU 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. * StarPU 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 <stdio.h>
  17. #include <unistd.h>
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. /*
  21. * This benchmark creates a thousand tasks of the same (small) duration, with
  22. * various number of cpus and various durations.
  23. *
  24. * Use ./tasks_size_overhead.sh to generate a plot of the result.
  25. *
  26. * Thanks Martin Tillenius for the idea.
  27. */
  28. #define START 4
  29. #define STOP 4096
  30. #ifdef STARPU_QUICK_CHECK
  31. #define FACTOR 64
  32. #else
  33. #define FACTOR 2
  34. #endif
  35. #ifdef STARPU_QUICK_CHECK
  36. #define CPUSTEP 8
  37. #else
  38. #define CPUSTEP 1
  39. #endif
  40. #ifdef STARPU_QUICK_CHECK
  41. static unsigned ntasks = 1;
  42. #elif !defined(STARPU_LONG_CHECK)
  43. static unsigned ntasks = 64;
  44. #else
  45. static unsigned ntasks = 256;
  46. #endif
  47. static unsigned nbuffers = 0;
  48. static unsigned total_nbuffers = 0;
  49. static unsigned mincpus = 1, maxcpus, cpustep = CPUSTEP;
  50. static unsigned mintime = START, maxtime = STOP, factortime = FACTOR;
  51. struct starpu_task *tasks;
  52. void func(void *descr[], void *arg)
  53. {
  54. (void)descr;
  55. unsigned n = (uintptr_t)arg;
  56. long usec = 0;
  57. double tv1 = starpu_timing_now();
  58. do
  59. {
  60. double tv2 = starpu_timing_now();
  61. usec = tv2 - tv1;
  62. }
  63. while (usec < n);
  64. }
  65. double cost_function(struct starpu_task *t, struct starpu_perfmodel_arch *a, unsigned i)
  66. {
  67. (void) t; (void) i; (void) a;
  68. unsigned n = (uintptr_t) t->cl_arg;
  69. return n;
  70. }
  71. static struct starpu_perfmodel perf_model =
  72. {
  73. .type = STARPU_PER_ARCH,
  74. .arch_cost_function = cost_function,
  75. };
  76. static struct starpu_codelet codelet =
  77. {
  78. .cpu_funcs = {func},
  79. .nbuffers = 0,
  80. .modes = {STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R, STARPU_R},
  81. .model = &perf_model,
  82. };
  83. static void parse_args(int argc, char **argv)
  84. {
  85. int c;
  86. while ((c = getopt(argc, argv, "i:b:B:c:C:s:t:T:f:h")) != -1)
  87. switch(c)
  88. {
  89. case 'i':
  90. ntasks = atoi(optarg);
  91. break;
  92. case 'b':
  93. nbuffers = atoi(optarg);
  94. codelet.nbuffers = nbuffers;
  95. break;
  96. case 'B':
  97. total_nbuffers = atoi(optarg);
  98. break;
  99. case 'c':
  100. mincpus = atoi(optarg);
  101. break;
  102. case 'C':
  103. maxcpus = atoi(optarg);
  104. break;
  105. case 's':
  106. cpustep = atoi(optarg);
  107. break;
  108. case 't':
  109. mintime = atoi(optarg);
  110. break;
  111. case 'T':
  112. maxtime = atoi(optarg);
  113. break;
  114. case 'f':
  115. factortime = atoi(optarg);
  116. break;
  117. case 'h':
  118. fprintf(stderr, "\
  119. Usage: %s [-h]\n\
  120. [-i ntasks] [-b nbuffers] [-B total_nbuffers]\n\
  121. [-c mincpus] [ -C maxcpus] [-s cpustep]\n\
  122. [-t mintime] [-T maxtime] [-f factortime]\n\n", argv[0]);
  123. fprintf(stderr,"\
  124. runs 'ntasks' tasks\n\
  125. - using 'nbuffers' data each, randomly among 'total_nbuffers' choices,\n\
  126. - with varying task durations, from 'mintime' to 'maxtime' (using 'factortime')\n\
  127. - on varying numbers of cpus, from 'mincpus' to 'maxcpus' (using 'cpustep')\n\
  128. \n\
  129. currently selected parameters: %u tasks using %u buffers among %u, from %uus to %uus (factor %u), from %u cpus to %u cpus (step %u)\n\
  130. ", ntasks, nbuffers, total_nbuffers, mintime, maxtime, factortime, mincpus, maxcpus, cpustep);
  131. exit(EXIT_SUCCESS);
  132. break;
  133. }
  134. }
  135. int main(int argc, char **argv)
  136. {
  137. int ret;
  138. unsigned i;
  139. unsigned size;
  140. unsigned ncpus;
  141. double timing;
  142. double start;
  143. double end;
  144. struct starpu_conf conf;
  145. unsigned buffer;
  146. char *starpu_sched = getenv("STARPU_SCHED");
  147. /* Get number of CPUs */
  148. starpu_conf_init(&conf);
  149. starpu_conf_noworker(&conf);
  150. conf.ncpus = -1;
  151. #ifdef STARPU_SIMGRID
  152. /* This will get serialized, avoid spending too much time on it. */
  153. maxcpus = 2;
  154. #else
  155. ret = starpu_init(&conf);
  156. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  157. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  158. maxcpus = starpu_worker_get_count_by_type(STARPU_CPU_WORKER);
  159. starpu_shutdown();
  160. #endif
  161. #ifdef STARPU_HAVE_UNSETENV
  162. /* That was useful to force the max number of cpus to use, but now we
  163. * want to make it vary */
  164. unsetenv("STARPU_NCPUS");
  165. unsetenv("STARPU_NCPU");
  166. #endif
  167. if (STARPU_RUNNING_ON_VALGRIND)
  168. {
  169. factortime *= 4;
  170. cpustep *= 4;
  171. }
  172. parse_args(argc, argv);
  173. float *buffers[total_nbuffers?total_nbuffers:1];
  174. /* Allocate data */
  175. for (buffer = 0; buffer < total_nbuffers; buffer++)
  176. buffers[buffer] = (float *) calloc(16, sizeof(float));
  177. tasks = (struct starpu_task *) calloc(1, ntasks*maxcpus*sizeof(struct starpu_task));
  178. /* Emit headers and compute raw tasks speed */
  179. FPRINTF(stdout, "# tasks : %u buffers : %u total_nbuffers : %u\n", ntasks, nbuffers, total_nbuffers);
  180. FPRINTF(stdout, "# ncpus\t");
  181. for (size = mintime; size <= maxtime; size *= factortime)
  182. FPRINTF(stdout, "%u iters(us)\ttotal(s)\t", size);
  183. FPRINTF(stdout, "\n");
  184. FPRINTF(stdout, "\"seq\"\t");
  185. for (size = mintime; size <= maxtime; size *= factortime)
  186. {
  187. double dstart, dend;
  188. dstart = starpu_timing_now();
  189. for (i = 0; i < ntasks; i++)
  190. func(NULL, (void*) (uintptr_t) size);
  191. dend = starpu_timing_now();
  192. FPRINTF(stdout, "%.0f \t%f\t", (dend-dstart)/ntasks, (dend-dstart)/1000000);
  193. }
  194. FPRINTF(stdout, "\n");
  195. fflush(stdout);
  196. starpu_data_handle_t data_handles[total_nbuffers?total_nbuffers:1];
  197. if (nbuffers && !total_nbuffers)
  198. {
  199. fprintf(stderr,"can not have %u buffers with %u total buffers\n", nbuffers, total_nbuffers);
  200. goto error;
  201. }
  202. if (mincpus <= 0)
  203. mincpus = 1;
  204. /* For each number of cpus, benchmark */
  205. for (ncpus= mincpus; ncpus <= maxcpus; ncpus += cpustep)
  206. {
  207. FPRINTF(stdout, "%u\t", ncpus);
  208. fflush(stdout);
  209. conf.ncpus = ncpus;
  210. ret = starpu_init(&conf);
  211. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  212. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  213. for (buffer = 0; buffer < total_nbuffers; buffer++)
  214. starpu_vector_data_register(&data_handles[buffer], STARPU_MAIN_RAM, (uintptr_t)buffers[buffer], 16, sizeof(float));
  215. for (size = mintime; size <= maxtime; size *= factortime)
  216. {
  217. /* submit tasks */
  218. start = starpu_timing_now();
  219. for (i = 0; i < ntasks * ncpus; i++)
  220. {
  221. starpu_data_handle_t *handles;
  222. starpu_task_init(&tasks[i]);
  223. tasks[i].callback_func = NULL;
  224. tasks[i].cl = &codelet;
  225. tasks[i].cl_arg = (void*) (uintptr_t) size;
  226. tasks[i].synchronous = 0;
  227. if (nbuffers > STARPU_NMAXBUFS)
  228. {
  229. tasks[i].dyn_handles = malloc(nbuffers * sizeof(*data_handles));
  230. handles = tasks[i].dyn_handles;
  231. tasks[i].dyn_modes = malloc(nbuffers * sizeof(tasks[i].dyn_modes[0]));
  232. for (buffer = 0; buffer < nbuffers; buffer++)
  233. tasks[i].dyn_modes[buffer] = STARPU_R;
  234. }
  235. else
  236. handles = tasks[i].handles;
  237. if (nbuffers >= total_nbuffers)
  238. for (buffer = 0; buffer < nbuffers; buffer++)
  239. handles[buffer] = data_handles[buffer%total_nbuffers];
  240. else
  241. for (buffer = 0; buffer < nbuffers; buffer++)
  242. handles[buffer] = data_handles[starpu_lrand48()%total_nbuffers];
  243. ret = starpu_task_submit(&tasks[i]);
  244. if (ret == -ENODEV) goto enodev;
  245. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task");
  246. }
  247. ret = starpu_task_wait_for_all();
  248. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  249. end = starpu_timing_now();
  250. for (i = 0; i < ntasks * ncpus; i++)
  251. starpu_task_clean(&tasks[i]);
  252. timing = end - start;
  253. FPRINTF(stdout, "%u\t%f\t", size, timing/ncpus/1000000);
  254. fflush(stdout);
  255. {
  256. char *output_dir = getenv("STARPU_BENCH_DIR");
  257. char *bench_id = getenv("STARPU_BENCH_ID");
  258. char *sched = getenv("STARPU_SCHED");
  259. if (output_dir && bench_id)
  260. {
  261. char file[1024];
  262. FILE *f;
  263. snprintf(file, sizeof(file), "%s/tasks_size_overhead_total%s%s.dat", output_dir, sched?"_":"", sched?sched:"");
  264. f = fopen(file, "a");
  265. fprintf(f, "%s\t%u\t%u\t%f\n", bench_id, ncpus, size, timing/1000000 /(ntasks*ncpus) *1000);
  266. fclose(f);
  267. }
  268. }
  269. }
  270. for (buffer = 0; buffer < total_nbuffers; buffer++)
  271. {
  272. starpu_data_unregister(data_handles[buffer]);
  273. }
  274. starpu_shutdown();
  275. FPRINTF(stdout, "\n");
  276. fflush(stdout);
  277. }
  278. free(tasks);
  279. return EXIT_SUCCESS;
  280. enodev:
  281. fprintf(stderr, "WARNING: No one can execute this task\n");
  282. /* yes, we do not perform the computation but we did detect that no one
  283. * could perform the kernel, so this is not an error from StarPU */
  284. error:
  285. starpu_shutdown();
  286. free(tasks);
  287. return STARPU_TEST_SKIPPED;
  288. }