Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
22cc2429ed2975e4c5564b8dde72c36f3b17ee1b
[simgrid.git] / examples / simdag / dot / simulate_dot.c
1 /* simple test trying to load a DOT file.                                   */
2
3 /* Copyright (c) 2010-2016. 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 <stdio.h>
10 #include "simgrid/simdag.h"
11 #include <string.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  and schedule tasks */
33   dot = SD_dotload_with_sched(argv[2]);
34   if(!dot){
35     XBT_CRITICAL("The dot file with the provided scheduling is wrong, more information with the option : --log=sd_dotparse.thres:verbose");
36     SD_exit();
37     exit(2);
38   }
39
40   char *tracefilename;
41   char *last = strrchr(argv[2], '.');
42   tracefilename = bprintf("%.*s.trace", (int) (last == NULL ? strlen(argv[2]) : last - argv[2]),argv[2]);
43   if (argc == 4) 
44     tracefilename = xbt_strdup(argv[3]);
45   
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   XBT_INFO("------------------- Run the schedule ---------------------------");
62   SD_simulate(-1);
63
64   XBT_INFO("------------------- Produce the trace file---------------------------");
65   XBT_INFO("Producing the trace of the run into %s", basename(tracefilename));
66   FILE *out = fopen(tracefilename, "w");
67   xbt_assert(out, "Cannot write to %s", tracefilename);
68   free(tracefilename);
69
70   xbt_dynar_foreach(dot, cursor, task) {
71     int kind = SD_task_get_kind(task);
72     sg_host_t *wsl = SD_task_get_workstation_list(task);
73     switch (kind) {
74     case SD_TASK_COMP_SEQ:
75       fprintf(out, "[%f->%f] %s compute %f flops # %s\n",
76           SD_task_get_start_time(task), SD_task_get_finish_time(task),
77           sg_host_get_name(wsl[0]), SD_task_get_amount(task), SD_task_get_name(task));
78       break;
79     case SD_TASK_COMM_E2E:
80       fprintf(out, "[%f -> %f] %s -> %s transfer of %.0f bytes # %s\n",
81           SD_task_get_start_time(task), SD_task_get_finish_time(task),
82           sg_host_get_name(wsl[0]), sg_host_get_name(wsl[1]), SD_task_get_amount(task), SD_task_get_name(task));
83       break;
84     default:
85       xbt_die("Task %s is of unknown kind %d", SD_task_get_name(task), SD_task_get_kind(task));
86     }
87     SD_task_destroy(task);
88   }
89   fclose(out);
90   xbt_dynar_free_container(&dot);
91
92   /* exit */
93   SD_exit();
94   return 0;
95 }