Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics in ChangeLog and NEWS: releasing 3.6.2 (yuhu)
[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     XBT_INFO("Usage: %s platform_file dot_file [trace_file]", argv[0]);
31     XBT_INFO("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   if(dot == NULL){
52     XBT_INFO("No dot load may be you have a cycle in your graph");
53     return -1;
54   }
55
56   /* Display all the tasks */
57   XBT_INFO
58       ("------------------- Display all tasks of the loaded DAG ---------------------------");
59   xbt_dynar_foreach(dot, cursor, task) {
60     SD_task_dump(task);
61   }
62
63   FILE *dotout = fopen("dot.dot", "w");
64   fprintf(dotout, "digraph A {\n");
65   xbt_dynar_foreach(dot, cursor, task) {
66     SD_task_dotty(task, dotout);
67   }
68   fprintf(dotout, "}\n");
69   fclose(dotout);
70
71   /* Schedule them all on the first workstation */
72   XBT_INFO("------------------- Schedule tasks ---------------------------");
73   const SD_workstation_t *ws_list = SD_workstation_get_list();
74
75   int count = SD_workstation_get_number();
76   xbt_dynar_foreach(dot, cursor, task) {
77     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
78       if (!strcmp(SD_task_get_name(task), "end"))
79         SD_task_schedulel(task, 1, ws_list[0]);
80       else
81         SD_task_schedulel(task, 1, ws_list[cursor % count]);
82     }
83   }
84
85   XBT_INFO
86       ("------------------- Run the schedule ---------------------------");
87   changed = SD_simulate(-1);
88   xbt_dynar_free_container(&changed);
89   XBT_INFO
90       ("------------------- Produce the trace file---------------------------");
91   XBT_INFO("Producing the trace of the run into %s", tracefilename);
92   FILE *out = fopen(tracefilename, "w");
93   xbt_assert(out, "Cannot write to %s", tracefilename);
94   free(tracefilename);
95
96   xbt_dynar_foreach(dot, cursor, task) {
97     int kind = SD_task_get_kind(task);
98     SD_workstation_t *wsl = SD_task_get_workstation_list(task);
99     switch (kind) {
100     case SD_TASK_COMP_SEQ:
101       fprintf(out, "[%f] %s compute %f # %s\n",
102               SD_task_get_start_time(task),
103               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
104               SD_task_get_name(task));
105       break;
106     case SD_TASK_COMM_E2E:
107       fprintf(out, "[%f] %s send %s %f # %s\n",
108               SD_task_get_start_time(task),
109               SD_workstation_get_name(wsl[0]),
110               SD_workstation_get_name(wsl[1]), SD_task_get_amount(task),
111               SD_task_get_name(task));
112       fprintf(out, "[%f] %s recv %s %f # %s\n",
113               SD_task_get_finish_time(task),
114               SD_workstation_get_name(wsl[1]),
115               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
116               SD_task_get_name(task));
117       break;
118     default:
119       xbt_die("Task %s is of unknown kind %d", SD_task_get_name(task),
120               SD_task_get_kind(task));
121     }
122     SD_task_destroy(task);
123   }
124   fclose(out);
125
126   /* exit */
127   SD_exit();
128   return 0;
129 }