Logo AND Algorithmique Numérique Distribuée

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