starpu_perfmodel_plot.c 13 KB

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