Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
62a03edae13238ae8b020934bb5f43d59cfd8b23
[simgrid.git] / examples / simdag / dot / dot_test.c
1 /* simple test trying to load a DOT file.                                   */
2
3 /* Copyright (c) 2010-2014. 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 */
40   dot = SD_dotload(argv[2]);
41   if(dot == NULL){
42     XBT_CRITICAL("No dot loaded. Do you have a cycle in your graph?");
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   /* Schedule them all on the first workstation */
75   XBT_INFO("------------------- Schedule tasks ---------------------------");
76   const SD_workstation_t *ws_list = SD_workstation_get_list();
77
78   int count = SD_workstation_get_number();
79   xbt_dynar_foreach(dot, cursor, task) {
80     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
81       if (!strcmp(SD_task_get_name(task), "end"))
82         SD_task_schedulel(task, 1, ws_list[0]);
83       else
84         SD_task_schedulel(task, 1, ws_list[cursor % count]);
85     }
86   }
87
88   XBT_INFO
89       ("------------------- Run the schedule ---------------------------");
90   changed = SD_simulate(-1);
91   xbt_dynar_free_container(&changed);
92   XBT_INFO
93       ("------------------- Produce the trace file---------------------------");
94   XBT_INFO("Producing the trace of the run into %s", basename(tracefilename));
95   FILE *out = fopen(tracefilename, "w");
96   xbt_assert(out, "Cannot write to %s", tracefilename);
97   free(tracefilename);
98
99   xbt_dynar_foreach(dot, cursor, task) {
100     int kind = SD_task_get_kind(task);
101     SD_workstation_t *wsl = SD_task_get_workstation_list(task);
102     switch (kind) {
103     case SD_TASK_COMP_SEQ:
104       fprintf(out, "[%f->%f] %s compute %f flops # %s\n",
105           SD_task_get_start_time(task),
106           SD_task_get_finish_time(task),
107           SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
108           SD_task_get_name(task));
109       break;
110     case SD_TASK_COMM_E2E:
111       fprintf(out, "[%f -> %f] %s -> %s transfer of %.0f bytes # %s\n",
112           SD_task_get_start_time(task),
113           SD_task_get_finish_time(task),
114           SD_workstation_get_name(wsl[0]),
115           SD_workstation_get_name(wsl[1]), 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   xbt_dynar_free_container(&dot);
125   fclose(out);
126
127   /* exit */
128   SD_exit();
129   return 0;
130 }