Logo AND Algorithmique Numérique Distribuée

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