starpu_perfmodel_display.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Université de Bordeaux 1
  4. * Copyright (C) 2011, 2012 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 Télécom-SudParis
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <config.h>
  19. #include <assert.h>
  20. #include <getopt.h>
  21. #include <unistd.h>
  22. #include <stdio.h>
  23. #include <starpu.h>
  24. #include <starpu_perfmodel.h>
  25. #ifdef __MINGW32__
  26. #include <windows.h>
  27. #endif
  28. #define PROGNAME "starpu_perfmodel_display"
  29. static struct starpu_perfmodel model;
  30. /* display all available models */
  31. static int list = 0;
  32. /* what kernel ? */
  33. static char *symbol = NULL;
  34. /* what parameter should be displayed ? (NULL = all) */
  35. static char *parameter = NULL;
  36. /* which architecture ? (NULL = all)*/
  37. static char *arch = NULL;
  38. /* should we display a specific footprint ? */
  39. unsigned display_specific_footprint;
  40. uint32_t specific_footprint;
  41. static void usage(char **argv)
  42. {
  43. fprintf(stderr, "Display a given perfmodel\n\n");
  44. fprintf(stderr, "Usage: %s [ options ]\n", PROGNAME);
  45. fprintf(stderr, "\n");
  46. fprintf(stderr, "One must specify either -l or -s\n");
  47. fprintf(stderr, "Options:\n");
  48. fprintf(stderr, " -l display all available models\n");
  49. fprintf(stderr, " -s <symbol> specify the symbol\n");
  50. fprintf(stderr, " -p <parameter> specify the parameter (e.g. a, b, c, mean, stddev)\n");
  51. fprintf(stderr, " -a <arch> specify the architecture (e.g. cpu, cpu:k, cuda, gordon)\n");
  52. fprintf(stderr, " -f <footprint> display the history-based model for the specified footprint\n");
  53. fprintf(stderr, " -h, --help display this help and exit\n");
  54. fprintf(stderr, " -v, --version output version information and exit\n\n");
  55. fprintf(stderr, "Reports bugs to <"PACKAGE_BUGREPORT">.");
  56. fprintf(stderr, "\n");
  57. }
  58. static void parse_args(int argc, char **argv)
  59. {
  60. int c;
  61. static struct option long_options[] =
  62. {
  63. {"arch", required_argument, NULL, 'a'},
  64. {"footprint", required_argument, NULL, 'f'},
  65. {"help", no_argument, NULL, 'h'},
  66. /* XXX Would be cleaner to set a flag */
  67. {"list", no_argument, NULL, 'l'},
  68. {"parameter", required_argument, NULL, 'p'},
  69. {"symbol", required_argument, NULL, 's'},
  70. {"version", no_argument, NULL, 'v'},
  71. {0, 0, 0, 0}
  72. };
  73. int option_index;
  74. while ((c = getopt_long(argc, argv, "ls:p:a:f:h", long_options, &option_index)) != -1) {
  75. switch (c) {
  76. case 'l':
  77. /* list all models */
  78. list = 1;
  79. break;
  80. case 's':
  81. /* symbol */
  82. symbol = optarg;
  83. break;
  84. case 'p':
  85. /* parameter (eg. a, b, c, mean, stddev) */
  86. parameter = optarg;
  87. break;
  88. case 'a':
  89. /* architecture (cpu, cuda, gordon) */
  90. arch = optarg;
  91. break;
  92. case 'f':
  93. /* footprint */
  94. display_specific_footprint = 1;
  95. sscanf(optarg, "%08x", &specific_footprint);
  96. break;
  97. case 'h':
  98. usage(argv);
  99. exit(EXIT_SUCCESS);
  100. case 'v':
  101. (void) fprintf(stdout, "%s %d.%d\n",
  102. PROGNAME, STARPU_MAJOR_VERSION,
  103. STARPU_MINOR_VERSION);
  104. exit(EXIT_SUCCESS);
  105. case '?':
  106. default:
  107. fprintf(stderr, "Unrecognized option: -%c\n", optopt);
  108. }
  109. }
  110. if (!symbol && !list)
  111. {
  112. fprintf(stderr, "Incorrect usage, aborting\n");
  113. usage(argv);
  114. exit(-1);
  115. }
  116. }
  117. static void display_history_based_perf_model(struct starpu_per_arch_perfmodel *per_arch_model)
  118. {
  119. struct starpu_history_list *ptr;
  120. ptr = per_arch_model->list;
  121. if (!parameter && ptr)
  122. fprintf(stderr, "# hash\t\tsize\t\tmean\t\tdev\t\tn\n");
  123. while (ptr) {
  124. struct starpu_history_entry *entry = ptr->entry;
  125. if (!display_specific_footprint || (entry->footprint == specific_footprint))
  126. {
  127. if (!parameter)
  128. {
  129. /* There isn't a parameter that is explicitely requested, so we display all parameters */
  130. printf("%08x\t%-15lu\t%-15le\t%-15le\t%u\n", entry->footprint,
  131. (unsigned long) entry->size, entry->mean, entry->deviation, entry->nsample);
  132. }
  133. else {
  134. /* only display the parameter that was specifically requested */
  135. if (strcmp(parameter, "mean") == 0) {
  136. printf("%-15le\n", entry->mean);
  137. }
  138. if (strcmp(parameter, "stddev") == 0) {
  139. printf("%-15le\n", entry->deviation);
  140. return;
  141. }
  142. }
  143. }
  144. ptr = ptr->next;
  145. }
  146. }
  147. static void display_perf_model(struct starpu_perfmodel *model, enum starpu_perf_archtype arch, unsigned nimpl)
  148. {
  149. struct starpu_per_arch_perfmodel *arch_model = &model->per_arch[arch][nimpl];
  150. char archname[32];
  151. if (arch_model->regression.nsample || arch_model->regression.valid || arch_model->regression.nl_valid || arch_model->list) {
  152. starpu_perfmodel_get_arch_name(arch, archname, 32, nimpl);
  153. fprintf(stderr, "performance model for %s\n", archname);
  154. }
  155. if (parameter == NULL)
  156. {
  157. /* no specific parameter was requested, so we display everything */
  158. if (arch_model->regression.nsample)
  159. fprintf(stderr, "\tRegression : #sample = %d\n",
  160. arch_model->regression.nsample);
  161. /* Only display the regression model if we could actually build a model */
  162. if (arch_model->regression.valid)
  163. {
  164. fprintf(stderr, "\tLinear: y = alpha size ^ beta\n");
  165. fprintf(stderr, "\t\talpha = %e\n", arch_model->regression.alpha);
  166. fprintf(stderr, "\t\tbeta = %e\n", arch_model->regression.beta);
  167. }
  168. else {
  169. //fprintf(stderr, "\tLinear model is INVALID\n");
  170. }
  171. if (arch_model->regression.nl_valid)
  172. {
  173. fprintf(stderr, "\tNon-Linear: y = a size ^b + c\n");
  174. fprintf(stderr, "\t\ta = %e\n", arch_model->regression.a);
  175. fprintf(stderr, "\t\tb = %e\n", arch_model->regression.b);
  176. fprintf(stderr, "\t\tc = %e\n", arch_model->regression.c);
  177. }
  178. else {
  179. //fprintf(stderr, "\tNon-Linear model is INVALID\n");
  180. }
  181. display_history_based_perf_model(arch_model);
  182. #if 0
  183. char debugname[1024];
  184. starpu_perfmodel_debugfilepath(model, arch, debugname, 1024, nimpl);
  185. printf("\t debug file path : %s\n", debugname);
  186. #endif
  187. }
  188. else {
  189. /* only display the parameter that was specifically requested */
  190. if (strcmp(parameter, "a") == 0) {
  191. printf("%e\n", arch_model->regression.a);
  192. return;
  193. }
  194. if (strcmp(parameter, "b") == 0) {
  195. printf("%e\n", arch_model->regression.b);
  196. return;
  197. }
  198. if (strcmp(parameter, "c") == 0) {
  199. printf("%e\n", arch_model->regression.c);
  200. return;
  201. }
  202. if (strcmp(parameter, "alpha") == 0) {
  203. printf("%e\n", arch_model->regression.alpha);
  204. return;
  205. }
  206. if (strcmp(parameter, "beta") == 0) {
  207. printf("%e\n", arch_model->regression.beta);
  208. return;
  209. }
  210. if (strcmp(parameter, "path-file-debug") == 0) {
  211. char debugname[256];
  212. starpu_perfmodel_debugfilepath(model, arch, debugname, 1024, nimpl);
  213. printf("%s\n", debugname);
  214. return;
  215. }
  216. if ((strcmp(parameter, "mean") == 0) || (strcmp(parameter, "stddev"))) {
  217. display_history_based_perf_model(arch_model);
  218. return;
  219. }
  220. /* TODO display if it's valid ? */
  221. fprintf(stderr, "Unknown parameter requested, aborting.\n");
  222. exit(-1);
  223. }
  224. }
  225. static void display_all_perf_models(struct starpu_perfmodel *model)
  226. {
  227. if (arch == NULL)
  228. {
  229. /* display all architectures */
  230. unsigned archid;
  231. unsigned implid;
  232. for (archid = 0; archid < STARPU_NARCH_VARIATIONS; archid++) {
  233. for (implid = 0; implid < STARPU_MAXIMPLEMENTATIONS; implid++) { /* Display all codelets on each arch */
  234. display_perf_model(model, (enum starpu_perf_archtype) archid, implid);
  235. }
  236. }
  237. }
  238. else {
  239. if (strcmp(arch, "cpu") == 0) {
  240. unsigned implid;
  241. for (implid = 0; implid < STARPU_MAXIMPLEMENTATIONS; implid++)
  242. display_perf_model(model, STARPU_CPU_DEFAULT,implid); /* Display all codelets on cpu */
  243. return;
  244. }
  245. int k;
  246. if (sscanf(arch, "cpu:%d", &k) == 1)
  247. {
  248. /* For combined CPU workers */
  249. if ((k < 1) || (k > STARPU_MAXCPUS))
  250. {
  251. fprintf(stderr, "Invalid CPU size\n");
  252. exit(-1);
  253. }
  254. unsigned implid;
  255. for (implid = 0; implid < STARPU_MAXIMPLEMENTATIONS; implid++)
  256. display_perf_model(model, (enum starpu_perf_archtype) (STARPU_CPU_DEFAULT + k - 1), implid);
  257. return;
  258. }
  259. if (strcmp(arch, "cuda") == 0) {
  260. unsigned archid;
  261. unsigned implid;
  262. for (archid = STARPU_CUDA_DEFAULT; archid < STARPU_CUDA_DEFAULT + STARPU_MAXCUDADEVS; archid++) {
  263. for (implid = 0; implid <STARPU_MAXIMPLEMENTATIONS; implid ++) {
  264. char archname[32];
  265. starpu_perfmodel_get_arch_name((enum starpu_perf_archtype) archid, archname, 32, implid);
  266. fprintf(stderr, "performance model for %s\n", archname);
  267. display_perf_model(model, (enum starpu_perf_archtype) archid, implid);
  268. }
  269. }
  270. return;
  271. }
  272. /* There must be a cleaner way ! */
  273. int gpuid;
  274. int nmatched;
  275. nmatched = sscanf(arch, "cuda_%d", &gpuid);
  276. if (nmatched == 1)
  277. {
  278. unsigned archid = STARPU_CUDA_DEFAULT+ gpuid;
  279. unsigned implid;
  280. for (implid = 0; implid < STARPU_MAXIMPLEMENTATIONS; implid++)
  281. display_perf_model(model, (enum starpu_perf_archtype) archid, implid);
  282. return;
  283. }
  284. if (strcmp(arch, "gordon") == 0) {
  285. fprintf(stderr, "performance model for gordon\n");
  286. unsigned implid;
  287. for (implid = 0; implid < STARPU_MAXIMPLEMENTATIONS; implid++)
  288. display_perf_model(model, STARPU_GORDON_DEFAULT, implid);
  289. return;
  290. }
  291. fprintf(stderr, "Unknown architecture requested, aborting.\n");
  292. exit(-1);
  293. }
  294. }
  295. int main(int argc, char **argv)
  296. {
  297. // assert(argc == 2);
  298. #ifdef __MINGW32__
  299. WSADATA wsadata;
  300. WSAStartup(MAKEWORD(1,0), &wsadata);
  301. #endif
  302. parse_args(argc, argv);
  303. if (list) {
  304. int ret = starpu_list_models(stdout);
  305. if (ret) {
  306. fprintf(stderr, "The performance model directory is invalid\n");
  307. return 1;
  308. }
  309. }
  310. else {
  311. int ret = starpu_load_history_debug(symbol, &model);
  312. if (ret == 1)
  313. {
  314. fprintf(stderr, "The performance model could not be loaded\n");
  315. return 1;
  316. }
  317. display_all_perf_models(&model);
  318. }
  319. return 0;
  320. }