starpu_perfmodel_plot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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_k, 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. fprintf(stderr,"Arch: %s\n", arch_name);
  128. #ifdef STARPU_USE_FXT
  129. if (!no_fxt_file && archtype_is_found[arch])
  130. {
  131. print_comma(gnuplot_file, first);
  132. fprintf(gnuplot_file, "\"< grep -w \\^%d %s\" using 2:3 title \"%s\"", arch, data_file_name, arch_name);
  133. }
  134. #endif
  135. struct starpu_per_arch_perfmodel_t *arch_model = &model->per_arch[arch];
  136. /* Only display the regression model if we could actually build a model */
  137. if (arch_model->regression.valid)
  138. {
  139. print_comma(gnuplot_file, first);
  140. fprintf(stderr, "\tLinear: y = alpha size ^ beta\n");
  141. fprintf(stderr, "\t\talpha = %le\n", arch_model->regression.alpha * 0.001);
  142. fprintf(stderr, "\t\tbeta = %le\n", arch_model->regression.beta);
  143. fprintf(gnuplot_file, "0.001 * %f * x ** %f title \"Linear Regression %s\"",
  144. arch_model->regression.alpha, arch_model->regression.beta, arch_name);
  145. }
  146. if (arch_model->regression.nl_valid)
  147. {
  148. print_comma(gnuplot_file, first);
  149. fprintf(stderr, "\tNon-Linear: y = a size ^b + c\n");
  150. fprintf(stderr, "\t\ta = %le\n", arch_model->regression.a * 0.001);
  151. fprintf(stderr, "\t\tb = %le\n", arch_model->regression.b);
  152. fprintf(stderr, "\t\tc = %le\n", arch_model->regression.c * 0.001);
  153. fprintf(gnuplot_file, "0.001 * %f * x ** %f + 0.001 * %f title \"Non-Linear Regression %s\"",
  154. arch_model->regression.a, arch_model->regression.b, arch_model->regression.c, arch_name);
  155. }
  156. }
  157. 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)
  158. {
  159. char *command;
  160. FILE *datafile;
  161. unsigned n = arch2 - arch1;
  162. enum starpu_perf_archtype arch;
  163. struct starpu_history_list_t *ptr[n], *ptrs[n];
  164. char archname[32];
  165. int col;
  166. int len;
  167. len = 10 + strlen(avg_file_name) + 1;
  168. command = malloc(len);
  169. snprintf(command, len, "sort -n > %s", avg_file_name);
  170. datafile = popen(command, "w");
  171. free(command);
  172. col = 2;
  173. for (arch = arch1; arch < arch2; arch++) {
  174. struct starpu_per_arch_perfmodel_t *arch_model = &model->per_arch[arch];
  175. starpu_perfmodel_get_arch_name(arch, archname, 32);
  176. ptrs[arch-arch1] = ptr[arch-arch1] = arch_model->list;
  177. if (ptr[arch-arch1]) {
  178. print_comma(gnuplot_file, first);
  179. fprintf(gnuplot_file, "\"%s\" using 1:%d:%d with errorlines title \"Measured %s\"", avg_file_name, col, col+1, archname);
  180. col += 2;
  181. }
  182. }
  183. while (1) {
  184. unsigned long minimum;
  185. /* Check whether there's data left */
  186. for (arch = arch1; arch < arch2; arch++) {
  187. if (ptr[arch-arch1])
  188. break;
  189. }
  190. if (arch == arch2)
  191. /* finished with archs */
  192. break;
  193. /* Get the minimum x */
  194. minimum = ULONG_MAX;
  195. for (arch = arch1; arch < arch2; arch++) {
  196. if (ptr[arch-arch1]) {
  197. struct starpu_history_entry_t *entry = ptr[arch-arch1]->entry;
  198. if (entry->size < minimum)
  199. minimum = entry->size;
  200. }
  201. }
  202. fprintf(stderr, "%lu ", minimum);
  203. fprintf(datafile, "%-15lu ", minimum);
  204. for (arch = arch1; arch < arch2; arch++) {
  205. if (ptr[arch-arch1]) {
  206. struct starpu_history_entry_t *entry = ptr[arch-arch1]->entry;
  207. if (entry->size == minimum) {
  208. fprintf(datafile, "\t%-15le\t%-15le", 0.001*entry->mean, 0.001*entry->deviation);
  209. ptr[arch-arch1] = ptr[arch-arch1]->next;
  210. } else
  211. fprintf(datafile, "\t\"\"\t\"\"");
  212. } else if (ptrs[arch-arch1]) {
  213. /* Finished for this arch only */
  214. fprintf(datafile, "\t\"\"\t\"\"");
  215. }
  216. }
  217. fprintf(datafile, "\n");
  218. }
  219. fprintf(stderr, "\n");
  220. }
  221. 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)
  222. {
  223. enum starpu_perf_archtype arch;
  224. for (arch = arch1; arch < arch2; arch++)
  225. display_perf_model(gnuplot_file, model, arch, first);
  226. display_history_based_perf_models(gnuplot_file, model, arch1, arch2, first);
  227. }
  228. #ifdef STARPU_USE_FXT
  229. static void dump_data_file(FILE *data_file)
  230. {
  231. memset(archtype_is_found, 0, STARPU_NARCH_VARIATIONS*sizeof(int));
  232. int i;
  233. for (i = 0; i < options.dumped_codelets_count; i++)
  234. {
  235. /* Dump only if the symbol matches user's request */
  236. if (strcmp(dumped_codelets[i].symbol, symbol) == 0) {
  237. enum starpu_perf_archtype archtype = dumped_codelets[i].archtype;
  238. archtype_is_found[archtype] = 1;
  239. size_t size = dumped_codelets[i].size;
  240. float time = dumped_codelets[i].time;
  241. fprintf(data_file, "%d %f %f\n", archtype, (float)size, time);
  242. }
  243. }
  244. }
  245. #endif
  246. static void display_selected_models(FILE *gnuplot_file, struct starpu_perfmodel_t *model)
  247. {
  248. fprintf(gnuplot_file, "#!/usr/bin/gnuplot -persist\n");
  249. fprintf(gnuplot_file, "\n");
  250. fprintf(gnuplot_file, "set term postscript eps enhanced color\n");
  251. fprintf(gnuplot_file, "set output \"starpu_%s.eps\"\n", symbol);
  252. fprintf(gnuplot_file, "set title \"Model for codelet %s\"\n", symbol);
  253. fprintf(gnuplot_file, "set xlabel \"Size\"\n");
  254. fprintf(gnuplot_file, "set ylabel \"Time\"\n");
  255. fprintf(gnuplot_file, "\n");
  256. fprintf(gnuplot_file, "set logscale x\n");
  257. fprintf(gnuplot_file, "set logscale y\n");
  258. fprintf(gnuplot_file, "\n");
  259. /* If no input data is given to gnuplot, we at least need to specify an
  260. * arbitrary range. */
  261. if (no_fxt_file)
  262. fprintf(gnuplot_file, "set xrange [10**3:10**9]\n\n");
  263. int first = 1;
  264. fprintf(gnuplot_file, "plot\t");
  265. if (arch == NULL)
  266. {
  267. /* display all architectures */
  268. display_perf_models(gnuplot_file, model, 0, STARPU_NARCH_VARIATIONS, &first);
  269. }
  270. else {
  271. if (strcmp(arch, "cpu") == 0) {
  272. display_perf_model(gnuplot_file, model, STARPU_CPU_DEFAULT, &first);
  273. return;
  274. }
  275. int k;
  276. if (sscanf(arch, "cpu:%d", &k) == 1)
  277. {
  278. /* For combined CPU workers */
  279. if ((k < 1) || (k > STARPU_MAXCPUS))
  280. {
  281. fprintf(stderr, "Invalid CPU size\n");
  282. exit(-1);
  283. }
  284. display_perf_models(gnuplot_file, model, STARPU_CPU_DEFAULT + k - 1, STARPU_CPU_DEFAULT + k, &first);
  285. return;
  286. }
  287. if (strcmp(arch, "cuda") == 0) {
  288. display_perf_models(gnuplot_file, model, STARPU_CUDA_DEFAULT, STARPU_CUDA_DEFAULT + STARPU_MAXCUDADEVS, &first);
  289. return;
  290. }
  291. /* There must be a cleaner way ! */
  292. int gpuid;
  293. int nmatched;
  294. nmatched = sscanf(arch, "cuda_%d", &gpuid);
  295. if (nmatched == 1)
  296. {
  297. unsigned archid = STARPU_CUDA_DEFAULT+ gpuid;
  298. display_perf_models(gnuplot_file, model, archid, archid + 1, &first);
  299. return;
  300. }
  301. if (strcmp(arch, "gordon") == 0) {
  302. display_perf_models(gnuplot_file, model, STARPU_GORDON_DEFAULT, STARPU_GORDON_DEFAULT + 1, &first);
  303. return;
  304. }
  305. fprintf(stderr, "Unknown architecture requested, aborting.\n");
  306. exit(-1);
  307. }
  308. }
  309. int main(int argc, char **argv)
  310. {
  311. int ret;
  312. #ifdef __MINGW32__
  313. WSADATA wsadata;
  314. WSAStartup(MAKEWORD(1,0), &wsadata);
  315. #endif
  316. parse_args(argc, argv);
  317. if (list) {
  318. int ret = starpu_list_models();
  319. if (ret) {
  320. fprintf(stderr, "The performance model directory is invalid\n");
  321. return 1;
  322. }
  323. return 0;
  324. }
  325. /* We need at least a symbol name */
  326. if (!symbol)
  327. {
  328. fprintf(stderr, "No symbol was specified\n");
  329. return 1;
  330. }
  331. /* Load the performance model associated to the symbol */
  332. ret = starpu_load_history_debug(symbol, &model);
  333. if (ret == 1)
  334. {
  335. fprintf(stderr, "The performance model could not be loaded\n");
  336. return 1;
  337. }
  338. /* If some FxT input was specified, we put the points on the graph */
  339. #ifdef STARPU_USE_FXT
  340. if (!no_fxt_file)
  341. {
  342. starpu_fxt_generate_trace(&options);
  343. snprintf(data_file_name, 256, "starpu_%s.data", symbol);
  344. FILE *data_file = fopen(data_file_name, "w+");
  345. STARPU_ASSERT(data_file);
  346. dump_data_file(data_file);
  347. fclose(data_file);
  348. }
  349. #endif
  350. snprintf(gnuplot_file_name, 256, "starpu_%s.gp", symbol);
  351. snprintf(avg_file_name, 256, "starpu_%s_avg.data", symbol);
  352. FILE *gnuplot_file = fopen(gnuplot_file_name, "w+");
  353. STARPU_ASSERT(gnuplot_file);
  354. display_selected_models(gnuplot_file, &model);
  355. fclose(gnuplot_file);
  356. /* Retrieve the current mode of the gnuplot executable */
  357. struct stat sb;
  358. ret = stat(gnuplot_file_name, &sb);
  359. if (ret)
  360. {
  361. perror("stat");
  362. STARPU_ABORT();
  363. }
  364. /* Make the gnuplot scrit executable for the owner */
  365. ret = chmod(gnuplot_file_name, sb.st_mode|S_IXUSR);
  366. if (ret)
  367. {
  368. perror("chmod");
  369. STARPU_ABORT();
  370. }
  371. return 0;
  372. }