starpu_perfmodel_plot.c 16 KB

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