starpu_perfmodel_recdump.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2013-2014, 2017 Université de Bordeaux
  4. * Copyright (C) 2011, 2012, 2013, 2014, 2016 CNRS
  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. #if !defined(_WIN32) || defined(__MINGW32__) || defined(__CYGWIN__)
  19. #include <dirent.h>
  20. #include <sys/stat.h>
  21. #endif
  22. #include <config.h>
  23. #include <assert.h>
  24. #include <getopt.h>
  25. #include <unistd.h>
  26. #include <stdio.h>
  27. #include <starpu.h>
  28. #include <common/utils.h>
  29. #include <common/uthash.h>
  30. #include <core/perfmodel/perfmodel.h> // we need to browse the list associated to history-based models
  31. // just like in starpu_perfmodel_plot
  32. #define STRHEADCMP(s, head) strncmp(s, head, strlen(head))
  33. #if defined(_WIN32) && !defined(__CYGWIN__)
  34. #include <windows.h>
  35. #endif
  36. #define PROGNAME "starpu_perfmodel_recdump"
  37. typedef struct _footprint_list {
  38. struct _footprint_list* next;
  39. uint32_t footprint;
  40. } footprint_list;
  41. footprint_list* add_footprint(footprint_list* list, uint32_t footprint) {
  42. footprint_list * l = list;
  43. while(l) {
  44. if(l->footprint == footprint) break;
  45. l = l->next;
  46. }
  47. if(l) return list;
  48. else {
  49. footprint_list * res = malloc(sizeof(footprint_list));
  50. res->footprint = footprint;
  51. res->next = list;
  52. return res;
  53. }
  54. }
  55. static struct model {
  56. UT_hash_handle hh;
  57. char *name;
  58. struct starpu_perfmodel model;
  59. footprint_list* footprints;
  60. } *models;
  61. void get_comb_name(int comb, char* name, int name_size) {
  62. char* space;
  63. struct starpu_perfmodel_arch *arch_comb = starpu_perfmodel_arch_comb_fetch(comb);
  64. STARPU_ASSERT_MSG(arch_comb->ndevices == 1, "Cannot work with multi-device workers\n");
  65. snprintf(name, name_size, "%s%u", starpu_perfmodel_get_archtype_name(arch_comb->devices[0].type), arch_comb->devices[0].devid);
  66. }
  67. void print_archs(FILE* output) {
  68. int nb_combs = starpu_perfmodel_get_narch_combs();
  69. int nb_workers_per_comb[nb_combs];
  70. nb_combs = starpu_perfmodel_get_narch_combs();
  71. unsigned workerid; int comb;
  72. for(comb = 0; comb < nb_combs; comb++) nb_workers_per_comb[comb] = 0;
  73. fprintf(output, "%%rec: worker_count\n\n");
  74. for (workerid = 0; workerid < starpu_worker_get_count(); workerid++)
  75. {
  76. struct starpu_perfmodel_arch* arch = starpu_worker_get_perf_archtype(workerid, STARPU_NMAX_SCHED_CTXS);
  77. int comb = starpu_perfmodel_arch_comb_get(arch->ndevices, arch->devices);
  78. STARPU_ASSERT(comb >= 0);
  79. nb_workers_per_comb[comb] += 1;
  80. }
  81. for(comb = 0; comb < nb_combs; comb++) {
  82. if(nb_workers_per_comb > 0 ) {
  83. char name[32];
  84. get_comb_name(comb, name, 32);
  85. fprintf(output, "Architecture: %s\n", name);
  86. fprintf(output, "NbWorkers: %d\n\n", nb_workers_per_comb[comb]);
  87. }
  88. }
  89. }
  90. /* output file name */
  91. static char* poutput = NULL;
  92. static char* pinput = NULL;
  93. static void usage()
  94. {
  95. fprintf(stderr, "Dumps perfmodels to a rec file\n\n");
  96. fprintf(stderr, "Usage: %s [ output-file ]\n", PROGNAME);
  97. fprintf(stderr, "\n");
  98. fprintf(stderr, "If input or output file names are not given, stdin and stdout are used.");
  99. fprintf(stderr, "\n");
  100. fprintf(stderr, "Report bugs to <"PACKAGE_BUGREPORT">.");
  101. fprintf(stderr, "\n");
  102. }
  103. static void parse_args(int argc, char **argv)
  104. {
  105. int c;
  106. static struct option long_options[] =
  107. {
  108. {"help", no_argument, NULL, 'h'},
  109. {"output", required_argument, NULL, 'o'},
  110. {0, 0, 0, 0}
  111. };
  112. int option_index;
  113. while ((c = getopt_long(argc, argv, "ho:", long_options, &option_index)) != -1)
  114. {
  115. switch (c)
  116. {
  117. case 'h': /* display help */
  118. usage();
  119. exit(EXIT_SUCCESS);
  120. break;
  121. case 'o':
  122. poutput = optarg;
  123. break;
  124. case '?':
  125. default:
  126. fprintf(stderr, "Unrecognized option: -%c\n", optopt);
  127. }
  128. }
  129. if(optind < argc) {
  130. pinput = argv[optind++];
  131. if(optind < argc) {
  132. fprintf(stderr, "Unrecognized argument: %s\n", argv[optind]);
  133. exit(EXIT_FAILURE);
  134. }
  135. }
  136. }
  137. int main(int argc, char **argv)
  138. {
  139. FILE* output;
  140. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
  141. WSADATA wsadata;
  142. WSAStartup(MAKEWORD(1,0), &wsadata);
  143. _STARPU_MSG("Listing perfmodels is not implemented on pure Windows yet\n");
  144. return 1;
  145. #else
  146. parse_args(argc, argv);
  147. if(poutput != NULL) {
  148. output = fopen(poutput, "w+");
  149. if (!output)
  150. {
  151. fprintf(stderr, "couldn't open %s for write: %s\n", poutput, strerror(errno));
  152. exit(EXIT_FAILURE);
  153. }
  154. } else {
  155. output = stdout;
  156. }
  157. if (starpu_init(NULL) != 0)
  158. {
  159. fprintf(stderr, "StarPU initialization failure\n");
  160. exit(EXIT_FAILURE);
  161. }
  162. starpu_pause();
  163. if(pinput) {
  164. FILE* input = fopen(pinput, "r");
  165. char s[1024], *c;
  166. struct model *model, *tmp;
  167. uint32_t footprint = 0;
  168. char *model_name = NULL;
  169. int ret;
  170. if (!input) {
  171. fprintf(stderr, "couldn't open %s for read: %s\n", pinput, strerror(errno));
  172. exit(EXIT_FAILURE);
  173. }
  174. while (fgets(s, sizeof(s), input)) {
  175. if (strlen(s) == sizeof(s) - 1)
  176. {
  177. fprintf(stderr, "oops, very long line '%s', it's odd\n", s);
  178. exit(EXIT_FAILURE);
  179. }
  180. if (s[0] == '\n')
  181. {
  182. /* empty line, end of task */
  183. if (model_name)
  184. {
  185. /* Try to get already-loaded model */
  186. HASH_FIND_STR(models, model_name, model);
  187. if (model == NULL)
  188. {
  189. model = malloc(sizeof(*model));
  190. model->name = model_name;
  191. model->footprints = NULL;
  192. memset(&model->model, 0, sizeof(model->model));
  193. model->model.type = STARPU_PERFMODEL_INVALID;
  194. ret = starpu_perfmodel_load_symbol(model_name, &model->model);
  195. if (ret == 1)
  196. {
  197. fprintf(stderr, "The performance model for the symbol <%s> could not be loaded\n", model_name);
  198. exit(EXIT_FAILURE);
  199. }
  200. HASH_ADD_STR(models, name, model);
  201. }
  202. else
  203. free(model_name);
  204. model->footprints = add_footprint(model->footprints, footprint);
  205. model_name = NULL;
  206. }
  207. continue;
  208. }
  209. /* Get rec field name */
  210. c = strchr(s, ':');
  211. if (!c)
  212. {
  213. fprintf(stderr, "odd line '%s'\n", s);
  214. exit(EXIT_FAILURE);
  215. }
  216. if (!STRHEADCMP(s, "Footprint: "))
  217. {
  218. footprint = strtoul(s + strlen("Footprint: "), NULL, 16);
  219. }
  220. else if (!STRHEADCMP(s, "Model: "))
  221. {
  222. model_name = strdup(s + strlen("Model: "));
  223. model_name[strlen(model_name) - 1] = '\0'; /* Drop '\n' */
  224. }
  225. }
  226. /* All models loaded */
  227. {
  228. print_archs(output);
  229. fprintf(output, "%%rec: timing\n\n");
  230. int nb_combs = starpu_perfmodel_get_narch_combs();
  231. HASH_ITER(hh, models, model, tmp) {
  232. footprint_list* l = model->footprints, *ltmp;
  233. int comb;
  234. while(l) {
  235. for(comb = 0; comb < nb_combs; comb++) {
  236. char archname[32];
  237. get_comb_name(comb, archname, 32);
  238. if(!model->model.state || model->model.state->nimpls[comb] == 0){
  239. _STARPU_DISP("Symbol %s does not have any implementation on comb %d, not dumping\n", model->name, comb);
  240. continue;
  241. }
  242. if(model->model.state->nimpls[comb] > 1)
  243. _STARPU_DISP("Warning, more than one implementations in comb %d of symbol %s, using only the first one\n", comb, model->name);
  244. struct starpu_perfmodel_per_arch *arch_model = &model->model.state->per_arch[comb][0];
  245. struct starpu_perfmodel_history_list *ptr;
  246. ptr = arch_model->list;
  247. if(!ptr)
  248. _STARPU_DISP("Implementation %d of symbol %s does not have history based model, not dumping\n", comb, model->name);
  249. else while(ptr) {
  250. struct starpu_perfmodel_history_entry *entry = ptr->entry;
  251. if(entry->footprint == l->footprint) {
  252. fprintf(output, "Name: %s\n", model->name);
  253. fprintf(output, "Architecture: %s\n", archname);
  254. fprintf(output, "Footprint: %08x\n", l->footprint);
  255. fprintf(output, "Mean: %-15e\nStddev: %-15e\n",
  256. entry->mean, entry->deviation);
  257. fprintf(output, "\n");
  258. break;
  259. }
  260. ptr=ptr->next;
  261. }
  262. }
  263. ltmp = l->next;
  264. free(l);
  265. l = ltmp;
  266. }
  267. free(model->name);
  268. HASH_DEL(models, model);
  269. }
  270. }
  271. }
  272. else
  273. {
  274. char *path;
  275. DIR *dp;
  276. struct dirent *ep;
  277. path = _starpu_get_perf_model_dir_codelet();
  278. dp = opendir(path);
  279. if (dp != NULL)
  280. {
  281. while ((ep = readdir(dp)))
  282. {
  283. if (strcmp(ep->d_name, ".") && strcmp(ep->d_name, "..")) {
  284. int comb, nb_combs;
  285. char* symbol = strdup(ep->d_name);
  286. char *dot = strchr(symbol, '.');
  287. struct starpu_perfmodel model = {.type = STARPU_PERFMODEL_INVALID };
  288. if(dot) *dot = '\0';
  289. STARPU_ASSERT(starpu_perfmodel_load_symbol(symbol, &model) == 0);
  290. if(model.state == NULL)
  291. continue;
  292. nb_combs = starpu_perfmodel_get_narch_combs();
  293. for(comb = 0; comb < nb_combs; ++comb) {
  294. char name[32];
  295. get_comb_name(comb, name, 32);
  296. if(!model.state || model.state->nimpls[comb] == 0) {
  297. _STARPU_DISP("Symbol %s does not have any implementation on comb %d, not dumping\n", symbol, comb);
  298. fprintf(output, "\n");
  299. continue;
  300. }
  301. struct starpu_perfmodel_per_arch *arch_model = &model.state->per_arch[comb][0];
  302. struct starpu_perfmodel_history_list *ptr;
  303. ptr = arch_model->list;
  304. if(!ptr)
  305. _STARPU_DISP("Symbol %s for comb %d does not have history based model, not dumping\n", symbol, comb);
  306. else while(ptr) {
  307. struct starpu_perfmodel_history_entry *entry = ptr->entry;
  308. fprintf(output, "Name: %s\n", symbol);
  309. fprintf(output, "Architecture: %s\n", name);
  310. fprintf(output, "Footprint: %08x\nMean: %-15e\nStddev: %-15e\n",
  311. entry->footprint, entry->mean, entry->deviation);
  312. fprintf(output, "\n");
  313. ptr=ptr->next;
  314. }
  315. }
  316. }
  317. }
  318. closedir (dp);
  319. }
  320. else
  321. {
  322. _STARPU_DISP("Could not open the perfmodel directory <%s>: %s\n", path, strerror(errno));
  323. }
  324. print_archs(output);
  325. }
  326. starpu_resume();
  327. starpu_shutdown();
  328. return 0;
  329. #endif
  330. }