starpu_perfmodel_plot.c 12 KB

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