starpu_tasks_rec_complete.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #include <errno.h>
  20. #include <string.h>
  21. #include <common/config.h>
  22. #include <common/uthash.h>
  23. #include <common/utils.h>
  24. #include <starpu.h>
  25. #define PROGNAME "starpu_tasks_rec_complete"
  26. /*
  27. * This program takes a tasks.rec file, and emits a tasks.rec file with
  28. * additional information, notably estimated termination times.
  29. */
  30. static struct model
  31. {
  32. UT_hash_handle hh;
  33. char *name;
  34. struct starpu_perfmodel model;
  35. } *models;
  36. int main(int argc, char *argv[])
  37. {
  38. FILE *input;
  39. FILE *output;
  40. char s[1024], *c;
  41. uint32_t footprint = 0;
  42. int already_there = 0;
  43. char *model_name = NULL;
  44. struct model *model, *tmp=NULL;
  45. int ret;
  46. if (argc >= 2)
  47. {
  48. if (!strcmp(argv[1], "-h") ||
  49. !strcmp(argv[1], "--help"))
  50. {
  51. fprintf(stderr, "Complete a tasks.rec file with additional information, notably estimated termination times.\n");
  52. fprintf(stderr, "\n");
  53. fprintf(stderr, "Usage: %s [input-file [output-file]]\n", PROGNAME);
  54. fprintf(stderr, "\n");
  55. fprintf(stderr, "If input or output file names are not given, stdin and stdout are used.");
  56. fprintf(stderr, "\n");
  57. fprintf(stderr, "Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
  58. exit(EXIT_SUCCESS);
  59. }
  60. }
  61. #ifdef STARPU_HAVE_SETENV
  62. setenv("STARPU_FXT_TRACE", "0", 1);
  63. #endif
  64. if (starpu_init(NULL) != 0)
  65. {
  66. fprintf(stderr, "StarPU initialization failure\n");
  67. exit(EXIT_FAILURE);
  68. }
  69. starpu_pause();
  70. if (argc >= 2)
  71. {
  72. input = fopen(argv[1], "r");
  73. if (!input)
  74. {
  75. fprintf(stderr, "couldn't open %s for read: %s\n", argv[1], strerror(errno));
  76. exit(EXIT_FAILURE);
  77. }
  78. }
  79. else
  80. input = stdin;
  81. if (argc >= 3)
  82. {
  83. output = fopen(argv[2], "w+");
  84. if (!output)
  85. {
  86. fprintf(stderr, "couldn't open %s for write: %s\n", argv[1], strerror(errno));
  87. exit(EXIT_FAILURE);
  88. }
  89. }
  90. else
  91. output = stdout;
  92. while (fgets(s, sizeof(s), input))
  93. {
  94. if (strlen(s) == sizeof(s) - 1)
  95. {
  96. fprintf(stderr, "oops, very long line '%s', it's odd\n", s);
  97. exit(EXIT_FAILURE);
  98. }
  99. if (s[0] == '\n')
  100. {
  101. /* empty line, end of task */
  102. if (model_name)
  103. {
  104. if (already_there)
  105. {
  106. free(model_name);
  107. }
  108. else
  109. {
  110. /* Try to get already-loaded model */
  111. HASH_FIND_STR(models, model_name, model);
  112. if (model == NULL)
  113. {
  114. _STARPU_MALLOC(model, sizeof(*model));
  115. model->name = model_name;
  116. memset(&model->model, 0, sizeof(model->model));
  117. model->model.type = STARPU_PERFMODEL_INVALID;
  118. ret = starpu_perfmodel_load_symbol(model_name, &model->model);
  119. if (ret == 1)
  120. {
  121. fprintf(stderr, "The performance model for the symbol <%s> could not be loaded\n", model_name);
  122. exit(EXIT_FAILURE);
  123. }
  124. HASH_ADD_STR(models, name, model);
  125. }
  126. else
  127. free(model_name);
  128. fprintf(output, "EstimatedTime: ");
  129. starpu_perfmodel_print_estimations(&model->model, footprint, output);
  130. fprintf(output, "\n");
  131. }
  132. model_name = NULL;
  133. }
  134. already_there = 0;
  135. fprintf(output, "\n");
  136. continue;
  137. }
  138. /* Get rec field name */
  139. c = strchr(s, ':');
  140. if (!c)
  141. {
  142. fprintf(stderr, "odd line '%s'\n", s);
  143. exit(EXIT_FAILURE);
  144. }
  145. #define STRHEADCMP(s, head) strncmp(s, head, strlen(head))
  146. if (!STRHEADCMP(s, "Footprint: "))
  147. {
  148. footprint = strtoul(s + strlen("Footprint: "), NULL, 16);
  149. }
  150. else if (!STRHEADCMP(s, "Model: "))
  151. {
  152. model_name = strdup(s + strlen("Model: "));
  153. model_name[strlen(model_name) - 1] = '\0'; /* Drop '\n' */
  154. }
  155. else if (!STRHEADCMP(s, "EstimatedTime: "))
  156. {
  157. already_there = 1;
  158. }
  159. fprintf(output, "%s", s);
  160. }
  161. if (fclose(input))
  162. {
  163. fprintf(stderr, "couldn't close input: %s\n", strerror(errno));
  164. exit(EXIT_FAILURE);
  165. }
  166. if (fclose(output))
  167. {
  168. fprintf(stderr, "couldn't close output: %s\n", strerror(errno));
  169. exit(EXIT_FAILURE);
  170. }
  171. starpu_resume();
  172. starpu_shutdown();
  173. HASH_ITER(hh, models, model, tmp)
  174. {
  175. free(model->name);
  176. HASH_DEL(models, model);
  177. }
  178. return 0;
  179. }