starpu_perfmodel_plot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2014 Université de Bordeaux 1
  4. * Copyright (C) 2011, 2012, 2013, 2014 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 Télécom-SudParis
  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 <config.h>
  19. #include <assert.h>
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #include <sys/stat.h>
  23. #include <limits.h>
  24. #ifdef STARPU_USE_FXT
  25. #include <common/fxt.h>
  26. #endif
  27. #include <common/utils.h>
  28. #include <starpu.h>
  29. #include <core/perfmodel/perfmodel.h> // we need to browse the list associated to history-based models
  30. #include <core/workers.h>
  31. #ifdef __MINGW32__
  32. #include <windows.h>
  33. #endif
  34. #define PROGNAME "starpu_perfmodel_plot"
  35. struct _perfmodel_plot_options
  36. {
  37. /* display all available models */
  38. int list;
  39. /* what kernel ? */
  40. char *symbol;
  41. /* which combination */
  42. int comb_is_set;
  43. int comb;
  44. /* display all available combinations of a specific model */
  45. int list_combs;
  46. int gflops;
  47. /* Unless a FxT file is specified, we just display the model */
  48. int with_fxt_file;
  49. char avg_file_name[256];
  50. #ifdef STARPU_USE_FXT
  51. struct starpu_fxt_codelet_event *dumped_codelets;
  52. struct starpu_fxt_options fxt_options;
  53. char data_file_name[256];
  54. #endif
  55. };
  56. static void usage()
  57. {
  58. fprintf(stderr, "Draw a graph corresponding to the execution time of a given perfmodel\n");
  59. fprintf(stderr, "Usage: %s [ options ]\n", PROGNAME);
  60. fprintf(stderr, "\n");
  61. fprintf(stderr, "One must specify a symbol with the -s option or use -l\n");
  62. fprintf(stderr, "Options:\n");
  63. fprintf(stderr, " -l display all available models\n");
  64. fprintf(stderr, " -s <symbol> specify the symbol\n");
  65. fprintf(stderr, " -f draw GFlops instead of time\n");
  66. fprintf(stderr, " -i <Fxt files> input FxT files generated by StarPU\n");
  67. fprintf(stderr, " -lc display all combinations of a given model\n");
  68. fprintf(stderr, " -c <combination> specify the combination (use the option -lc to list all combinations of a given model)\n");
  69. fprintf(stderr, " -h, --help display this help and exit\n");
  70. fprintf(stderr, " -v, --version output version information and exit\n\n");
  71. fprintf(stderr, "Report bugs to <%s>.", PACKAGE_BUGREPORT);
  72. fprintf(stderr, "\n");
  73. }
  74. static void parse_args(int argc, char **argv, struct _perfmodel_plot_options *options)
  75. {
  76. memset(options, 0, sizeof(struct _perfmodel_plot_options));
  77. #ifdef STARPU_USE_FXT
  78. /* Default options */
  79. starpu_fxt_options_init(&options->fxt_options);
  80. options->fxt_options.out_paje_path = NULL;
  81. options->fxt_options.activity_path = NULL;
  82. options->fxt_options.distrib_time_path = NULL;
  83. options->fxt_options.dag_path = NULL;
  84. options->fxt_options.dumped_codelets = &options->dumped_codelets;
  85. #endif
  86. /* We want to support arguments such as "-i trace_*" */
  87. unsigned reading_input_filenames = 0;
  88. int i;
  89. for (i = 1; i < argc; i++)
  90. {
  91. if (strcmp(argv[i], "-s") == 0)
  92. {
  93. options->symbol = argv[++i];
  94. continue;
  95. }
  96. if (strcmp(argv[i], "-i") == 0)
  97. {
  98. reading_input_filenames = 1;
  99. #ifdef STARPU_USE_FXT
  100. options->fxt_options.filenames[options->fxt_options.ninputfiles++] = argv[++i];
  101. options->with_fxt_file = 1;
  102. #else
  103. fprintf(stderr, "Warning: FxT support was not enabled in StarPU: FxT traces will thus be ignored!\n");
  104. #endif
  105. continue;
  106. }
  107. if (strcmp(argv[i], "-l") == 0)
  108. {
  109. options->list = 1;
  110. continue;
  111. }
  112. if (strcmp(argv[i], "-lc") == 0)
  113. {
  114. options->list_combs = 1;
  115. continue;
  116. }
  117. if (strcmp(argv[i], "-f") == 0)
  118. {
  119. options->gflops = 1;
  120. continue;
  121. }
  122. if (strcmp(argv[i], "-c") == 0)
  123. {
  124. options->comb_is_set = 1;
  125. options->comb = atoi(argv[++i]);
  126. continue;
  127. }
  128. if (strcmp(argv[i], "-h") == 0 ||
  129. strcmp(argv[i], "--help") == 0)
  130. {
  131. usage();
  132. exit(EXIT_SUCCESS);
  133. }
  134. if (strcmp(argv[i], "-v") == 0 ||
  135. strcmp(argv[i], "--version") == 0)
  136. {
  137. fputs(PROGNAME " (" PACKAGE_NAME ") " PACKAGE_VERSION "\n", stderr);
  138. exit(EXIT_SUCCESS);
  139. }
  140. /* If the reading_input_filenames flag is set, and that the
  141. * argument does not match an option, we assume this may be
  142. * another filename */
  143. if (reading_input_filenames)
  144. {
  145. #ifdef STARPU_USE_FXT
  146. options->fxt_options.filenames[options->fxt_options.ninputfiles++] = argv[i];
  147. #endif
  148. continue;
  149. }
  150. }
  151. if ((!options->symbol && !options->list) || (options->list_combs && !options->symbol))
  152. {
  153. fprintf(stderr, "Incorrect usage, aborting\n");
  154. usage();
  155. exit(-1);
  156. }
  157. }
  158. static char *replace_char(char *str, char old, char new)
  159. {
  160. char *p = strdup(str);
  161. char *ptr = p;
  162. while (*ptr)
  163. {
  164. if (*ptr == old) *ptr = new;
  165. ptr ++;
  166. }
  167. return p;
  168. }
  169. static void print_comma(FILE *gnuplot_file, int *first)
  170. {
  171. if (*first)
  172. {
  173. *first = 0;
  174. }
  175. else
  176. {
  177. fprintf(gnuplot_file, ",\\\n\t");
  178. }
  179. }
  180. static void display_perf_model(FILE *gnuplot_file, struct starpu_perfmodel *model, struct starpu_perfmodel_arch* arch, struct starpu_perfmodel_per_arch *arch_model, int comb, int impl, int *first, struct _perfmodel_plot_options *options)
  181. {
  182. char arch_name[256];
  183. starpu_perfmodel_get_arch_name(arch, arch_name, 256, impl);
  184. #ifdef STARPU_USE_FXT
  185. if (!options->gflops && options->with_fxt_file && impl == 0)
  186. {
  187. // print_comma(gnuplot_file, first);
  188. // fprintf(gnuplot_file, "\"< grep -w \\^%d_%d_%d %s\" using 2:3 title \"Profiling %s\"", arch->type, arch->devid, arch->ncore, options->data_file_name, arch_name);
  189. }
  190. #endif
  191. /* Only display the regression model if we could actually build a model */
  192. if (!options->gflops && arch_model->regression.valid && !arch_model->regression.nl_valid)
  193. {
  194. print_comma(gnuplot_file, first);
  195. fprintf(stderr, "\tLinear: y = alpha size ^ beta\n");
  196. fprintf(stderr, "\t\talpha = %e\n", arch_model->regression.alpha * 0.001);
  197. fprintf(stderr, "\t\tbeta = %e\n", arch_model->regression.beta);
  198. fprintf(gnuplot_file, "0.001 * %f * x ** %f title \"Linear Regression %s\"",
  199. arch_model->regression.alpha, arch_model->regression.beta, arch_name);
  200. }
  201. if (!options->gflops && arch_model->regression.nl_valid)
  202. {
  203. print_comma(gnuplot_file, first);
  204. fprintf(stderr, "\tNon-Linear: y = a size ^b + c\n");
  205. fprintf(stderr, "\t\ta = %e\n", arch_model->regression.a * 0.001);
  206. fprintf(stderr, "\t\tb = %e\n", arch_model->regression.b);
  207. fprintf(stderr, "\t\tc = %e\n", arch_model->regression.c * 0.001);
  208. fprintf(gnuplot_file, "0.001 * %f * x ** %f + 0.001 * %f title \"Non-Linear Regression %s\"",
  209. arch_model->regression.a, arch_model->regression.b, arch_model->regression.c, arch_name);
  210. }
  211. }
  212. static void display_history_based_perf_models(FILE *gnuplot_file, struct starpu_perfmodel *model, int *first, struct _perfmodel_plot_options *options)
  213. {
  214. FILE *datafile;
  215. struct starpu_perfmodel_history_list *ptr;
  216. char arch_name[32];
  217. int col;
  218. unsigned long last, minimum = 0;
  219. datafile = fopen(options->avg_file_name, "w");
  220. col = 2;
  221. int i;
  222. for(i = 0; i < model->state->ncombs; i++)
  223. {
  224. int comb = model->state->combs[i];
  225. if (options->comb_is_set == 0 || options->comb == comb)
  226. {
  227. struct starpu_perfmodel_arch *arch;
  228. int impl;
  229. arch = _starpu_arch_comb_get(comb);
  230. for(impl = 0; impl < model->state->nimpls[i]; impl++)
  231. {
  232. struct starpu_perfmodel_per_arch *arch_model = &model->state->per_arch[comb][impl];
  233. starpu_perfmodel_get_arch_name(arch, arch_name, 32, impl);
  234. if (arch_model->list)
  235. {
  236. print_comma(gnuplot_file, first);
  237. fprintf(gnuplot_file, "\"%s\" using 1:%d:%d with errorlines title \"Average %s\"", options->avg_file_name, col, col+1, replace_char(arch_name, '_', '-'));
  238. col += 2;
  239. }
  240. }
  241. }
  242. }
  243. /* Dump entries in size order */
  244. while (1)
  245. {
  246. last = minimum;
  247. minimum = ULONG_MAX;
  248. /* Get the next minimum */
  249. for(i = 0; i < model->state->ncombs; i++)
  250. {
  251. int comb = model->state->combs[i];
  252. if (options->comb_is_set == 0 || options->comb == comb)
  253. {
  254. int impl;
  255. for(impl = 0; impl < model->state->nimpls[i]; impl++)
  256. {
  257. struct starpu_perfmodel_per_arch *arch_model = &model->state->per_arch[comb][impl];
  258. for (ptr = arch_model->list; ptr; ptr = ptr->next)
  259. {
  260. unsigned long size = ptr->entry->size;
  261. if (size > last && size < minimum)
  262. minimum = size;
  263. }
  264. }
  265. }
  266. }
  267. if (minimum == ULONG_MAX)
  268. break;
  269. fprintf(stderr, "%lu ", minimum);
  270. fprintf(datafile, "%-15lu ", minimum);
  271. for(i = 0; i < model->state->ncombs; i++)
  272. {
  273. int comb = model->state->combs[i];
  274. if (options->comb_is_set == 0 || options->comb == comb)
  275. {
  276. int impl;
  277. for(impl = 0; impl < model->state->nimpls[i]; impl++)
  278. {
  279. struct starpu_perfmodel_per_arch *arch_model = &model->state->per_arch[comb][impl];
  280. for (ptr = arch_model->list; ptr; ptr = ptr->next)
  281. {
  282. struct starpu_perfmodel_history_entry *entry = ptr->entry;
  283. if (entry->size == minimum)
  284. {
  285. if (options->gflops)
  286. fprintf(datafile, "\t%-15le\t%-15le", entry->flops / (entry->mean * 1000),
  287. entry->flops / ((entry->mean + entry->deviation) * 1000) -
  288. entry->flops / (entry->mean * 1000)
  289. );
  290. else
  291. fprintf(datafile, "\t%-15le\t%-15le", 0.001*entry->mean, 0.001*entry->deviation);
  292. break;
  293. }
  294. }
  295. if (!ptr && arch_model->list)
  296. /* No value for this arch. */
  297. fprintf(datafile, "\t\"\"\t\"\"");
  298. }
  299. fprintf(datafile, "\n");
  300. }
  301. }
  302. }
  303. fprintf(stderr, "\n");
  304. fclose(datafile);
  305. }
  306. static void display_all_perf_models(FILE *gnuplot_file, struct starpu_perfmodel *model, int *first, struct _perfmodel_plot_options *options)
  307. {
  308. int i;
  309. for(i = 0; i < model->state->ncombs; i++)
  310. {
  311. int comb = model->state->combs[i];
  312. if (options->comb_is_set == 0 || options->comb == comb)
  313. {
  314. struct starpu_perfmodel_arch *arch;
  315. int impl;
  316. arch = _starpu_arch_comb_get(comb);
  317. for(impl = 0; impl < model->state->nimpls[i]; impl++)
  318. {
  319. struct starpu_perfmodel_per_arch *archmodel = &model->state->per_arch[comb][impl];
  320. display_perf_model(gnuplot_file, model, arch, archmodel, comb, impl, first, options);
  321. }
  322. }
  323. }
  324. }
  325. #ifdef STARPU_USE_FXT
  326. static void dump_data_file(FILE *data_file, struct starpu_perfmodel *model, struct _perfmodel_plot_options *options)
  327. {
  328. int i;
  329. for (i = 0; i < options->fxt_options.dumped_codelets_count; i++)
  330. {
  331. /* Dump only if the symbol matches user's request */
  332. if (strncmp(options->dumped_codelets[i].symbol, options->symbol, (FXT_MAX_PARAMS - 4)*sizeof(unsigned long)-1) == 0)
  333. {
  334. struct starpu_perfmodel_arch* arch = &options->dumped_codelets[i].arch;
  335. size_t size = options->dumped_codelets[i].size;
  336. float time = options->dumped_codelets[i].time;
  337. // fprintf(data_file, "%d_%d_%d %f %f\n", arch->type, arch->devid, arch->ncore, (float)size, time);
  338. }
  339. }
  340. }
  341. #endif
  342. static void display_selected_models(FILE *gnuplot_file, struct starpu_perfmodel *model, struct _perfmodel_plot_options *options)
  343. {
  344. fprintf(gnuplot_file, "#!/usr/bin/gnuplot -persist\n");
  345. fprintf(gnuplot_file, "\n");
  346. fprintf(gnuplot_file, "set term postscript eps enhanced color\n");
  347. fprintf(gnuplot_file, "set output \"starpu_%s.eps\"\n", options->symbol);
  348. fprintf(gnuplot_file, "set title \"Model for codelet %s\"\n", replace_char(options->symbol, '_', '-'));
  349. fprintf(gnuplot_file, "set xlabel \"Total data size\"\n");
  350. if (options->gflops)
  351. fprintf(gnuplot_file, "set ylabel \"GFlops\"\n");
  352. else
  353. fprintf(gnuplot_file, "set ylabel \"Time (ms)\"\n");
  354. fprintf(gnuplot_file, "\n");
  355. fprintf(gnuplot_file, "set key top left\n");
  356. fprintf(gnuplot_file, "set logscale x\n");
  357. fprintf(gnuplot_file, "set logscale y\n");
  358. fprintf(gnuplot_file, "\n");
  359. /* If no input data is given to gnuplot, we at least need to specify an
  360. * arbitrary range. */
  361. if (options->with_fxt_file == 0)
  362. fprintf(gnuplot_file, "set xrange [1:10**9]\n\n");
  363. int first = 1;
  364. fprintf(gnuplot_file, "plot\t");
  365. /* display all or selected combinations */
  366. display_all_perf_models(gnuplot_file, model, &first, options);
  367. display_history_based_perf_models(gnuplot_file, model, &first, options);
  368. }
  369. int main(int argc, char **argv)
  370. {
  371. int ret = 0;
  372. struct starpu_perfmodel model = {};
  373. char gnuplot_file_name[256];
  374. struct _perfmodel_plot_options options;
  375. #ifdef __MINGW32__
  376. WSADATA wsadata;
  377. WSAStartup(MAKEWORD(1,0), &wsadata);
  378. #endif
  379. parse_args(argc, argv, &options);
  380. if (options.list)
  381. {
  382. ret = starpu_perfmodel_list(stdout);
  383. if (ret)
  384. {
  385. fprintf(stderr, "The performance model directory is invalid\n");
  386. return 1;
  387. }
  388. return 0;
  389. }
  390. /* Load the performance model associated to the symbol */
  391. ret = starpu_perfmodel_load_symbol(options.symbol, &model);
  392. if (ret == 1)
  393. {
  394. fprintf(stderr, "The performance model for the symbol <%s> could not be loaded\n", options.symbol);
  395. return 1;
  396. }
  397. if (options.list_combs)
  398. {
  399. ret = starpu_perfmodel_list_combs(stdout, &model);
  400. if (ret)
  401. {
  402. fprintf(stderr, "Error when listing combinations for model <%s>\n", options.symbol);
  403. return 1;
  404. }
  405. return 0;
  406. }
  407. /* If some FxT input was specified, we put the points on the graph */
  408. #ifdef STARPU_USE_FXT
  409. if (options.with_fxt_file)
  410. {
  411. starpu_fxt_generate_trace(&options.fxt_options);
  412. snprintf(options.data_file_name, 256, "starpu_%s.data", options.symbol);
  413. FILE *data_file = fopen(options.data_file_name, "w+");
  414. STARPU_ASSERT(data_file);
  415. dump_data_file(data_file, &model, &options);
  416. fclose(data_file);
  417. }
  418. #endif
  419. snprintf(gnuplot_file_name, 256, "starpu_%s.gp", options.symbol);
  420. snprintf(options.avg_file_name, 256, "starpu_%s_avg.data", options.symbol);
  421. FILE *gnuplot_file = fopen(gnuplot_file_name, "w+");
  422. STARPU_ASSERT(gnuplot_file);
  423. display_selected_models(gnuplot_file, &model, &options);
  424. fprintf(gnuplot_file,"\n");
  425. fclose(gnuplot_file);
  426. /* Retrieve the current mode of the gnuplot executable */
  427. struct stat sb;
  428. ret = stat(gnuplot_file_name, &sb);
  429. if (ret)
  430. {
  431. perror("stat");
  432. STARPU_ABORT();
  433. }
  434. /* Make the gnuplot scrit executable for the owner */
  435. ret = chmod(gnuplot_file_name, sb.st_mode|S_IXUSR);
  436. if (ret)
  437. {
  438. perror("chmod");
  439. STARPU_ABORT();
  440. }
  441. _STARPU_DISP("Gnuplot file <%s> generated\n", gnuplot_file_name);
  442. return 0;
  443. }