starpu_tasks_rec_complete.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2014, 2016 Universite de Bordeaux
  4. * Copyright (C) 2012-2015 CNRS
  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. #include <config.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdint.h>
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <common/uthash.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. UT_hash_handle hh;
  32. char *name;
  33. struct starpu_perfmodel model;
  34. } *models;
  35. int main(int argc, char *argv[]) {
  36. FILE *input;
  37. FILE *output;
  38. char s[1024], *c;
  39. uint32_t footprint = 0;
  40. int already_there = 0;
  41. char *model_name = NULL;
  42. struct model *model, *tmp;
  43. int ret;
  44. if (argc >= 2) {
  45. if (!strcmp(argv[1], "-h") ||
  46. !strcmp(argv[1], "--help"))
  47. {
  48. fprintf(stderr, "Complete a tasks.rec file with additional information, notably estimated termination times.\n");
  49. fprintf(stderr, "\n");
  50. fprintf(stderr, "Usage: %s [input-file [output-file]]\n", PROGNAME);
  51. fprintf(stderr, "\n");
  52. fprintf(stderr, "If input or output file names are not given, stdin and stdout are used.");
  53. fprintf(stderr, "\n");
  54. fprintf(stderr, "Report bugs to <" PACKAGE_BUGREPORT ">.\n");
  55. exit(EXIT_SUCCESS);
  56. }
  57. }
  58. if (starpu_init(NULL) != 0)
  59. {
  60. fprintf(stderr, "StarPU initialization failure\n");
  61. exit(EXIT_FAILURE);
  62. }
  63. starpu_pause();
  64. if (argc >= 2)
  65. {
  66. input = fopen(argv[1], "r");
  67. if (!input)
  68. {
  69. fprintf(stderr, "couldn't open %s for read: %s\n", argv[1], strerror(errno));
  70. exit(EXIT_FAILURE);
  71. }
  72. }
  73. else
  74. input = stdin;
  75. if (argc >= 3)
  76. {
  77. output = fopen(argv[2], "w+");
  78. if (!output)
  79. {
  80. fprintf(stderr, "couldn't open %s for write: %s\n", argv[1], strerror(errno));
  81. exit(EXIT_FAILURE);
  82. }
  83. }
  84. else
  85. output = stdout;
  86. while (fgets(s, sizeof(s), input))
  87. {
  88. if (strlen(s) == sizeof(s) - 1)
  89. {
  90. fprintf(stderr, "oops, very long line '%s', it's odd\n", s);
  91. exit(EXIT_FAILURE);
  92. }
  93. if (s[0] == '\n')
  94. {
  95. /* empty line, end of task */
  96. if (model_name)
  97. {
  98. if (already_there)
  99. {
  100. free(model_name);
  101. }
  102. else
  103. {
  104. /* Try to get already-loaded model */
  105. HASH_FIND_STR(models, model_name, model);
  106. if (model == NULL)
  107. {
  108. model = malloc(sizeof(*model));
  109. model->name = model_name;
  110. memset(&model->model, 0, sizeof(model->model));
  111. model->model.type = STARPU_PERFMODEL_INVALID;
  112. ret = starpu_perfmodel_load_symbol(model_name, &model->model);
  113. if (ret == 1)
  114. {
  115. fprintf(stderr, "The performance model for the symbol <%s> could not be loaded\n", model_name);
  116. exit(EXIT_FAILURE);
  117. }
  118. HASH_ADD_STR(models, name, model);
  119. }
  120. else
  121. free(model_name);
  122. fprintf(output, "EstimatedTime: ");
  123. starpu_perfmodel_print_estimations(&model->model, footprint, output);
  124. fprintf(output, "\n");
  125. }
  126. model_name = NULL;
  127. }
  128. already_there = 0;
  129. fprintf(output, "\n");
  130. continue;
  131. }
  132. /* Get rec field name */
  133. c = strchr(s, ':');
  134. if (!c)
  135. {
  136. fprintf(stderr, "odd line '%s'\n", s);
  137. exit(EXIT_FAILURE);
  138. }
  139. #define STRHEADCMP(s, head) strncmp(s, head, strlen(head))
  140. if (!STRHEADCMP(s, "Footprint: "))
  141. {
  142. footprint = strtoul(s + strlen("Footprint: "), NULL, 16);
  143. }
  144. else if (!STRHEADCMP(s, "Model: "))
  145. {
  146. model_name = strdup(s + strlen("Model: "));
  147. model_name[strlen(model_name) - 1] = '\0'; /* Drop '\n' */
  148. }
  149. else if (!STRHEADCMP(s, "EstimatedTime: "))
  150. {
  151. already_there = 1;
  152. }
  153. fprintf(output, "%s", s);
  154. }
  155. if (fclose(output))
  156. {
  157. fprintf(stderr, "couldn't close output: %s\n", strerror(errno));
  158. exit(EXIT_FAILURE);
  159. }
  160. starpu_resume();
  161. starpu_shutdown();
  162. HASH_ITER(hh, models, model, tmp)
  163. {
  164. free(model->name);
  165. HASH_DEL(models, model);
  166. }
  167. return 0;
  168. }