Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a dot loader to have a connection with the random dag generator ggen.
[simgrid.git] / examples / simdag / dot / dot_test.c
1 /* simple test trying to load a DAX file.                                   */
2
3 /* Copyright (c) 2009, 2010. 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 "simdag/simdag.h"
12 #include "xbt/log.h"
13 #include "xbt/ex.h"
14 #include <string.h>
15
16 XBT_LOG_NEW_DEFAULT_CATEGORY(test,
17                              "Logging specific to this SimDag example");
18
19 static int name_compare_hosts(const void *n1, const void *n2)
20 {
21   char name1[80], name2[80];
22   strcpy(name1, SD_workstation_get_name(*((SD_workstation_t *) n1)));
23   strcpy(name2, SD_workstation_get_name(*((SD_workstation_t *) n2)));
24
25   return strcmp(name1, name2);
26 }
27
28 int main(int argc, char **argv) {
29   xbt_dynar_t dot, changed;
30   unsigned int cursor;
31   SD_task_t task;
32
33   /* initialisation of SD */
34   SD_init(&argc, argv);
35
36   /* Check our arguments */
37   if (argc < 3) {
38     INFO1("Usage: %s platform_file dot_file [trace_file]", argv[0]);
39     INFO1("example: %s ../sd_platform.xml Montage_50.xml Montage_50.mytrace", argv[0]);
40     exit(1);
41   }
42   char *tracefilename;
43   if (argc == 3) {
44     char *last=strrchr(argv[2],'.');
45
46     tracefilename=bprintf("%.*s.trace",(int)(last==NULL?strlen(argv[2]):last-argv[2]),argv[2]);
47   } else {
48     tracefilename = xbt_strdup(argv[3]);
49   }
50
51   /* creation of the environment */
52   SD_create_environment(argv[1]);
53
54   /* load the DAX file */
55   dot=SD_dotload(argv[2]);
56         INFO1("dot : %lld",dot);
57
58   /* Display all the tasks */
59   INFO0("------------------- Display all tasks of the loaded DAG ---------------------------");
60   xbt_dynar_foreach(dot,cursor,task) {
61     SD_task_dump(task);
62   }
63
64   FILE *dotout = fopen("dot.dot","w");
65   fprintf(dotout,"digraph A {\n");
66   xbt_dynar_foreach(dot,cursor,task) {
67     SD_task_dotty(task,dotout);
68   }
69   fprintf(dotout,"}\n");
70   fclose(dotout);
71
72   /* Schedule them all on the first workstation */
73   INFO0("------------------- Schedule tasks ---------------------------");
74   const SD_workstation_t *ws_list =  SD_workstation_get_list();
75   int totalHosts = SD_workstation_get_number();
76   qsort((void *) ws_list, totalHosts, sizeof(SD_workstation_t),
77         name_compare_hosts);
78
79   int count = SD_workstation_get_number();
80   xbt_dynar_foreach(dot,cursor,task) {
81     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
82       if (!strcmp(SD_task_get_name(task),"end"))
83         SD_task_schedulel(task,1,ws_list[0]);
84       else
85         SD_task_schedulel(task,1,ws_list[cursor%count]);
86     }
87   }
88
89   INFO0("------------------- Run the schedule ---------------------------");
90   changed = SD_simulate(-1);
91   xbt_dynar_free_container(&changed);
92   INFO0("------------------- Produce the trace file---------------------------");
93   INFO1("Producing the trace of the run into %s",tracefilename);
94   FILE*out = fopen(tracefilename,"w");
95   xbt_assert1(out,"Cannot write to %s",tracefilename);
96   free(tracefilename);
97
98   xbt_dynar_foreach(dot,cursor,task) {
99     int kind = SD_task_get_kind(task);
100     SD_workstation_t *wsl = SD_task_get_workstation_list(task);
101     switch (kind) {
102     case SD_TASK_COMP_SEQ:
103       fprintf(out,"[%f] %s compute %f # %s\n",SD_task_get_start_time(task),
104           SD_workstation_get_name(wsl[0]),SD_task_get_amount(task),
105           SD_task_get_name(task));
106       break;
107     case SD_TASK_COMM_E2E:
108       fprintf(out,"[%f] %s send %s %f # %s\n",SD_task_get_start_time(task),
109           SD_workstation_get_name(wsl[0]),SD_workstation_get_name(wsl[1]),
110           SD_task_get_amount(task), SD_task_get_name(task));
111       fprintf(out,"[%f] %s recv %s %f # %s\n",SD_task_get_finish_time(task),
112           SD_workstation_get_name(wsl[1]),SD_workstation_get_name(wsl[0]),
113           SD_task_get_amount(task), SD_task_get_name(task));
114       break;
115     default:
116       xbt_die(bprintf("Task %s is of unknown kind %d",SD_task_get_name(task),SD_task_get_kind(task)));
117     }
118     SD_task_destroy(task);
119   }
120   fclose(out);
121
122   /* exit */
123   SD_exit();
124   return 0;
125 }