tasks_size_overhead.c 8.2 KB

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