tasks_size_overhead.c 7.9 KB

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