starpu_perfmodel_plot.c 15 KB

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