starpu_perfmodel_display.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. /* display all available models */
  30. static int list = 0;
  31. /* what kernel ? */
  32. static char *symbol = NULL;
  33. /* what parameter should be displayed ? (NULL = all) */
  34. static char *parameter = NULL;
  35. /* which architecture ? (NULL = all)*/
  36. static char *arch = NULL;
  37. /* should we display a specific footprint ? */
  38. unsigned display_specific_footprint;
  39. uint32_t specific_footprint;
  40. static void usage(char **argv)
  41. {
  42. fprintf(stderr, "Display a given perfmodel\n\n");
  43. fprintf(stderr, "Usage: %s [ options ]\n", PROGNAME);
  44. fprintf(stderr, "\n");
  45. fprintf(stderr, "One must specify either -l or -s\n");
  46. fprintf(stderr, "Options:\n");
  47. fprintf(stderr, " -l display all available models\n");
  48. fprintf(stderr, " -s <symbol> specify the symbol\n");
  49. fprintf(stderr, " -p <parameter> specify the parameter (e.g. a, b, c, mean, stddev)\n");
  50. fprintf(stderr, " -a <arch> specify the architecture (e.g. cpu, cpu:k, cuda, gordon)\n");
  51. fprintf(stderr, " -f <footprint> display the history-based model for the specified footprint\n");
  52. fprintf(stderr, " -h, --help display this help and exit\n");
  53. fprintf(stderr, " -v, --version output version information and exit\n\n");
  54. fprintf(stderr, "Reports bugs to <"PACKAGE_BUGREPORT">.");
  55. fprintf(stderr, "\n");
  56. }
  57. static void parse_args(int argc, char **argv)
  58. {
  59. int c;
  60. static struct option long_options[] =
  61. {
  62. {"arch", required_argument, NULL, 'a'},
  63. {"footprint", required_argument, NULL, 'f'},
  64. {"help", no_argument, NULL, 'h'},
  65. /* XXX Would be cleaner to set a flag */
  66. {"list", no_argument, NULL, 'l'},
  67. {"parameter", required_argument, NULL, 'p'},
  68. {"symbol", required_argument, NULL, 's'},
  69. {"version", no_argument, NULL, 'v'},
  70. {0, 0, 0, 0}
  71. };
  72. int option_index;
  73. while ((c = getopt_long(argc, argv, "ls:p:a:f:h", long_options, &option_index)) != -1)
  74. {
  75. switch (c)
  76. {
  77. case 'l':
  78. /* list all models */
  79. list = 1;
  80. break;
  81. case 's':
  82. /* symbol */
  83. symbol = optarg;
  84. break;
  85. case 'p':
  86. /* parameter (eg. a, b, c, mean, stddev) */
  87. parameter = optarg;
  88. break;
  89. case 'a':
  90. /* architecture (cpu, cuda, gordon) */
  91. arch = optarg;
  92. break;
  93. case 'f':
  94. /* footprint */
  95. display_specific_footprint = 1;
  96. sscanf(optarg, "%08x", &specific_footprint);
  97. break;
  98. case 'h':
  99. usage(argv);
  100. exit(EXIT_SUCCESS);
  101. case 'v':
  102. (void) fprintf(stdout, "%s %d.%d\n",
  103. PROGNAME, STARPU_MAJOR_VERSION,
  104. STARPU_MINOR_VERSION);
  105. exit(EXIT_SUCCESS);
  106. case '?':
  107. default:
  108. fprintf(stderr, "Unrecognized option: -%c\n", optopt);
  109. }
  110. }
  111. if (!symbol && !list)
  112. {
  113. fprintf(stderr, "Incorrect usage, aborting\n");
  114. usage(argv);
  115. exit(-1);
  116. }
  117. }
  118. static void display_history_based_perf_model(struct starpu_per_arch_perfmodel *per_arch_model)
  119. {
  120. struct starpu_history_list *ptr;
  121. ptr = per_arch_model->list;
  122. if (!parameter && ptr)
  123. fprintf(stderr, "# hash\t\tsize\t\tmean\t\tdev\t\tn\n");
  124. while (ptr)
  125. {
  126. struct starpu_history_entry *entry = ptr->entry;
  127. if (!display_specific_footprint || (entry->footprint == specific_footprint))
  128. {
  129. if (!parameter)
  130. {
  131. /* There isn't a parameter that is explicitely requested, so we display all parameters */
  132. printf("%08x\t%-15lu\t%-15le\t%-15le\t%u\n", entry->footprint,
  133. (unsigned long) entry->size, entry->mean, entry->deviation, entry->nsample);
  134. }
  135. else
  136. {
  137. /* only display the parameter that was specifically requested */
  138. if (strcmp(parameter, "mean") == 0)
  139. {
  140. printf("%-15le\n", entry->mean);
  141. }
  142. if (strcmp(parameter, "stddev") == 0)
  143. {
  144. printf("%-15le\n", entry->deviation);
  145. return;
  146. }
  147. }
  148. }
  149. ptr = ptr->next;
  150. }
  151. }
  152. static void display_perf_model(struct starpu_perfmodel *model, enum starpu_perf_archtype arch, unsigned nimpl)
  153. {
  154. struct starpu_per_arch_perfmodel *arch_model = &model->per_arch[arch][nimpl];
  155. char archname[32];
  156. if (arch_model->regression.nsample || arch_model->regression.valid || arch_model->regression.nl_valid || arch_model->list)
  157. {
  158. starpu_perfmodel_get_arch_name(arch, archname, 32, nimpl);
  159. fprintf(stderr, "performance model for %s\n", archname);
  160. }
  161. if (parameter == NULL)
  162. {
  163. /* no specific parameter was requested, so we display everything */
  164. if (arch_model->regression.nsample)
  165. {
  166. fprintf(stderr, "\tRegression : #sample = %d\n", arch_model->regression.nsample);
  167. }
  168. /* Only display the regression model if we could actually build a model */
  169. if (arch_model->regression.valid)
  170. {
  171. fprintf(stderr, "\tLinear: y = alpha size ^ beta\n");
  172. fprintf(stderr, "\t\talpha = %e\n", arch_model->regression.alpha);
  173. fprintf(stderr, "\t\tbeta = %e\n", arch_model->regression.beta);
  174. }
  175. else
  176. {
  177. //fprintf(stderr, "\tLinear model is INVALID\n");
  178. }
  179. if (arch_model->regression.nl_valid)
  180. {
  181. fprintf(stderr, "\tNon-Linear: y = a size ^b + c\n");
  182. fprintf(stderr, "\t\ta = %e\n", arch_model->regression.a);
  183. fprintf(stderr, "\t\tb = %e\n", arch_model->regression.b);
  184. fprintf(stderr, "\t\tc = %e\n", arch_model->regression.c);
  185. }
  186. else
  187. {
  188. //fprintf(stderr, "\tNon-Linear model is INVALID\n");
  189. }
  190. display_history_based_perf_model(arch_model);
  191. #if 0
  192. char debugname[1024];
  193. starpu_perfmodel_debugfilepath(model, arch, debugname, 1024, nimpl);
  194. printf("\t debug file path : %s\n", debugname);
  195. #endif
  196. }
  197. else
  198. {
  199. /* only display the parameter that was specifically requested */
  200. if (strcmp(parameter, "a") == 0)
  201. {
  202. printf("%e\n", arch_model->regression.a);
  203. return;
  204. }
  205. if (strcmp(parameter, "b") == 0)
  206. {
  207. printf("%e\n", arch_model->regression.b);
  208. return;
  209. }
  210. if (strcmp(parameter, "c") == 0)
  211. {
  212. printf("%e\n", arch_model->regression.c);
  213. return;
  214. }
  215. if (strcmp(parameter, "alpha") == 0)
  216. {
  217. printf("%e\n", arch_model->regression.alpha);
  218. return;
  219. }
  220. if (strcmp(parameter, "beta") == 0)
  221. {
  222. printf("%e\n", arch_model->regression.beta);
  223. return;
  224. }
  225. if (strcmp(parameter, "path-file-debug") == 0)
  226. {
  227. char debugname[256];
  228. starpu_perfmodel_debugfilepath(model, arch, debugname, 1024, nimpl);
  229. printf("%s\n", debugname);
  230. return;
  231. }
  232. if ((strcmp(parameter, "mean") == 0) || (strcmp(parameter, "stddev")))
  233. {
  234. display_history_based_perf_model(arch_model);
  235. return;
  236. }
  237. /* TODO display if it's valid ? */
  238. fprintf(stderr, "Unknown parameter requested, aborting.\n");
  239. exit(-1);
  240. }
  241. }
  242. static void display_all_perf_models(struct starpu_perfmodel *model)
  243. {
  244. if (arch == NULL)
  245. {
  246. /* display all architectures */
  247. unsigned archid;
  248. unsigned implid;
  249. for (archid = 0; archid < STARPU_NARCH_VARIATIONS; archid++)
  250. {
  251. for (implid = 0; implid < STARPU_MAXIMPLEMENTATIONS; implid++)
  252. { /* Display all codelets on each arch */
  253. display_perf_model(model, (enum starpu_perf_archtype) archid, implid);
  254. }
  255. }
  256. }
  257. else
  258. {
  259. if (strcmp(arch, "cpu") == 0)
  260. {
  261. unsigned implid;
  262. for (implid = 0; implid < STARPU_MAXIMPLEMENTATIONS; implid++)
  263. display_perf_model(model, STARPU_CPU_DEFAULT,implid); /* Display all codelets on cpu */
  264. return;
  265. }
  266. int k;
  267. if (sscanf(arch, "cpu:%d", &k) == 1)
  268. {
  269. /* For combined CPU workers */
  270. if ((k < 1) || (k > STARPU_MAXCPUS))
  271. {
  272. fprintf(stderr, "Invalid CPU size\n");
  273. exit(-1);
  274. }
  275. unsigned implid;
  276. for (implid = 0; implid < STARPU_MAXIMPLEMENTATIONS; implid++)
  277. display_perf_model(model, (enum starpu_perf_archtype) (STARPU_CPU_DEFAULT + k - 1), implid);
  278. return;
  279. }
  280. if (strcmp(arch, "cuda") == 0)
  281. {
  282. unsigned archid;
  283. unsigned implid;
  284. for (archid = STARPU_CUDA_DEFAULT; archid < STARPU_CUDA_DEFAULT + STARPU_MAXCUDADEVS; archid++)
  285. {
  286. for (implid = 0; implid <STARPU_MAXIMPLEMENTATIONS; implid ++)
  287. {
  288. char archname[32];
  289. starpu_perfmodel_get_arch_name((enum starpu_perf_archtype) archid, archname, 32, implid);
  290. fprintf(stderr, "performance model for %s\n", archname);
  291. display_perf_model(model, (enum starpu_perf_archtype) archid, implid);
  292. }
  293. }
  294. return;
  295. }
  296. /* There must be a cleaner way ! */
  297. int gpuid;
  298. int nmatched;
  299. nmatched = sscanf(arch, "cuda_%d", &gpuid);
  300. if (nmatched == 1)
  301. {
  302. unsigned archid = STARPU_CUDA_DEFAULT+ gpuid;
  303. unsigned implid;
  304. for (implid = 0; implid < STARPU_MAXIMPLEMENTATIONS; implid++)
  305. display_perf_model(model, (enum starpu_perf_archtype) archid, implid);
  306. return;
  307. }
  308. if (strcmp(arch, "gordon") == 0)
  309. {
  310. fprintf(stderr, "performance model for gordon\n");
  311. unsigned implid;
  312. for (implid = 0; implid < STARPU_MAXIMPLEMENTATIONS; implid++)
  313. display_perf_model(model, STARPU_GORDON_DEFAULT, implid);
  314. return;
  315. }
  316. fprintf(stderr, "Unknown architecture requested, aborting.\n");
  317. exit(-1);
  318. }
  319. }
  320. int main(int argc, char **argv)
  321. {
  322. #ifdef __MINGW32__
  323. WSADATA wsadata;
  324. WSAStartup(MAKEWORD(1,0), &wsadata);
  325. #endif
  326. parse_args(argc, argv);
  327. if (list)
  328. {
  329. int ret = starpu_list_models(stdout);
  330. if (ret)
  331. {
  332. fprintf(stderr, "The performance model directory is invalid\n");
  333. return 1;
  334. }
  335. }
  336. else
  337. {
  338. struct starpu_perfmodel model;
  339. int ret = starpu_load_history_debug(symbol, &model);
  340. if (ret == 1)
  341. {
  342. fprintf(stderr, "The performance model could not be loaded\n");
  343. return 1;
  344. }
  345. display_all_perf_models(&model);
  346. }
  347. return 0;
  348. }