Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
319be2477e11358c70e97706bc9621e388c7390c
[simgrid.git] / examples / simdag / dot / dot_test.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
16 XBT_LOG_NEW_DEFAULT_CATEGORY(test,
17                              "Logging specific to this SimDag example");
18
19 int main(int argc, char **argv)
20 {
21   xbt_dynar_t dot, changed;
22   unsigned int cursor;
23   SD_task_t task;
24
25   /* initialisation of SD */
26   SD_init(&argc, argv);
27
28   TRACE_start ();
29
30   /* Check our arguments */
31   if (argc < 3) {
32     INFO1("Usage: %s platform_file dot_file [trace_file]", argv[0]);
33     INFO1("example: %s ../2clusters.xml dag.dot dag.mytrace", argv[0]);
34     exit(1);
35   }
36   char *tracefilename;
37   if (argc == 3) {
38     char *last = strrchr(argv[2], '.');
39
40     tracefilename =
41         bprintf("%.*s.trace",
42                 (int) (last == NULL ? strlen(argv[2]) : last - argv[2]),
43                 argv[2]);
44   } else {
45     tracefilename = xbt_strdup(argv[3]);
46   }
47
48   /* creation of the environment */
49   SD_create_environment(argv[1]);
50
51   /* load the DOT file */
52   dot = SD_dotload(argv[2]);
53
54   /* Display all the tasks */
55   INFO0
56       ("------------------- Display all tasks of the loaded DAG ---------------------------");
57   xbt_dynar_foreach(dot, cursor, task) {
58     SD_task_dump(task);
59   }
60
61   FILE *dotout = fopen("dot.dot", "w");
62   fprintf(dotout, "digraph A {\n");
63   xbt_dynar_foreach(dot, cursor, task) {
64     SD_task_dotty(task, dotout);
65   }
66   fprintf(dotout, "}\n");
67   fclose(dotout);
68
69   /* Schedule them all on the first workstation */
70   INFO0("------------------- Schedule tasks ---------------------------");
71   const SD_workstation_t *ws_list = SD_workstation_get_list();
72
73   int count = SD_workstation_get_number();
74   xbt_dynar_foreach(dot, cursor, task) {
75     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
76       if (!strcmp(SD_task_get_name(task), "end"))
77         SD_task_schedulel(task, 1, ws_list[0]);
78       else
79         SD_task_schedulel(task, 1, ws_list[cursor % count]);
80     }
81   }
82
83   INFO0
84       ("------------------- Run the schedule ---------------------------");
85   changed = SD_simulate(-1);
86   xbt_dynar_free_container(&changed);
87   INFO0
88       ("------------------- Produce the trace file---------------------------");
89   INFO1("Producing the trace of the run into %s", tracefilename);
90   FILE *out = fopen(tracefilename, "w");
91   xbt_assert1(out, "Cannot write to %s", tracefilename);
92   free(tracefilename);
93
94   xbt_dynar_foreach(dot, cursor, task) {
95     int kind = SD_task_get_kind(task);
96     SD_workstation_t *wsl = SD_task_get_workstation_list(task);
97     switch (kind) {
98     case SD_TASK_COMP_SEQ:
99       fprintf(out, "[%f] %s compute %f # %s\n",
100               SD_task_get_start_time(task),
101               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
102               SD_task_get_name(task));
103       break;
104     case SD_TASK_COMM_E2E:
105       fprintf(out, "[%f] %s send %s %f # %s\n",
106               SD_task_get_start_time(task),
107               SD_workstation_get_name(wsl[0]),
108               SD_workstation_get_name(wsl[1]), SD_task_get_amount(task),
109               SD_task_get_name(task));
110       fprintf(out, "[%f] %s recv %s %f # %s\n",
111               SD_task_get_finish_time(task),
112               SD_workstation_get_name(wsl[1]),
113               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
114               SD_task_get_name(task));
115       break;
116     default:
117       xbt_die(bprintf
118               ("Task %s is of unknown kind %d", SD_task_get_name(task),
119                SD_task_get_kind(task)));
120     }
121     SD_task_destroy(task);
122   }
123   fclose(out);
124
125   /* exit */
126   SD_exit();
127
128   TRACE_end();
129   return 0;
130 }