Logo AND Algorithmique Numérique Distribuée

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