Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New XBT module: file
[simgrid.git] / examples / simdag / dot / simulate_dot.c
1 /* simple test trying to load a DOT file.                                   */
2
3 /* Copyright (c) 2010-2015. 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 "simgrid/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   /* initialization 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
36   /* creation of the environment */
37   SD_create_environment(argv[1]);
38
39   /* load the DOT file  and schedule tasks */
40   dot = SD_dotload_with_sched(argv[2]);
41   if(!dot){
42     XBT_CRITICAL("The dot file with the provided scheduling is wrong, more information with the option : --log=sd_dotparse.thres:verbose");
43     SD_exit();
44     exit(2);
45   }
46
47   char *tracefilename;
48   if (argc == 3) {
49     char *last = strrchr(argv[2], '.');
50
51     tracefilename =
52         bprintf("%.*s.trace",
53             (int) (last == NULL ? strlen(argv[2]) : last - argv[2]),
54             argv[2]);
55   } else {
56     tracefilename = xbt_strdup(argv[3]);
57   }
58
59   /* Display all the tasks */
60   XBT_INFO
61   ("------------------- Display all tasks of the loaded DAG ---------------------------");
62   xbt_dynar_foreach(dot, cursor, task) {
63     SD_task_dump(task);
64   }
65
66   FILE *dotout = fopen("dot.dot", "w");
67   fprintf(dotout, "digraph A {\n");
68   xbt_dynar_foreach(dot, cursor, task) {
69     SD_task_dotty(task, dotout);
70   }
71   fprintf(dotout, "}\n");
72   fclose(dotout);
73
74   XBT_INFO
75   ("------------------- Run the schedule ---------------------------");
76   changed = SD_simulate(-1);
77   xbt_dynar_free_container(&changed);
78   XBT_INFO
79   ("------------------- Produce the trace file---------------------------");
80   XBT_INFO("Producing the trace of the run into %s", basename(tracefilename));
81   FILE *out = fopen(tracefilename, "w");
82   xbt_assert(out, "Cannot write to %s", tracefilename);
83   free(tracefilename);
84
85   xbt_dynar_foreach(dot, cursor, task) {
86     int kind = SD_task_get_kind(task);
87     SD_workstation_t *wsl = SD_task_get_workstation_list(task);
88     switch (kind) {
89     case SD_TASK_COMP_SEQ:
90       fprintf(out, "[%f->%f] %s compute %f flops # %s\n",
91           SD_task_get_start_time(task),
92           SD_task_get_finish_time(task),
93           SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
94           SD_task_get_name(task));
95       break;
96     case SD_TASK_COMM_E2E:
97       fprintf(out, "[%f -> %f] %s -> %s transfer of %.0f bytes # %s\n",
98           SD_task_get_start_time(task),
99           SD_task_get_finish_time(task),
100           SD_workstation_get_name(wsl[0]),
101           SD_workstation_get_name(wsl[1]), SD_task_get_amount(task),
102           SD_task_get_name(task));
103       break;
104     default:
105       xbt_die("Task %s is of unknown kind %d", SD_task_get_name(task),
106           SD_task_get_kind(task));
107     }
108     SD_task_destroy(task);
109   }
110   fclose(out);
111   xbt_dynar_free_container(&dot);
112
113   /* exit */
114   SD_exit();
115   return 0;
116 }