starpu_tasks_rec_complete.c 4.1 KB

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