Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
final revalidation of this multi-example
[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 #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   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 */
51   dot = SD_dotload(argv[2]);
52   if(dot == NULL){
53     SD_exit();
54     xbt_die("No dot load may be you have a cycle in your graph");
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   /* Schedule them all on the first workstation */
73   XBT_INFO("------------------- Schedule tasks ---------------------------");
74   const SD_workstation_t *ws_list = SD_workstation_get_list();
75
76   int count = SD_workstation_get_number();
77   xbt_dynar_foreach(dot, cursor, task) {
78     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
79       if (!strcmp(SD_task_get_name(task), "end"))
80         SD_task_schedulel(task, 1, ws_list[0]);
81       else
82         SD_task_schedulel(task, 1, ws_list[cursor % count]);
83     }
84   }
85
86   XBT_INFO
87       ("------------------- Run the schedule ---------------------------");
88   changed = SD_simulate(-1);
89   xbt_dynar_free_container(&changed);
90   XBT_INFO
91       ("------------------- Produce the trace file---------------------------");
92   XBT_INFO("Producing the trace of the run into %s", basename(tracefilename));
93   FILE *out = fopen(tracefilename, "w");
94   xbt_assert(out, "Cannot write to %s", tracefilename);
95   free(tracefilename);
96
97   xbt_dynar_foreach(dot, cursor, task) {
98     int kind = SD_task_get_kind(task);
99     SD_workstation_t *wsl = SD_task_get_workstation_list(task);
100     switch (kind) {
101     case SD_TASK_COMP_SEQ:
102       fprintf(out, "[%f] %s compute %f # %s\n",
103               SD_task_get_start_time(task),
104               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
105               SD_task_get_name(task));
106       break;
107     case SD_TASK_COMM_E2E:
108       fprintf(out, "[%f] %s send %s %f # %s\n",
109               SD_task_get_start_time(task),
110               SD_workstation_get_name(wsl[0]),
111               SD_workstation_get_name(wsl[1]), SD_task_get_amount(task),
112               SD_task_get_name(task));
113       fprintf(out, "[%f] %s recv %s %f # %s\n",
114               SD_task_get_finish_time(task),
115               SD_workstation_get_name(wsl[1]),
116               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
117               SD_task_get_name(task));
118       break;
119     default:
120       xbt_die("Task %s is of unknown kind %d", SD_task_get_name(task),
121               SD_task_get_kind(task));
122     }
123     SD_task_destroy(task);
124   }
125   xbt_dynar_free_container(&dot);
126   fclose(out);
127
128   /* exit */
129   SD_exit();
130   return 0;
131 }