starpu_perfmodel_plot.c 12 KB

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