starpu_perfmodel_plot.c 13 KB

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