starpu_regression_display.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <starpu.h>
  22. #include <starpu_perfmodel.h>
  23. #include <core/perfmodel/perfmodel.h> // we need to browse the list associated to history-based models
  24. #ifdef __MINGW32__
  25. #include <windows.h>
  26. #endif
  27. static struct starpu_perfmodel_t model;
  28. /* what kernel ? */
  29. static char *symbol = NULL;
  30. /* which architecture ? (NULL = all)*/
  31. static char *arch = NULL;
  32. /* Unless a FxT file is specified, we just display the model */
  33. static int no_fxt_file = 1;
  34. #ifdef STARPU_USE_FXT
  35. static struct starpu_fxt_codelet_event *dumped_codelets;
  36. static long dumped_codelets_count;
  37. static struct starpu_fxt_options options;
  38. #endif
  39. static int archtype_is_found[STARPU_NARCH_VARIATIONS];
  40. static char data_file_name[256];
  41. static char gnuplot_file_name[256];
  42. static void usage(char **argv)
  43. {
  44. fprintf(stderr, "Usage: %s [ options ]\n", argv[0]);
  45. fprintf(stderr, "\n");
  46. fprintf(stderr, "One must specify a symbol with the -s option\n");
  47. fprintf(stderr, "Options:\n");
  48. fprintf(stderr, " -s <symbol> specify the symbol\n");
  49. fprintf(stderr, " -i <Fxt files> input FxT files generated by StarPU\n");
  50. fprintf(stderr, " -a <arch> specify the architecture (e.g. cpu, cpu:k, cuda, gordon)\n");
  51. fprintf(stderr, "\n");
  52. }
  53. static void parse_args(int argc, char **argv)
  54. {
  55. #ifdef STARPU_USE_FXT
  56. /* Default options */
  57. starpu_fxt_options_init(&options);
  58. options.out_paje_path = NULL;
  59. options.activity_path = NULL;
  60. options.distrib_time_path = NULL;
  61. options.dag_path = NULL;
  62. options.dumped_codelets = &dumped_codelets;
  63. #endif
  64. /* We want to support arguments such as "-i trace_*" */
  65. unsigned reading_input_filenames = 0;
  66. int i;
  67. for (i = 1; i < argc; i++) {
  68. if (strcmp(argv[i], "-s") == 0) {
  69. symbol = argv[++i];
  70. continue;
  71. }
  72. if (strcmp(argv[i], "-i") == 0) {
  73. reading_input_filenames = 1;
  74. #ifdef STARPU_USE_FXT
  75. options.filenames[options.ninputfiles++] = argv[++i];
  76. no_fxt_file = 0;
  77. #else
  78. fprintf(stderr, "Warning: FxT support was not enabled in StarPU: FxT traces will thus be ignored!\n");
  79. #endif
  80. continue;
  81. }
  82. if (strcmp(argv[i], "-a") == 0) {
  83. arch = argv[++i];
  84. continue;
  85. }
  86. if (strcmp(argv[i], "-h") == 0) {
  87. usage(argv);
  88. exit(-1);
  89. }
  90. /* If the reading_input_filenames flag is set, and that the
  91. * argument does not match an option, we assume this may be
  92. * another filename */
  93. if (reading_input_filenames)
  94. {
  95. #ifdef STARPU_USE_FXT
  96. options.filenames[options.ninputfiles++] = argv[i];
  97. #endif
  98. continue;
  99. }
  100. }
  101. }
  102. static void display_perf_model(FILE *gnuplot_file, struct starpu_perfmodel_t *model, enum starpu_perf_archtype arch, int *first)
  103. {
  104. char arch_name[256];
  105. starpu_perfmodel_get_arch_name(arch, arch_name, 256);
  106. #ifdef STARPU_USE_FXT
  107. if (!no_fxt_file && archtype_is_found[arch])
  108. {
  109. if (*first)
  110. {
  111. *first = 0;
  112. }
  113. else {
  114. fprintf(gnuplot_file, ",\\\n\t");
  115. }
  116. fprintf(gnuplot_file, "\"< grep -w \\^%d %s\" using 2:3 title \"%s\"", arch, data_file_name, arch_name);
  117. }
  118. #endif
  119. struct starpu_per_arch_perfmodel_t *arch_model = &model->per_arch[arch];
  120. /* Only display the regression model if we could actually build a model */
  121. if (arch_model->regression.valid)
  122. {
  123. if (*first)
  124. {
  125. *first = 0;
  126. }
  127. else {
  128. fprintf(gnuplot_file, ",\\\n\t");
  129. }
  130. fprintf(stderr, "\tLinear: y = alpha size ^ beta\n");
  131. fprintf(stderr, "\t\talpha = %le\n", arch_model->regression.alpha * 0.001);
  132. fprintf(stderr, "\t\tbeta = %le\n", arch_model->regression.beta);
  133. fprintf(gnuplot_file, "0.001 * %f * x ** %f title \"Linear Regression %s\"",
  134. arch_model->regression.alpha, arch_model->regression.beta, arch_name);
  135. }
  136. if (arch_model->regression.nl_valid)
  137. {
  138. if (*first)
  139. {
  140. *first = 0;
  141. }
  142. else {
  143. fprintf(gnuplot_file, ",\\\n\t");
  144. }
  145. fprintf(stderr, "\tNon-Linear: y = a size ^b + c\n");
  146. fprintf(stderr, "\t\ta = %le\n", arch_model->regression.a * 0.001);
  147. fprintf(stderr, "\t\tb = %le\n", arch_model->regression.b);
  148. fprintf(stderr, "\t\tc = %le\n", arch_model->regression.c * 0.001);
  149. fprintf(gnuplot_file, "0.001 * %f * x ** %f + 0.001 * %f title \"Non-Linear Regression %s\"",
  150. arch_model->regression.a, arch_model->regression.b, arch_model->regression.c, arch_name);
  151. }
  152. }
  153. #ifdef STARPU_USE_FXT
  154. static void dump_data_file(FILE *data_file)
  155. {
  156. memset(archtype_is_found, 0, STARPU_NARCH_VARIATIONS*sizeof(int));
  157. int i;
  158. for (i = 0; i < options.dumped_codelets_count; i++)
  159. {
  160. /* Dump only if the symbol matches user's request */
  161. if (strcmp(dumped_codelets[i].symbol, symbol) == 0) {
  162. enum starpu_perf_archtype archtype = dumped_codelets[i].archtype;
  163. archtype_is_found[archtype] = 1;
  164. size_t size = dumped_codelets[i].size;
  165. float time = dumped_codelets[i].time;
  166. fprintf(data_file, "%d %f %f\n", archtype, (float)size, time);
  167. }
  168. }
  169. }
  170. #endif
  171. static void display_selected_models(FILE *gnuplot_file, struct starpu_perfmodel_t *model)
  172. {
  173. fprintf(gnuplot_file, "#!/usr/bin/gnuplot -persist\n");
  174. fprintf(gnuplot_file, "\n");
  175. fprintf(gnuplot_file, "set term postscript eps enhanced color\n");
  176. fprintf(gnuplot_file, "set output \"regression_%s.eps\"\n", symbol);
  177. fprintf(gnuplot_file, "set title \"Model for codelet %s\"\n", symbol);
  178. fprintf(gnuplot_file, "set xlabel \"Size\"\n");
  179. fprintf(gnuplot_file, "set ylabel \"Time\"\n");
  180. fprintf(gnuplot_file, "\n");
  181. fprintf(gnuplot_file, "set logscale x\n");
  182. fprintf(gnuplot_file, "set logscale y\n");
  183. fprintf(gnuplot_file, "\n");
  184. /* If no input data is given to gnuplot, we at least need to specify an
  185. * arbitrary range. */
  186. if (no_fxt_file)
  187. fprintf(gnuplot_file, "set xrange [10**3:10**9]\n\n");
  188. int first = 1;
  189. fprintf(gnuplot_file, "plot\t");
  190. if (arch == NULL)
  191. {
  192. /* display all architectures */
  193. unsigned archid;
  194. for (archid = 0; archid < STARPU_NARCH_VARIATIONS; archid++)
  195. display_perf_model(gnuplot_file, model, archid, &first);
  196. }
  197. else {
  198. if (strcmp(arch, "cpu") == 0) {
  199. display_perf_model(gnuplot_file, model, STARPU_CPU_DEFAULT, &first);
  200. return;
  201. }
  202. int k;
  203. if (sscanf(arch, "cpu:%d", &k) == 1)
  204. {
  205. /* For combined CPU workers */
  206. if ((k < 1) || (k > STARPU_MAXCPUS))
  207. {
  208. fprintf(stderr, "Invalid CPU size\n");
  209. exit(-1);
  210. }
  211. display_perf_model(gnuplot_file, model, STARPU_CPU_DEFAULT + k - 1, &first);
  212. return;
  213. }
  214. if (strcmp(arch, "cuda") == 0) {
  215. unsigned archid;
  216. for (archid = STARPU_CUDA_DEFAULT; archid < STARPU_CUDA_DEFAULT + STARPU_MAXCUDADEVS; archid++)
  217. {
  218. char archname[32];
  219. starpu_perfmodel_get_arch_name(archid, archname, 32);
  220. display_perf_model(gnuplot_file, model, archid, &first);
  221. }
  222. return;
  223. }
  224. /* There must be a cleaner way ! */
  225. int gpuid;
  226. int nmatched;
  227. nmatched = sscanf(arch, "cuda_%d", &gpuid);
  228. if (nmatched == 1)
  229. {
  230. unsigned archid = STARPU_CUDA_DEFAULT+ gpuid;
  231. display_perf_model(gnuplot_file, model, archid, &first);
  232. return;
  233. }
  234. if (strcmp(arch, "gordon") == 0) {
  235. display_perf_model(gnuplot_file, model, STARPU_GORDON_DEFAULT, &first);
  236. return;
  237. }
  238. fprintf(stderr, "Unknown architecture requested, aborting.\n");
  239. exit(-1);
  240. }
  241. }
  242. int main(int argc, char **argv)
  243. {
  244. int ret;
  245. #ifdef __MINGW32__
  246. WSADATA wsadata;
  247. WSAStartup(MAKEWORD(1,0), &wsadata);
  248. #endif
  249. parse_args(argc, argv);
  250. /* We need at least a symbol name */
  251. if (!symbol)
  252. {
  253. fprintf(stderr, "No symbol was specified\n");
  254. return 1;
  255. }
  256. /* Load the performance model associated to the symbol */
  257. ret = starpu_load_history_debug(symbol, &model);
  258. if (ret == 1)
  259. {
  260. fprintf(stderr, "The performance model could not be loaded\n");
  261. return 1;
  262. }
  263. /* If some FxT input was specified, we put the points on the graph */
  264. #ifdef STARPU_USE_FXT
  265. if (!no_fxt_file)
  266. {
  267. starpu_fxt_generate_trace(&options);
  268. snprintf(data_file_name, 256, "starpu_%s.data", symbol);
  269. FILE *data_file = fopen(data_file_name, "w+");
  270. STARPU_ASSERT(data_file);
  271. dump_data_file(data_file);
  272. fclose(data_file);
  273. }
  274. #endif
  275. snprintf(gnuplot_file_name, 256, "starpu_%s.gp", symbol);
  276. FILE *gnuplot_file = fopen(gnuplot_file_name, "w+");
  277. STARPU_ASSERT(gnuplot_file);
  278. display_selected_models(gnuplot_file, &model);
  279. fclose(gnuplot_file);
  280. /* Retrieve the current mode of the gnuplot executable */
  281. struct stat sb;
  282. ret = stat(gnuplot_file_name, &sb);
  283. if (ret)
  284. {
  285. perror("stat");
  286. STARPU_ABORT();
  287. }
  288. /* Make the gnuplot scrit executable for the owner */
  289. ret = chmod(gnuplot_file_name, sb.st_mode|S_IXUSR);
  290. if (ret)
  291. {
  292. perror("chmod");
  293. STARPU_ABORT();
  294. }
  295. return 0;
  296. }