starpu_perfmodel_plot.c 15 KB

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