starpu_regression_display.c 12 KB

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