starpu_perfmodel_recdump.c 12 KB

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