Logo AND Algorithmique Numérique Distribuée

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