starpu_perfmodel_plot.c 13 KB

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