Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug a bunch of memleaks in SD examples
[simgrid.git] / examples / simdag / dag-dotload / sd_dag-dotload.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 "simgrid/simdag.h"
10 #include "xbt/log.h"
11 #include <stdio.h>
12 #include <string.h>
13 #include <libgen.h>
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "Logging specific to this SimDag example");
16
17 int main(int argc, char **argv)
18 {
19   xbt_dynar_t dot;
20   unsigned int cursor;
21   SD_task_t task;
22
23   /* initialization of SD */
24   SD_init(&argc, argv);
25
26   /* Check our arguments */
27   xbt_assert(argc > 2, "Usage: %s platform_file dot_file [trace_file]"
28              "example: %s ../2clusters.xml dag.dot dag.mytrace", argv[0], argv[0]);
29
30   /* creation of the environment */
31   SD_create_environment(argv[1]);
32
33   /* load the DOT file */
34   dot = SD_dotload(argv[2]);
35   if(dot == NULL){
36     XBT_CRITICAL("No dot loaded. Do you have a cycle in your graph?");
37     SD_exit();
38     exit(2);
39   }
40
41   char *tracefilename;
42   char *last = strrchr(argv[2], '.');
43   tracefilename = bprintf("%.*s.trace", (int) (last == NULL ? strlen(argv[2]) : last - argv[2]),argv[2]);
44   if (argc == 4) 
45     tracefilename = xbt_strdup(argv[3]);
46
47   /* Display all the tasks */
48   XBT_INFO("------------------- Display all tasks of the loaded DAG ---------------------------");
49   xbt_dynar_foreach(dot, cursor, task) {
50     SD_task_dump(task);
51   }
52
53   FILE *dotout = fopen("dot.dot", "w");
54   fprintf(dotout, "digraph A {\n");
55   xbt_dynar_foreach(dot, cursor, task) {
56     SD_task_dotty(task, dotout);
57   }
58   fprintf(dotout, "}\n");
59   fclose(dotout);
60
61   /* Schedule them all on the first workstation */
62   XBT_INFO("------------------- Schedule tasks ---------------------------");
63   sg_host_t *ws_list = sg_host_list();
64
65   int count = sg_host_count();
66   xbt_dynar_foreach(dot, cursor, task) {
67     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
68       if (!strcmp(SD_task_get_name(task), "end"))
69         SD_task_schedulel(task, 1, ws_list[0]);
70       else
71         SD_task_schedulel(task, 1, ws_list[cursor % count]);
72     }
73   }
74   xbt_free(ws_list);
75
76   XBT_INFO("------------------- Run the schedule ---------------------------");
77   SD_simulate(-1);
78
79   XBT_INFO("------------------- 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     sg_host_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), SD_task_get_finish_time(task),
92           sg_host_get_name(wsl[0]), SD_task_get_amount(task), SD_task_get_name(task));
93       break;
94     case SD_TASK_COMM_E2E:
95       fprintf(out, "[%f -> %f] %s -> %s transfer of %.0f bytes # %s\n",
96           SD_task_get_start_time(task), SD_task_get_finish_time(task),
97           sg_host_get_name(wsl[0]), sg_host_get_name(wsl[1]), SD_task_get_amount(task), SD_task_get_name(task));
98       break;
99     default:
100       xbt_die("Task %s is of unknown kind %d", SD_task_get_name(task), SD_task_get_kind(task));
101     }
102     SD_task_destroy(task);
103   }
104   xbt_dynar_free_container(&dot);
105   fclose(out);
106
107   /* exit */
108   SD_exit();
109   return 0;
110 }