starpu_perfmodel_plot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2014 Université de Bordeaux
  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. #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->gflops && options->with_fxt_file && impl == 0)
  197. {
  198. print_comma(gnuplot_file, first);
  199. fprintf(gnuplot_file, "\"< grep -w \\^%s %s\" using 2:3 title \"Profiling %s\"", arch_name, options->data_file_name, replace_char(arch_name, '_', '-'));
  200. }
  201. #endif
  202. /* Only display the regression model if we could actually build a model */
  203. if (!options->gflops && arch_model->regression.valid && !arch_model->regression.nl_valid)
  204. {
  205. print_comma(gnuplot_file, first);
  206. fprintf(stderr, "\tLinear: y = alpha size ^ beta\n");
  207. fprintf(stderr, "\t\talpha = %e\n", arch_model->regression.alpha * 0.001);
  208. fprintf(stderr, "\t\tbeta = %e\n", arch_model->regression.beta);
  209. fprintf(gnuplot_file, "0.001 * %f * x ** %f title \"Linear Regression %s\"",
  210. arch_model->regression.alpha, arch_model->regression.beta, arch_name);
  211. }
  212. if (!options->gflops && arch_model->regression.nl_valid)
  213. {
  214. print_comma(gnuplot_file, first);
  215. fprintf(stderr, "\tNon-Linear: y = a size ^b + c\n");
  216. fprintf(stderr, "\t\ta = %e\n", arch_model->regression.a * 0.001);
  217. fprintf(stderr, "\t\tb = %e\n", arch_model->regression.b);
  218. fprintf(stderr, "\t\tc = %e\n", arch_model->regression.c * 0.001);
  219. fprintf(gnuplot_file, "0.001 * %f * x ** %f + 0.001 * %f title \"Non-Linear Regression %s\"",
  220. arch_model->regression.a, arch_model->regression.b, arch_model->regression.c, arch_name);
  221. }
  222. }
  223. static void display_history_based_perf_models(FILE *gnuplot_file, struct starpu_perfmodel *model, int *first, struct _perfmodel_plot_options *options)
  224. {
  225. FILE *datafile;
  226. struct starpu_perfmodel_history_list *ptr;
  227. char arch_name[32];
  228. int col;
  229. unsigned long last, minimum = 0;
  230. datafile = fopen(options->avg_file_name, "w");
  231. col = 2;
  232. int i;
  233. for(i = 0; i < model->state->ncombs; i++)
  234. {
  235. int comb = model->state->combs[i];
  236. if (options->comb_is_set == 0 || options->comb == comb)
  237. {
  238. struct starpu_perfmodel_arch *arch;
  239. int impl;
  240. arch = _starpu_arch_comb_get(comb);
  241. for(impl = 0; impl < model->state->nimpls[comb]; impl++)
  242. {
  243. struct starpu_perfmodel_per_arch *arch_model = &model->state->per_arch[comb][impl];
  244. starpu_perfmodel_get_arch_name(arch, arch_name, 32, impl);
  245. if (arch_model->list)
  246. {
  247. print_comma(gnuplot_file, first);
  248. fprintf(gnuplot_file, "\"%s\" using 1:%d:%d with errorlines title \"Average %s\"", options->avg_file_name, col, col+1, replace_char(arch_name, '_', '-'));
  249. col += 2;
  250. }
  251. }
  252. }
  253. }
  254. /* Dump entries in size order */
  255. while (1)
  256. {
  257. last = minimum;
  258. minimum = ULONG_MAX;
  259. /* Get the next minimum */
  260. for(i = 0; i < model->state->ncombs; i++)
  261. {
  262. int comb = model->state->combs[i];
  263. if (options->comb_is_set == 0 || options->comb == comb)
  264. {
  265. int impl;
  266. for(impl = 0; impl < model->state->nimpls[comb]; impl++)
  267. {
  268. struct starpu_perfmodel_per_arch *arch_model = &model->state->per_arch[comb][impl];
  269. for (ptr = arch_model->list; ptr; ptr = ptr->next)
  270. {
  271. unsigned long size = ptr->entry->size;
  272. if (size > last && size < minimum)
  273. minimum = size;
  274. }
  275. }
  276. }
  277. }
  278. if (minimum == ULONG_MAX)
  279. break;
  280. fprintf(stderr, "%lu ", minimum);
  281. fprintf(datafile, "%-15lu ", minimum);
  282. for(i = 0; i < model->state->ncombs; i++)
  283. {
  284. int comb = model->state->combs[i];
  285. if (options->comb_is_set == 0 || options->comb == comb)
  286. {
  287. int impl;
  288. for(impl = 0; impl < model->state->nimpls[comb]; impl++)
  289. {
  290. struct starpu_perfmodel_per_arch *arch_model = &model->state->per_arch[comb][impl];
  291. for (ptr = arch_model->list; ptr; ptr = ptr->next)
  292. {
  293. struct starpu_perfmodel_history_entry *entry = ptr->entry;
  294. if (entry->size == minimum)
  295. {
  296. if (options->gflops)
  297. fprintf(datafile, "\t%-15le\t%-15le", entry->flops / (entry->mean * 1000),
  298. entry->flops / ((entry->mean + entry->deviation) * 1000) -
  299. entry->flops / (entry->mean * 1000)
  300. );
  301. else
  302. fprintf(datafile, "\t%-15le\t%-15le", 0.001*entry->mean, 0.001*entry->deviation);
  303. break;
  304. }
  305. }
  306. if (!ptr && arch_model->list)
  307. /* No value for this arch. */
  308. fprintf(datafile, "\t\"\"\t\"\"");
  309. }
  310. }
  311. }
  312. fprintf(datafile, "\n");
  313. }
  314. fprintf(stderr, "\n");
  315. fclose(datafile);
  316. }
  317. static void display_all_perf_models(FILE *gnuplot_file, struct starpu_perfmodel *model, int *first, struct _perfmodel_plot_options *options)
  318. {
  319. int i;
  320. for(i = 0; i < model->state->ncombs; i++)
  321. {
  322. int comb = model->state->combs[i];
  323. if (options->comb_is_set == 0 || options->comb == comb)
  324. {
  325. struct starpu_perfmodel_arch *arch;
  326. int impl;
  327. arch = _starpu_arch_comb_get(comb);
  328. for(impl = 0; impl < model->state->nimpls[comb]; impl++)
  329. {
  330. struct starpu_perfmodel_per_arch *archmodel = &model->state->per_arch[comb][impl];
  331. display_perf_model(gnuplot_file, arch, archmodel, impl, first, options);
  332. }
  333. }
  334. }
  335. }
  336. #ifdef STARPU_USE_FXT
  337. static void dump_data_file(FILE *data_file, struct _perfmodel_plot_options *options)
  338. {
  339. int i;
  340. for (i = 0; i < options->fxt_options.dumped_codelets_count; i++)
  341. {
  342. /* Dump only if the codelet symbol matches user's request (with or without the machine name) */
  343. char *tmp = strdup(options->symbol);
  344. char *dot = strchr(tmp, '.');
  345. if (dot) tmp[strlen(tmp)-strlen(dot)] = '\0';
  346. if ((strncmp(options->dumped_codelets[i].symbol, options->symbol, (FXT_MAX_PARAMS - 4)*sizeof(unsigned long)-1) == 0)
  347. || (strncmp(options->dumped_codelets[i].symbol, tmp, (FXT_MAX_PARAMS - 4)*sizeof(unsigned long)-1) == 0))
  348. {
  349. char *archname = options->dumped_codelets[i].perfmodel_archname;
  350. size_t size = options->dumped_codelets[i].size;
  351. float time = options->dumped_codelets[i].time;
  352. fprintf(data_file, "%s %f %f\n", archname, (float)size, time);
  353. }
  354. free(tmp);
  355. }
  356. }
  357. #endif
  358. static void display_selected_models(FILE *gnuplot_file, struct starpu_perfmodel *model, struct _perfmodel_plot_options *options)
  359. {
  360. fprintf(gnuplot_file, "#!/usr/bin/gnuplot -persist\n");
  361. fprintf(gnuplot_file, "\n");
  362. fprintf(gnuplot_file, "set term postscript eps enhanced color\n");
  363. fprintf(gnuplot_file, "set output \"starpu_%s.eps\"\n", options->symbol);
  364. fprintf(gnuplot_file, "set title \"Model for codelet %s\"\n", replace_char(options->symbol, '_', '-'));
  365. fprintf(gnuplot_file, "set xlabel \"Total data size\"\n");
  366. if (options->gflops)
  367. fprintf(gnuplot_file, "set ylabel \"GFlops\"\n");
  368. else
  369. fprintf(gnuplot_file, "set ylabel \"Time (ms)\"\n");
  370. fprintf(gnuplot_file, "\n");
  371. fprintf(gnuplot_file, "set key top left\n");
  372. fprintf(gnuplot_file, "set logscale x\n");
  373. fprintf(gnuplot_file, "set logscale y\n");
  374. fprintf(gnuplot_file, "\n");
  375. /* If no input data is given to gnuplot, we at least need to specify an
  376. * arbitrary range. */
  377. if (options->with_fxt_file == 0)
  378. fprintf(gnuplot_file, "set xrange [1:10**9]\n\n");
  379. int first = 1;
  380. fprintf(gnuplot_file, "plot\t");
  381. /* display all or selected combinations */
  382. display_all_perf_models(gnuplot_file, model, &first, options);
  383. display_history_based_perf_models(gnuplot_file, model, &first, options);
  384. }
  385. int main(int argc, char **argv)
  386. {
  387. int ret = 0;
  388. struct starpu_perfmodel model = { .type = STARPU_PERFMODEL_INVALID };
  389. char gnuplot_file_name[256];
  390. struct _perfmodel_plot_options options;
  391. #if defined(_WIN32) && !defined(__CYGWIN__)
  392. WSADATA wsadata;
  393. WSAStartup(MAKEWORD(1,0), &wsadata);
  394. #endif
  395. parse_args(argc, argv, &options);
  396. if (options.directory)
  397. {
  398. starpu_perfmodel_directory(stdout);
  399. }
  400. else if (options.list)
  401. {
  402. ret = starpu_perfmodel_list(stdout);
  403. if (ret)
  404. {
  405. _STARPU_DISP("The performance model directory is invalid\n");
  406. }
  407. }
  408. else
  409. {
  410. /* Load the performance model associated to the symbol */
  411. ret = starpu_perfmodel_load_symbol(options.symbol, &model);
  412. if (ret)
  413. {
  414. _STARPU_DISP("The performance model for the symbol <%s> could not be loaded\n", options.symbol);
  415. }
  416. else if (options.list_combs)
  417. {
  418. ret = starpu_perfmodel_list_combs(stdout, &model);
  419. if (ret)
  420. {
  421. fprintf(stderr, "Error when listing combinations for model <%s>\n", options.symbol);
  422. }
  423. }
  424. else
  425. {
  426. /* If some FxT input was specified, we put the points on the graph */
  427. #ifdef STARPU_USE_FXT
  428. if (options.with_fxt_file)
  429. {
  430. starpu_fxt_generate_trace(&options.fxt_options);
  431. snprintf(options.data_file_name, 256, "starpu_%s.data", options.symbol);
  432. FILE *data_file = fopen(options.data_file_name, "w+");
  433. STARPU_ASSERT(data_file);
  434. dump_data_file(data_file, &options);
  435. fclose(data_file);
  436. }
  437. #endif
  438. snprintf(gnuplot_file_name, 256, "starpu_%s.gp", options.symbol);
  439. snprintf(options.avg_file_name, 256, "starpu_%s_avg.data", options.symbol);
  440. FILE *gnuplot_file = fopen(gnuplot_file_name, "w+");
  441. STARPU_ASSERT(gnuplot_file);
  442. display_selected_models(gnuplot_file, &model, &options);
  443. fprintf(gnuplot_file,"\n");
  444. fclose(gnuplot_file);
  445. /* Retrieve the current mode of the gnuplot executable */
  446. struct stat sb;
  447. ret = stat(gnuplot_file_name, &sb);
  448. if (ret)
  449. {
  450. perror("stat");
  451. STARPU_ABORT();
  452. }
  453. /* Make the gnuplot scrit executable for the owner */
  454. ret = chmod(gnuplot_file_name, sb.st_mode|S_IXUSR);
  455. if (ret)
  456. {
  457. perror("chmod");
  458. STARPU_ABORT();
  459. }
  460. _STARPU_DISP("Gnuplot file <%s> generated\n", gnuplot_file_name);
  461. }
  462. }
  463. starpu_perfmodel_free_sampling_directories();
  464. return ret;
  465. }