starpu_perfmodel_plot.c 15 KB

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