starpu_perfmodel_plot.c 16 KB

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