Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
doesn't hurt to use types expected by some xbt_dynar functions
[simgrid.git] / examples / simdag / dot / simulate_dot.c
1 /* simple test trying to load a DOT file.                                   */
2
3 /* Copyright (c) 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include "simdag/simdag.h"
12 #include "xbt/log.h"
13 #include "xbt/ex.h"
14 #include <string.h>
15 #include <libgen.h>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test,
18                              "Logging specific to this SimDag example");
19
20 int main(int argc, char **argv)
21 {
22   xbt_dynar_t dot, changed;
23   unsigned int cursor;
24   SD_task_t task;
25
26   /* initialisation of SD */
27   SD_init(&argc, argv);
28
29   /* Check our arguments */
30   if (argc < 3) {
31     XBT_INFO("Usage: %s platform_file dot_file [trace_file]", argv[0]);
32     XBT_INFO("example: %s ../2clusters.xml dag.dot dag.mytrace", argv[0]);
33     exit(1);
34   }
35   char *tracefilename;
36   if (argc == 3) {
37     char *last = strrchr(argv[2], '.');
38
39     tracefilename =
40         bprintf("%.*s.trace",
41                 (int) (last == NULL ? strlen(argv[2]) : last - argv[2]),
42                 argv[2]);
43   } else {
44     tracefilename = xbt_strdup(argv[3]);
45   }
46
47   /* creation of the environment */
48   SD_create_environment(argv[1]);
49
50   /* load the DOT file  and schedule tasks */
51   dot = SD_dotload_with_sched(argv[2]);
52   if(!dot){
53     SD_exit();
54     xbt_die("The dot file with the provided scheduling is wrong, more information with the option : --log=sd_dotparse.thres:verbose");
55   }
56
57   /* Display all the tasks */
58   XBT_INFO
59       ("------------------- Display all tasks of the loaded DAG ---------------------------");
60   xbt_dynar_foreach(dot, cursor, task) {
61     SD_task_dump(task);
62   }
63
64   FILE *dotout = fopen("dot.dot", "w");
65   fprintf(dotout, "digraph A {\n");
66   xbt_dynar_foreach(dot, cursor, task) {
67     SD_task_dotty(task, dotout);
68   }
69   fprintf(dotout, "}\n");
70   fclose(dotout);
71
72   XBT_INFO
73       ("------------------- Run the schedule ---------------------------");
74   changed = SD_simulate(-1);
75   xbt_dynar_free_container(&changed);
76   XBT_INFO
77       ("------------------- Produce the trace file---------------------------");
78   XBT_INFO("Producing the trace of the run into %s", basename(tracefilename));
79   FILE *out = fopen(tracefilename, "w");
80   xbt_assert(out, "Cannot write to %s", tracefilename);
81   free(tracefilename);
82
83   xbt_dynar_foreach(dot, cursor, task) {
84     int kind = SD_task_get_kind(task);
85     SD_workstation_t *wsl = SD_task_get_workstation_list(task);
86     switch (kind) {
87     case SD_TASK_COMP_SEQ:
88       fprintf(out, "[%f] %s compute %f # %s\n",
89               SD_task_get_start_time(task),
90               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
91               SD_task_get_name(task));
92       break;
93     case SD_TASK_COMM_E2E:
94       fprintf(out, "[%f] %s send %s %f # %s\n",
95               SD_task_get_start_time(task),
96               SD_workstation_get_name(wsl[0]),
97               SD_workstation_get_name(wsl[1]), SD_task_get_amount(task),
98               SD_task_get_name(task));
99       fprintf(out, "[%f] %s recv %s %f # %s\n",
100               SD_task_get_finish_time(task),
101               SD_workstation_get_name(wsl[1]),
102               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
103               SD_task_get_name(task));
104       break;
105     default:
106       xbt_die("Task %s is of unknown kind %d", SD_task_get_name(task),
107               SD_task_get_kind(task));
108     }
109     SD_task_destroy(task);
110   }
111   fclose(out);
112
113   /* exit */
114   SD_exit();
115   return 0;
116 }