Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
coverage madness: use asserts here too
[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 <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, "Logging specific to this SimDag example");
18
19 int main(int argc, char **argv)
20 {
21   xbt_dynar_t dot;
22   unsigned int cursor;
23   SD_task_t task;
24
25   /* initialization of SD */
26   SD_init(&argc, argv);
27
28   /* Check our arguments */
29   xbt_assert(argc > 2, "Usage: %s platform_file dot_file [trace_file]"
30              "example: %s ../2clusters.xml dag.dot dag.mytrace", argv[0], argv[0]);
31
32   /* creation of the environment */
33   SD_create_environment(argv[1]);
34
35   /* load the DOT file  and schedule tasks */
36   dot = SD_dotload_with_sched(argv[2]);
37   if(!dot){
38     XBT_CRITICAL("The dot file with the provided scheduling is wrong, more information with the option : --log=sd_dotparse.thres:verbose");
39     SD_exit();
40     exit(2);
41   }
42
43   char *tracefilename;
44   char *last = strrchr(argv[2], '.');
45   tracefilename = bprintf("%.*s.trace", (int) (last == NULL ? strlen(argv[2]) : last - argv[2]),argv[2]);
46   if (argc == 4) 
47     tracefilename = xbt_strdup(argv[3]);
48   
49
50   /* Display all the tasks */
51   XBT_INFO("------------------- Display all tasks of the loaded DAG ---------------------------");
52   xbt_dynar_foreach(dot, cursor, task) {
53     SD_task_dump(task);
54   }
55
56   FILE *dotout = fopen("dot.dot", "w");
57   fprintf(dotout, "digraph A {\n");
58   xbt_dynar_foreach(dot, cursor, task) {
59     SD_task_dotty(task, dotout);
60   }
61   fprintf(dotout, "}\n");
62   fclose(dotout);
63
64   XBT_INFO("------------------- Run the schedule ---------------------------");
65   SD_simulate(-1);
66
67   XBT_INFO("------------------- Produce the trace file---------------------------");
68   XBT_INFO("Producing the trace of the run into %s", basename(tracefilename));
69   FILE *out = fopen(tracefilename, "w");
70   xbt_assert(out, "Cannot write to %s", tracefilename);
71   free(tracefilename);
72
73   xbt_dynar_foreach(dot, cursor, task) {
74     int kind = SD_task_get_kind(task);
75     sg_host_t *wsl = SD_task_get_workstation_list(task);
76     switch (kind) {
77     case SD_TASK_COMP_SEQ:
78       fprintf(out, "[%f->%f] %s compute %f flops # %s\n",
79           SD_task_get_start_time(task), SD_task_get_finish_time(task),
80           sg_host_get_name(wsl[0]), SD_task_get_amount(task), SD_task_get_name(task));
81       break;
82     case SD_TASK_COMM_E2E:
83       fprintf(out, "[%f -> %f] %s -> %s transfer of %.0f bytes # %s\n",
84           SD_task_get_start_time(task), SD_task_get_finish_time(task),
85           sg_host_get_name(wsl[0]), sg_host_get_name(wsl[1]), SD_task_get_amount(task), SD_task_get_name(task));
86       break;
87     default:
88       xbt_die("Task %s is of unknown kind %d", SD_task_get_name(task), SD_task_get_kind(task));
89     }
90     SD_task_destroy(task);
91   }
92   fclose(out);
93   xbt_dynar_free_container(&dot);
94
95   /* exit */
96   SD_exit();
97   return 0;
98 }