starpu_perfmodel_plot.c 13 KB

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