starpu_regression_display.c 9.0 KB

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