Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6bc8f255386a3063ce7744e67df65af37eaed3f1
[simgrid.git] / examples / simdag / dax / dax_test.c
1 /* simple test trying to load a DAX file.                                   */
2
3 /* Copyright (c) 2009-2015. 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
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;
31   unsigned int cursor;
32   SD_task_t task;
33
34   /* SD initialization */
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   if (!dax){
63     XBT_ERROR("A problem occurred during DAX parsing (cycle or syntax). Do not continue this test");
64     free(tracefilename);
65     SD_exit();
66     exit(255);
67   }
68
69   /* Display all the tasks */
70   XBT_INFO
71       ("------------------- Display all tasks of the loaded DAG ---------------------------");
72   xbt_dynar_foreach(dax, cursor, task) {
73     SD_task_dump(task);
74   }
75
76   FILE *dotout = fopen("dax.dot", "w");
77   fprintf(dotout, "digraph A {\n");
78   xbt_dynar_foreach(dax, cursor, task) {
79     SD_task_dotty(task, dotout);
80   }
81   fprintf(dotout, "}\n");
82   fclose(dotout);
83
84   /* Schedule them all on the first workstation */
85   XBT_INFO("------------------- Schedule tasks ---------------------------");
86   const SD_workstation_t *ws_list = SD_workstation_get_list();
87   int totalHosts = SD_workstation_get_number();
88   qsort((void *) ws_list, totalHosts, sizeof(SD_workstation_t),
89         name_compare_hosts);
90
91   int count = SD_workstation_get_number();
92   xbt_dynar_foreach(dax, cursor, task) {
93     if (SD_task_get_kind(task) == SD_TASK_COMP_SEQ) {
94       if (!strcmp(SD_task_get_name(task), "end"))
95         SD_task_schedulel(task, 1, ws_list[0]);
96       else
97         SD_task_schedulel(task, 1, ws_list[cursor % count]);
98     }
99   }
100
101   XBT_INFO
102       ("------------------- Run the schedule ---------------------------");
103   SD_simulate(-1);
104   XBT_INFO
105       ("------------------- Produce the trace file---------------------------");
106   XBT_INFO("Producing the trace of the run into %s", tracefilename);
107   FILE *out = fopen(tracefilename, "w");
108   xbt_assert(out, "Cannot write to %s", tracefilename);
109   free(tracefilename);
110
111   xbt_dynar_foreach(dax, cursor, task) {
112     int kind = SD_task_get_kind(task);
113     SD_workstation_t *wsl = SD_task_get_workstation_list(task);
114     switch (kind) {
115     case SD_TASK_COMP_SEQ:
116       fprintf(out, "[%f] %s compute %f # %s\n",
117               SD_task_get_start_time(task),
118               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
119               SD_task_get_name(task));
120       break;
121     case SD_TASK_COMM_E2E:
122       fprintf(out, "[%f] %s send %s %f # %s\n",
123               SD_task_get_start_time(task),
124               SD_workstation_get_name(wsl[0]),
125               SD_workstation_get_name(wsl[1]), SD_task_get_amount(task),
126               SD_task_get_name(task));
127       fprintf(out, "[%f] %s recv %s %f # %s\n",
128               SD_task_get_finish_time(task),
129               SD_workstation_get_name(wsl[1]),
130               SD_workstation_get_name(wsl[0]), SD_task_get_amount(task),
131               SD_task_get_name(task));
132       break;
133     default:
134       xbt_die("Task %s is of unknown kind %d", SD_task_get_name(task),
135               SD_task_get_kind(task));
136     }
137     SD_task_destroy(task);
138   }
139   fclose(out);
140   xbt_dynar_free_container(&dax);
141   /* exit */
142   SD_exit();
143   return 0;
144 }