starpu_perfmodel_plot.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011,2012,2015 Inria
  4. * Copyright (C) 2011-2017 CNRS
  5. * Copyright (C) 2011-2014,2017 Université de Bordeaux
  6. * Copyright (C) 2013 Thibaut Lambert
  7. * Copyright (C) 2011 Télécom-SudParis
  8. *
  9. * StarPU is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * StarPU is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  19. */
  20. #include <common/config.h>
  21. #include <assert.h>
  22. #include <unistd.h>
  23. #include <stdio.h>
  24. #include <sys/stat.h>
  25. #include <limits.h>
  26. #ifdef STARPU_USE_FXT
  27. #include <common/fxt.h>
  28. #endif
  29. #include <common/utils.h>
  30. #include <starpu.h>
  31. #include <core/perfmodel/perfmodel.h> // we need to browse the list associated to history-based models
  32. #if defined(_WIN32) && !defined(__CYGWIN__)
  33. #include <windows.h>
  34. #endif
  35. #define PROGNAME "starpu_perfmodel_plot"
  36. struct _perfmodel_plot_options
  37. {
  38. /* display all available models */
  39. int list;
  40. /* display directory */
  41. int directory;
  42. /* what kernel ? */
  43. char *symbol;
  44. /* which combination */
  45. int comb_is_set;
  46. int comb;
  47. /* display all available combinations of a specific model */
  48. int list_combs;
  49. int gflops;
  50. /* Unless a FxT file is specified, we just display the model */
  51. int with_fxt_file;
  52. char avg_file_name[256];
  53. #ifdef STARPU_USE_FXT
  54. struct starpu_fxt_codelet_event *dumped_codelets;
  55. struct starpu_fxt_options fxt_options;
  56. char data_file_name[256];
  57. #endif
  58. };
  59. static void usage()
  60. {
  61. fprintf(stderr, "Draw a graph corresponding to the execution time of a given perfmodel\n");
  62. fprintf(stderr, "Usage: %s [ options ]\n", PROGNAME);
  63. fprintf(stderr, "\n");
  64. fprintf(stderr, "One must specify a symbol with the -s option or use -l or -d\n");
  65. fprintf(stderr, "Options:\n");
  66. fprintf(stderr, " -d display the directory storing performance models\n");
  67. fprintf(stderr, " -l display all available models\n");
  68. fprintf(stderr, " -s <symbol> specify the symbol\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], "-c") == 0)
  130. {
  131. options->comb_is_set = 1;
  132. options->comb = atoi(argv[++i]);
  133. continue;
  134. }
  135. if (strcmp(argv[i], "-d") == 0)
  136. {
  137. options->directory = 1;
  138. correct_usage = 1;
  139. continue;
  140. }
  141. if (strcmp(argv[i], "-h") == 0 ||
  142. strcmp(argv[i], "--help") == 0)
  143. {
  144. usage();
  145. exit(EXIT_SUCCESS);
  146. }
  147. if (strcmp(argv[i], "-v") == 0 ||
  148. strcmp(argv[i], "--version") == 0)
  149. {
  150. fputs(PROGNAME " (" PACKAGE_NAME ") " PACKAGE_VERSION "\n", stderr);
  151. exit(EXIT_SUCCESS);
  152. }
  153. /* If the reading_input_filenames flag is set, and that the
  154. * argument does not match an option, we assume this may be
  155. * another filename */
  156. if (reading_input_filenames)
  157. {
  158. #ifdef STARPU_USE_FXT
  159. options->fxt_options.filenames[options->fxt_options.ninputfiles++] = argv[i];
  160. #endif
  161. continue;
  162. }
  163. }
  164. if (correct_usage == 0)
  165. {
  166. fprintf(stderr, "Incorrect usage, aborting\n");
  167. usage();
  168. exit(-1);
  169. }
  170. }
  171. static char *replace_char(char *str, char old, char new)
  172. {
  173. char *p = strdup(str);
  174. char *ptr = p;
  175. while (*ptr)
  176. {
  177. if (*ptr == old) *ptr = new;
  178. ptr ++;
  179. }
  180. return p;
  181. }
  182. static void print_comma(FILE *gnuplot_file, int *first)
  183. {
  184. if (*first)
  185. {
  186. *first = 0;
  187. }
  188. else
  189. {
  190. fprintf(gnuplot_file, ",\\\n\t");
  191. }
  192. }
  193. 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)
  194. {
  195. char arch_name[256];
  196. starpu_perfmodel_get_arch_name(arch, arch_name, 256, impl);
  197. #ifdef STARPU_USE_FXT
  198. if (options->with_fxt_file && impl == 0)
  199. {
  200. if (options->gflops)
  201. {
  202. _STARPU_DISP("gflops unit selected, ignoring fxt trace\n");
  203. }
  204. else
  205. {
  206. char *arch_name2 = replace_char(arch_name, '_', '-');
  207. print_comma(gnuplot_file, first);
  208. fprintf(gnuplot_file, "\"< grep '^%s' %s\" using 3:4 title \"Profiling %s\"", arch_name, options->data_file_name, arch_name2);
  209. free(arch_name2);
  210. }
  211. }
  212. #endif
  213. /* Only display the regression model if we could actually build a model */
  214. if (!options->gflops && arch_model->regression.valid && !arch_model->regression.nl_valid)
  215. {
  216. print_comma(gnuplot_file, first);
  217. fprintf(stderr, "\tLinear: y = alpha size ^ beta\n");
  218. fprintf(stderr, "\t\talpha = %e\n", arch_model->regression.alpha * 0.001);
  219. fprintf(stderr, "\t\tbeta = %e\n", arch_model->regression.beta);
  220. fprintf(gnuplot_file, "0.001 * %f * x ** %f title \"Linear Regression %s\"",
  221. arch_model->regression.alpha, arch_model->regression.beta, arch_name);
  222. }
  223. if (!options->gflops && arch_model->regression.nl_valid)
  224. {
  225. print_comma(gnuplot_file, first);
  226. fprintf(stderr, "\tNon-Linear: y = a size ^b + c\n");
  227. fprintf(stderr, "\t\ta = %e\n", arch_model->regression.a * 0.001);
  228. fprintf(stderr, "\t\tb = %e\n", arch_model->regression.b);
  229. fprintf(stderr, "\t\tc = %e\n", arch_model->regression.c * 0.001);
  230. fprintf(gnuplot_file, "0.001 * %f * x ** %f + 0.001 * %f title \"Non-Linear Regression %s\"",
  231. arch_model->regression.a, arch_model->regression.b, arch_model->regression.c, arch_name);
  232. }
  233. }
  234. static void display_history_based_perf_models(FILE *gnuplot_file, struct starpu_perfmodel *model, int *first, struct _perfmodel_plot_options *options)
  235. {
  236. FILE *datafile;
  237. struct starpu_perfmodel_history_list *ptr;
  238. char arch_name[32];
  239. int col;
  240. unsigned long minimum = 0;
  241. datafile = fopen(options->avg_file_name, "w");
  242. col = 2;
  243. int i;
  244. for(i = 0; i < model->state->ncombs; i++)
  245. {
  246. int comb = model->state->combs[i];
  247. if (options->comb_is_set == 0 || options->comb == comb)
  248. {
  249. struct starpu_perfmodel_arch *arch;
  250. int impl;
  251. arch = starpu_perfmodel_arch_comb_fetch(comb);
  252. for(impl = 0; impl < model->state->nimpls[comb]; impl++)
  253. {
  254. struct starpu_perfmodel_per_arch *arch_model = &model->state->per_arch[comb][impl];
  255. starpu_perfmodel_get_arch_name(arch, arch_name, 32, impl);
  256. if (arch_model->list)
  257. {
  258. char *arch_name2 = replace_char(arch_name, '_', '-');
  259. print_comma(gnuplot_file, first);
  260. fprintf(gnuplot_file, "\"%s\" using 1:%d:%d with errorlines title \"Average %s\"", options->avg_file_name, col, col+1, arch_name2);
  261. col += 2;
  262. free(arch_name2);
  263. }
  264. }
  265. }
  266. }
  267. /* Dump entries in size order */
  268. while (1)
  269. {
  270. unsigned long last = minimum;
  271. minimum = ULONG_MAX;
  272. /* Get the next minimum */
  273. for(i = 0; i < model->state->ncombs; i++)
  274. {
  275. int comb = model->state->combs[i];
  276. if (options->comb_is_set == 0 || options->comb == comb)
  277. {
  278. int impl;
  279. for(impl = 0; impl < model->state->nimpls[comb]; impl++)
  280. {
  281. struct starpu_perfmodel_per_arch *arch_model = &model->state->per_arch[comb][impl];
  282. for (ptr = arch_model->list; ptr; ptr = ptr->next)
  283. {
  284. unsigned long size = ptr->entry->size;
  285. if (size > last && size < minimum)
  286. minimum = size;
  287. }
  288. }
  289. }
  290. }
  291. if (minimum == ULONG_MAX)
  292. break;
  293. fprintf(stderr, "%lu ", minimum);
  294. fprintf(datafile, "%-15lu ", minimum);
  295. for(i = 0; i < model->state->ncombs; i++)
  296. {
  297. int comb = model->state->combs[i];
  298. if (options->comb_is_set == 0 || options->comb == comb)
  299. {
  300. int impl;
  301. for(impl = 0; impl < model->state->nimpls[comb]; impl++)
  302. {
  303. struct starpu_perfmodel_per_arch *arch_model = &model->state->per_arch[comb][impl];
  304. for (ptr = arch_model->list; ptr; ptr = ptr->next)
  305. {
  306. struct starpu_perfmodel_history_entry *entry = ptr->entry;
  307. if (entry->size == minimum)
  308. {
  309. if (options->gflops)
  310. fprintf(datafile, "\t%-15le\t%-15le", entry->flops / (entry->mean * 1000),
  311. entry->flops / ((entry->mean + entry->deviation) * 1000) -
  312. entry->flops / (entry->mean * 1000)
  313. );
  314. else
  315. fprintf(datafile, "\t%-15le\t%-15le", 0.001*entry->mean, 0.001*entry->deviation);
  316. break;
  317. }
  318. }
  319. if (!ptr && arch_model->list)
  320. /* No value for this arch. */
  321. fprintf(datafile, "\t\"\"\t\"\"");
  322. }
  323. }
  324. }
  325. fprintf(datafile, "\n");
  326. }
  327. fprintf(stderr, "\n");
  328. fclose(datafile);
  329. }
  330. static void display_all_perf_models(FILE *gnuplot_file, struct starpu_perfmodel *model, int *first, struct _perfmodel_plot_options *options)
  331. {
  332. int i;
  333. for(i = 0; i < model->state->ncombs; i++)
  334. {
  335. int comb = model->state->combs[i];
  336. if (options->comb_is_set == 0 || options->comb == comb)
  337. {
  338. struct starpu_perfmodel_arch *arch;
  339. int impl;
  340. arch = starpu_perfmodel_arch_comb_fetch(comb);
  341. for(impl = 0; impl < model->state->nimpls[comb]; impl++)
  342. {
  343. struct starpu_perfmodel_per_arch *archmodel = &model->state->per_arch[comb][impl];
  344. display_perf_model(gnuplot_file, arch, archmodel, impl, first, options);
  345. }
  346. }
  347. }
  348. }
  349. #ifdef STARPU_USE_FXT
  350. static void dump_data_file(FILE *data_file, struct _perfmodel_plot_options *options)
  351. {
  352. int i;
  353. for (i = 0; i < options->fxt_options.dumped_codelets_count; i++)
  354. {
  355. /* Dump only if the codelet symbol matches user's request (with or without the machine name) */
  356. char *tmp = strdup(options->symbol);
  357. char *dot = strchr(tmp, '.');
  358. if (dot) tmp[strlen(tmp)-strlen(dot)] = '\0';
  359. if ((strncmp(options->dumped_codelets[i].symbol, options->symbol, (FXT_MAX_PARAMS - 4)*sizeof(unsigned long)-1) == 0)
  360. || (strncmp(options->dumped_codelets[i].symbol, tmp, (FXT_MAX_PARAMS - 4)*sizeof(unsigned long)-1) == 0))
  361. {
  362. char *archname = options->dumped_codelets[i].perfmodel_archname;
  363. size_t size = options->dumped_codelets[i].size;
  364. float time = options->dumped_codelets[i].time;
  365. fprintf(data_file, "%s %f %f\n", archname, (float)size, time);
  366. }
  367. free(tmp);
  368. }
  369. }
  370. #endif
  371. static void display_selected_models(FILE *gnuplot_file, struct starpu_perfmodel *model, struct _perfmodel_plot_options *options)
  372. {
  373. char *symbol = replace_char(options->symbol, '_', '-');
  374. fprintf(gnuplot_file, "#!/usr/bin/gnuplot -persist\n");
  375. fprintf(gnuplot_file, "\n");
  376. fprintf(gnuplot_file, "set term postscript eps enhanced color\n");
  377. fprintf(gnuplot_file, "set output \"starpu_%s.eps\"\n", options->symbol);
  378. fprintf(gnuplot_file, "set title \"Model for codelet %s\"\n", symbol);
  379. fprintf(gnuplot_file, "set xlabel \"Total data size\"\n");
  380. if (options->gflops)
  381. fprintf(gnuplot_file, "set ylabel \"GFlops\"\n");
  382. else
  383. fprintf(gnuplot_file, "set ylabel \"Time (ms)\"\n");
  384. fprintf(gnuplot_file, "\n");
  385. fprintf(gnuplot_file, "set key top left\n");
  386. fprintf(gnuplot_file, "set logscale x\n");
  387. fprintf(gnuplot_file, "set logscale y\n");
  388. fprintf(gnuplot_file, "\n");
  389. /* If no input data is given to gnuplot, we at least need to specify an
  390. * arbitrary range. */
  391. if (options->with_fxt_file == 0 || options->gflops)
  392. fprintf(gnuplot_file, "set xrange [1:10**9]\n\n");
  393. int first = 1;
  394. fprintf(gnuplot_file, "plot\t");
  395. /* display all or selected combinations */
  396. display_all_perf_models(gnuplot_file, model, &first, options);
  397. display_history_based_perf_models(gnuplot_file, model, &first, options);
  398. free(symbol);
  399. }
  400. int main(int argc, char **argv)
  401. {
  402. int ret = 0;
  403. struct starpu_perfmodel model = { .type = STARPU_PERFMODEL_INVALID };
  404. char gnuplot_file_name[256];
  405. struct _perfmodel_plot_options options;
  406. #if defined(_WIN32) && !defined(__CYGWIN__)
  407. WSADATA wsadata;
  408. WSAStartup(MAKEWORD(1,0), &wsadata);
  409. #endif
  410. parse_args(argc, argv, &options);
  411. starpu_perfmodel_initialize();
  412. if (options.directory)
  413. {
  414. starpu_perfmodel_directory(stdout);
  415. }
  416. else if (options.list)
  417. {
  418. ret = starpu_perfmodel_list(stdout);
  419. if (ret)
  420. {
  421. _STARPU_DISP("The performance model directory is invalid\n");
  422. }
  423. }
  424. else
  425. {
  426. /* Load the performance model associated to the symbol */
  427. ret = starpu_perfmodel_load_symbol(options.symbol, &model);
  428. if (ret)
  429. {
  430. _STARPU_DISP("The performance model for the symbol <%s> could not be loaded\n", options.symbol);
  431. }
  432. else if (options.list_combs)
  433. {
  434. ret = starpu_perfmodel_list_combs(stdout, &model);
  435. if (ret)
  436. {
  437. fprintf(stderr, "Error when listing combinations for model <%s>\n", options.symbol);
  438. }
  439. }
  440. else
  441. {
  442. /* If some FxT input was specified, we put the points on the graph */
  443. #ifdef STARPU_USE_FXT
  444. if (options.with_fxt_file)
  445. {
  446. starpu_fxt_generate_trace(&options.fxt_options);
  447. snprintf(options.data_file_name, sizeof(options.data_file_name), "starpu_%s.data", options.symbol);
  448. FILE *data_file = fopen(options.data_file_name, "w+");
  449. STARPU_ASSERT(data_file);
  450. dump_data_file(data_file, &options);
  451. fclose(data_file);
  452. }
  453. #endif
  454. snprintf(gnuplot_file_name, sizeof(gnuplot_file_name), "starpu_%s.gp", options.symbol);
  455. snprintf(options.avg_file_name, sizeof(options.avg_file_name), "starpu_%s_avg.data", options.symbol);
  456. FILE *gnuplot_file = fopen(gnuplot_file_name, "w+");
  457. STARPU_ASSERT(gnuplot_file);
  458. display_selected_models(gnuplot_file, &model, &options);
  459. fprintf(gnuplot_file,"\n");
  460. fclose(gnuplot_file);
  461. /* Retrieve the current mode of the gnuplot executable */
  462. struct stat sb;
  463. ret = stat(gnuplot_file_name, &sb);
  464. if (ret)
  465. {
  466. perror("stat");
  467. STARPU_ABORT();
  468. }
  469. /* Make the gnuplot scrit executable for the owner */
  470. ret = chmod(gnuplot_file_name, sb.st_mode|S_IXUSR);
  471. if (ret)
  472. {
  473. perror("chmod");
  474. STARPU_ABORT();
  475. }
  476. _STARPU_DISP("Gnuplot file <%s> generated\n", gnuplot_file_name);
  477. }
  478. }
  479. starpu_perfmodel_free_sampling_directories();
  480. return ret;
  481. }