starpu_perfmodel_plot.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 void print_comma(FILE *gnuplot_file, int *first)
  159. {
  160. if (*first)
  161. {
  162. *first = 0;
  163. }
  164. else
  165. {
  166. fprintf(gnuplot_file, ",\\\n\t");
  167. }
  168. }
  169. 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)
  170. {
  171. char arch_name[256];
  172. starpu_perfmodel_get_arch_name(arch, arch_name, 256, impl);
  173. #ifdef STARPU_USE_FXT
  174. if (!options->gflops && options->with_fxt_file && impl == 0)
  175. {
  176. // print_comma(gnuplot_file, first);
  177. // 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);
  178. }
  179. #endif
  180. /* Only display the regression model if we could actually build a model */
  181. if (!options->gflops && arch_model->regression.valid && !arch_model->regression.nl_valid)
  182. {
  183. print_comma(gnuplot_file, first);
  184. fprintf(stderr, "\tLinear: y = alpha size ^ beta\n");
  185. fprintf(stderr, "\t\talpha = %e\n", arch_model->regression.alpha * 0.001);
  186. fprintf(stderr, "\t\tbeta = %e\n", arch_model->regression.beta);
  187. fprintf(gnuplot_file, "0.001 * %f * x ** %f title \"Linear Regression %s\"",
  188. arch_model->regression.alpha, arch_model->regression.beta, arch_name);
  189. }
  190. if (!options->gflops && arch_model->regression.nl_valid)
  191. {
  192. print_comma(gnuplot_file, first);
  193. fprintf(stderr, "\tNon-Linear: y = a size ^b + c\n");
  194. fprintf(stderr, "\t\ta = %e\n", arch_model->regression.a * 0.001);
  195. fprintf(stderr, "\t\tb = %e\n", arch_model->regression.b);
  196. fprintf(stderr, "\t\tc = %e\n", arch_model->regression.c * 0.001);
  197. fprintf(gnuplot_file, "0.001 * %f * x ** %f + 0.001 * %f title \"Non-Linear Regression %s\"",
  198. arch_model->regression.a, arch_model->regression.b, arch_model->regression.c, arch_name);
  199. }
  200. }
  201. static void display_history_based_perf_models(FILE *gnuplot_file, struct starpu_perfmodel *model, int *first, struct _perfmodel_plot_options *options)
  202. {
  203. FILE *datafile;
  204. struct starpu_perfmodel_history_list *ptr;
  205. char arch_name[32];
  206. int col;
  207. unsigned long last, minimum = 0;
  208. datafile = fopen(options->avg_file_name, "w");
  209. col = 2;
  210. int i;
  211. for(i = 0; i < model->ncombs; i++)
  212. {
  213. int comb = model->combs[i];
  214. if (options->comb_is_set == 0 || options->comb == comb)
  215. {
  216. struct starpu_perfmodel_arch *arch;
  217. int impl;
  218. arch = _starpu_arch_comb_get(comb);
  219. for(impl = 0; impl < model->nimpls[i]; impl++)
  220. {
  221. struct starpu_perfmodel_per_arch *arch_model = &model->per_arch[comb][impl];
  222. starpu_perfmodel_get_arch_name(arch, arch_name, 32, impl);
  223. if (arch_model->list)
  224. {
  225. print_comma(gnuplot_file, first);
  226. fprintf(gnuplot_file, "\"%s\" using 1:%d:%d with errorlines title \"Average %s\"", options->avg_file_name, col, col+1, arch_name);
  227. col += 2;
  228. }
  229. }
  230. }
  231. }
  232. /* Dump entries in size order */
  233. while (1)
  234. {
  235. last = minimum;
  236. minimum = ULONG_MAX;
  237. /* Get the next minimum */
  238. for(i = 0; i < model->ncombs; i++)
  239. {
  240. int comb = model->combs[i];
  241. if (options->comb_is_set == 0 || options->comb == comb)
  242. {
  243. int impl;
  244. for(impl = 0; impl < model->nimpls[i]; impl++)
  245. {
  246. struct starpu_perfmodel_per_arch *arch_model = &model->per_arch[comb][impl];
  247. for (ptr = arch_model->list; ptr; ptr = ptr->next)
  248. {
  249. unsigned long size = ptr->entry->size;
  250. if (size > last && size < minimum)
  251. minimum = size;
  252. }
  253. }
  254. }
  255. }
  256. if (minimum == ULONG_MAX)
  257. break;
  258. fprintf(stderr, "%lu ", minimum);
  259. fprintf(datafile, "%-15lu ", minimum);
  260. for(i = 0; i < model->ncombs; i++)
  261. {
  262. int comb = model->combs[i];
  263. if (options->comb_is_set == 0 || options->comb == comb)
  264. {
  265. int impl;
  266. for(impl = 0; impl < model->nimpls[i]; impl++)
  267. {
  268. struct starpu_perfmodel_per_arch *arch_model = &model->per_arch[comb][impl];
  269. for (ptr = arch_model->list; ptr; ptr = ptr->next)
  270. {
  271. struct starpu_perfmodel_history_entry *entry = ptr->entry;
  272. if (entry->size == minimum)
  273. {
  274. if (options->gflops)
  275. fprintf(datafile, "\t%-15le\t%-15le", entry->flops / (entry->mean * 1000),
  276. entry->flops / ((entry->mean + entry->deviation) * 1000) -
  277. entry->flops / (entry->mean * 1000)
  278. );
  279. else
  280. fprintf(datafile, "\t%-15le\t%-15le", 0.001*entry->mean, 0.001*entry->deviation);
  281. break;
  282. }
  283. }
  284. if (!ptr && arch_model->list)
  285. /* No value for this arch. */
  286. fprintf(datafile, "\t\"\"\t\"\"");
  287. }
  288. fprintf(datafile, "\n");
  289. }
  290. }
  291. }
  292. fprintf(stderr, "\n");
  293. fclose(datafile);
  294. }
  295. static void display_all_perf_models(FILE *gnuplot_file, struct starpu_perfmodel *model, int *first, struct _perfmodel_plot_options *options)
  296. {
  297. int i;
  298. for(i = 0; i < model->ncombs; i++)
  299. {
  300. int comb = model->combs[i];
  301. if (options->comb_is_set == 0 || options->comb == comb)
  302. {
  303. struct starpu_perfmodel_arch *arch;
  304. int impl;
  305. arch = _starpu_arch_comb_get(comb);
  306. for(impl = 0; impl < model->nimpls[i]; impl++)
  307. {
  308. struct starpu_perfmodel_per_arch *archmodel = &model->per_arch[comb][impl];
  309. display_perf_model(gnuplot_file, model, arch, archmodel, comb, impl, first, options);
  310. }
  311. }
  312. }
  313. }
  314. #ifdef STARPU_USE_FXT
  315. static void dump_data_file(FILE *data_file, struct starpu_perfmodel *model, struct _perfmodel_plot_options *options)
  316. {
  317. int i;
  318. for (i = 0; i < options->fxt_options.dumped_codelets_count; i++)
  319. {
  320. /* Dump only if the symbol matches user's request */
  321. if (strncmp(options->dumped_codelets[i].symbol, options->symbol, (FXT_MAX_PARAMS - 4)*sizeof(unsigned long)-1) == 0)
  322. {
  323. struct starpu_perfmodel_arch* arch = &options->dumped_codelets[i].arch;
  324. size_t size = options->dumped_codelets[i].size;
  325. float time = options->dumped_codelets[i].time;
  326. // fprintf(data_file, "%d_%d_%d %f %f\n", arch->type, arch->devid, arch->ncore, (float)size, time);
  327. }
  328. }
  329. }
  330. #endif
  331. static void display_selected_models(FILE *gnuplot_file, struct starpu_perfmodel *model, struct _perfmodel_plot_options *options)
  332. {
  333. fprintf(gnuplot_file, "#!/usr/bin/gnuplot -persist\n");
  334. fprintf(gnuplot_file, "\n");
  335. fprintf(gnuplot_file, "set term postscript eps enhanced color\n");
  336. fprintf(gnuplot_file, "set output \"starpu_%s.eps\"\n", options->symbol);
  337. fprintf(gnuplot_file, "set title \"Model for codelet %s\"\n", options->symbol);
  338. fprintf(gnuplot_file, "set xlabel \"Total data size\"\n");
  339. if (options->gflops)
  340. fprintf(gnuplot_file, "set ylabel \"GFlops\"\n");
  341. else
  342. fprintf(gnuplot_file, "set ylabel \"Time (ms)\"\n");
  343. fprintf(gnuplot_file, "\n");
  344. fprintf(gnuplot_file, "set key top left\n");
  345. fprintf(gnuplot_file, "set logscale x\n");
  346. fprintf(gnuplot_file, "set logscale y\n");
  347. fprintf(gnuplot_file, "\n");
  348. /* If no input data is given to gnuplot, we at least need to specify an
  349. * arbitrary range. */
  350. if (options->with_fxt_file == 0)
  351. fprintf(gnuplot_file, "set xrange [1:10**9]\n\n");
  352. int first = 1;
  353. fprintf(gnuplot_file, "plot\t");
  354. /* display all or selected combinations */
  355. display_all_perf_models(gnuplot_file, model, &first, options);
  356. display_history_based_perf_models(gnuplot_file, model, &first, options);
  357. }
  358. int main(int argc, char **argv)
  359. {
  360. int ret = 0;
  361. struct starpu_perfmodel model = {};
  362. char gnuplot_file_name[256];
  363. struct _perfmodel_plot_options options;
  364. #ifdef __MINGW32__
  365. WSADATA wsadata;
  366. WSAStartup(MAKEWORD(1,0), &wsadata);
  367. #endif
  368. parse_args(argc, argv, &options);
  369. if (options.list)
  370. {
  371. ret = starpu_perfmodel_list(stdout);
  372. if (ret)
  373. {
  374. fprintf(stderr, "The performance model directory is invalid\n");
  375. return 1;
  376. }
  377. return 0;
  378. }
  379. /* Load the performance model associated to the symbol */
  380. ret = starpu_perfmodel_load_symbol(options.symbol, &model);
  381. if (ret == 1)
  382. {
  383. fprintf(stderr, "The performance model for the symbol <%s> could not be loaded\n", options.symbol);
  384. return 1;
  385. }
  386. if (options.list_combs)
  387. {
  388. ret = starpu_perfmodel_list_combs(stdout, &model);
  389. if (ret)
  390. {
  391. fprintf(stderr, "Error when listing combinations for model <%s>\n", options.symbol);
  392. return 1;
  393. }
  394. return 0;
  395. }
  396. /* If some FxT input was specified, we put the points on the graph */
  397. #ifdef STARPU_USE_FXT
  398. if (options.with_fxt_file)
  399. {
  400. starpu_fxt_generate_trace(&options.fxt_options);
  401. snprintf(options.data_file_name, 256, "starpu_%s.data", options.symbol);
  402. FILE *data_file = fopen(options.data_file_name, "w+");
  403. STARPU_ASSERT(data_file);
  404. dump_data_file(data_file, &model, &options);
  405. fclose(data_file);
  406. }
  407. #endif
  408. snprintf(gnuplot_file_name, 256, "starpu_%s.gp", options.symbol);
  409. snprintf(options.avg_file_name, 256, "starpu_%s_avg.data", options.symbol);
  410. FILE *gnuplot_file = fopen(gnuplot_file_name, "w+");
  411. STARPU_ASSERT(gnuplot_file);
  412. display_selected_models(gnuplot_file, &model, &options);
  413. fprintf(gnuplot_file,"\n");
  414. fclose(gnuplot_file);
  415. /* Retrieve the current mode of the gnuplot executable */
  416. struct stat sb;
  417. ret = stat(gnuplot_file_name, &sb);
  418. if (ret)
  419. {
  420. perror("stat");
  421. STARPU_ABORT();
  422. }
  423. /* Make the gnuplot scrit executable for the owner */
  424. ret = chmod(gnuplot_file_name, sb.st_mode|S_IXUSR);
  425. if (ret)
  426. {
  427. perror("chmod");
  428. STARPU_ABORT();
  429. }
  430. _STARPU_DISP("Gnuplot file <%s> generated\n", gnuplot_file_name);
  431. return 0;
  432. }