Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
instrumentation of the dax loader and its example
[simgrid.git] / examples / simdag / dax / dax_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 {
30   xbt_dynar_t dax, changed;
31   unsigned int cursor;
32   SD_task_t task;
33
34   /* initialisation of SD */
35   SD_init(&argc, argv);
36 #ifdef HAVE_TRACING
37   TRACE_start ();
38 #endif
39
40   /* Check our arguments */
41   if (argc < 3) {
42     INFO1("Usage: %s platform_file dax_file [trace_file]", argv[0]);
43     INFO1
44         ("example: %s ../sd_platform.xml Montage_50.xml Montage_50.mytrace",
45          argv[0]);
46     exit(1);
47   }
48   char *tracefilename;
49   if (argc == 3) {
50     char *last = strrchr(argv[2], '.');
51
52     tracefilename =
53         bprintf("%.*s.trace",
54                 (int) (last == NULL ? strlen(argv[2]) : last - argv[2]),
55                 argv[2]);
56   } else {
57     tracefilename = xbt_strdup(argv[3]);
58   }
59
60   /* creation of the environment */
61   SD_create_environment(argv[1]);
62
63   /* load the DAX file */
64   dax = SD_daxload(argv[2]);
65
66   /* Display all the tasks */
67   INFO0
68       ("------------------- Display all tasks of the loaded DAG ---------------------------");
69   xbt_dynar_foreach(dax, cursor, task) {
70     SD_task_dump(task);
71   }
72
73   FILE *dotout = fopen("dax.dot", "w");
74   fprintf(dotout, "digraph A {\n");
75   xbt_dynar_foreach(dax, cursor, task) {
76     SD_task_dotty(task, dotout);
77   }
78   fprintf(dotout, "}\n");
79   fclose(dotout);
80
81   /* Schedule them all on the first workstation */
82   INFO0("------------------- Schedule tasks ---------------------------");
83   const SD_workstation_t *ws_list = SD_workstation_get_list();
84   int totalHosts = SD_workstation_get_number();
85   qsort((void *) ws_list, totalHosts, sizeof(SD_workstation_t),
86         name_compare_hosts);
87
88   int count = SD_workstation_get_number();
89   xbt_dynar_foreach(dax, cursor, task) {
90     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
91       if (!strcmp(SD_task_get_name(task), "end"))
92         SD_task_schedulel(task, 1, ws_list[0]);
93       else
94         SD_task_schedulel(task, 1, ws_list[cursor % count]);
95     }
96   }
97
98   INFO0
99       ("------------------- Run the schedule ---------------------------");
100   changed = SD_simulate(-1);
101   xbt_dynar_free_container(&changed);
102   INFO0
103       ("------------------- Produce the trace file---------------------------");
104   INFO1("Producing the trace of the run into %s", tracefilename);
105   FILE *out = fopen(tracefilename, "w");
106   xbt_assert1(out, "Cannot write to %s", tracefilename);
107   free(tracefilename);
108
109   xbt_dynar_foreach(dax, cursor, task) {
110     int kind = SD_task_get_kind(task);
111     SD_workstation_t *wsl = SD_task_get_workstation_list(task);
112     switch (kind) {
113     case SD_TASK_COMP_SEQ:
114       fprintf(out, "[%f] %s compute %f # %s\n",
115               SD_task_get_start_time(task),
116               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
117               SD_task_get_name(task));
118       break;
119     case SD_TASK_COMM_E2E:
120       fprintf(out, "[%f] %s send %s %f # %s\n",
121               SD_task_get_start_time(task),
122               SD_workstation_get_name(wsl[0]),
123               SD_workstation_get_name(wsl[1]), SD_task_get_amount(task),
124               SD_task_get_name(task));
125       fprintf(out, "[%f] %s recv %s %f # %s\n",
126               SD_task_get_finish_time(task),
127               SD_workstation_get_name(wsl[1]),
128               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
129               SD_task_get_name(task));
130       break;
131     default:
132       xbt_die(bprintf
133               ("Task %s is of unknown kind %d", SD_task_get_name(task),
134                SD_task_get_kind(task)));
135     }
136     SD_task_destroy(task);
137   }
138   fclose(out);
139
140   /* exit */
141   SD_exit();
142 #ifdef HAVE_TRACING
143   TRACE_end();
144 #endif
145   return 0;
146 }