starpu_perfmodel_plot.c 14 KB

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