starpu_perfmodel_recdump.c 11 KB

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