starpu_perfmodel_plot.c 14 KB

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