Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
metaxml example Makefile modifications
[simgrid.git] / examples / simdag / metaxml / sd_test.c
1 /*
2    See examples/platforms/metaxml.xml and examples/platforms/metaxml_platform.xml files for examples on how to use the cluster, foreach, set, route:multi, trace and trace:connect tags
3 */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include "simdag/simdag.h"
7 #include "xbt/ex.h"
8 #include "xbt/log.h"
9 #include "xbt/dynar.h"
10 #include "xbt/dict.h"
11 #include "xbt/time.h"
12
13 XBT_LOG_NEW_DEFAULT_CATEGORY(sd_test,
14                              "Logging specific to this SimDag example");
15
16 int main(int argc, char **argv) {
17   /* initialisation of SD */
18   SD_init(&argc, argv);
19
20   /*  xbt_log_control_set("sd.thres=debug"); */
21
22   if (argc < 2) {
23     INFO1("Usage: %s platform_file", argv[0]);
24     INFO1("example: %s sd_platform.xml", argv[0]);
25     exit(1);
26   }
27
28   /* creation of the environment */
29   const char * platform_file = argv[1];
30   SD_create_environment(platform_file);
31
32   /* test the estimation functions */
33   const SD_workstation_t *workstations = SD_workstation_get_list();
34   int ws_nr = SD_workstation_get_number();
35
36   SD_workstation_t w1 = NULL;
37   SD_workstation_t w2 = NULL;
38   const char *name1, *name2;
39   /* Show routes between all workstation */
40   int i,j,k;
41   for (i=0; i<ws_nr; i++){
42     for (j=0;j<ws_nr; j++){
43       w1 = workstations[i];
44       w2 = workstations[j];
45       name1 = SD_workstation_get_name(w1);
46       name2 = SD_workstation_get_name(w2);
47       INFO2("Route between %s and %s:", name1, name2);   
48       const SD_link_t *route = SD_route_get_list(w1, w2);
49       int route_size = SD_route_get_size(w1, w2);
50       for (k = 0; k < route_size; k++) {
51         INFO3("\tLink %s: latency = %f, bandwidth = %f", SD_link_get_name(route[k]),
52           SD_link_get_current_latency(route[k]), SD_link_get_current_bandwidth(route[k]));
53       }
54     }
55   }
56
57   /* Simulate */
58
59   w1 = workstations[0];
60   w2 = workstations[1];
61   SD_workstation_set_access_mode(w2, SD_WORKSTATION_SEQUENTIAL_ACCESS);
62   name1 = SD_workstation_get_name(w1);
63   name2 = SD_workstation_get_name(w2);
64
65   const double computation_amount1 = 2000000;
66   const double computation_amount2 = 1000000;
67   const double communication_amount12 = 2000000;
68   const double communication_amount21 = 3000000;
69   INFO3("Computation time for %f flops on %s: %f", computation_amount1, name1,
70         SD_workstation_get_computation_time(w1, computation_amount1));
71   INFO3("Computation time for %f flops on %s: %f", computation_amount2, name2,
72         SD_workstation_get_computation_time(w2, computation_amount2));
73
74   /* creation of the tasks and their dependencies */
75   SD_task_t taskA = SD_task_create("Task A", NULL, 10.0);
76   SD_task_t taskB = SD_task_create("Task B", NULL, 40.0);
77   SD_task_t taskC = SD_task_create("Task C", NULL, 30.0);
78   SD_task_t taskD = SD_task_create("Task D", NULL, 60.0);
79   
80
81   SD_task_dependency_add(NULL, NULL, taskB, taskA);
82   SD_task_dependency_add(NULL, NULL, taskC, taskA);
83   SD_task_dependency_add(NULL, NULL, taskD, taskB);
84   SD_task_dependency_add(NULL, NULL, taskD, taskC);
85
86   xbt_ex_t ex;
87
88   TRY {
89     SD_task_dependency_add(NULL, NULL, taskA, taskA); /* shouldn't work and must raise an exception */
90     xbt_die("Hey, I can add a dependency between Task A and Task A!");
91   }
92   CATCH (ex) {
93     if (ex.category != arg_error)
94
95       RETHROW; /* this is a serious error */
96     xbt_ex_free(ex);
97   }
98   
99   TRY {
100     SD_task_dependency_add(NULL, NULL, taskB, taskA); /* shouldn't work and must raise an exception */
101     xbt_die("Oh oh, I can add an already existing dependency!");
102   }
103   CATCH (ex) {
104     if (ex.category != arg_error)
105       RETHROW;
106     xbt_ex_free(ex);
107   }
108
109   TRY {
110     SD_task_dependency_remove(taskA, taskC); /* shouldn't work and must raise an exception */
111     xbt_die("Dude, I can remove an unknown dependency!");
112   }
113   CATCH (ex) {
114     if (ex.category != arg_error)
115       RETHROW;
116     xbt_ex_free(ex);
117   }
118
119   TRY {
120     SD_task_dependency_remove(taskC, taskC); /* shouldn't work and must raise an exception */
121     xbt_die("Wow, I can remove a dependency between Task C and itself!");
122   }
123   CATCH (ex) {
124     if (ex.category != arg_error)
125       RETHROW;
126     xbt_ex_free(ex);
127   }
128
129
130   /* if everything is ok, no exception is forwarded or rethrown by main() */
131
132   /* watch points */
133   SD_task_watch(taskD, SD_DONE);
134   SD_task_watch(taskB, SD_DONE);
135   SD_task_unwatch(taskD, SD_DONE);
136   
137
138   /* scheduling parameters */
139
140   const int workstation_number = 2;
141   const SD_workstation_t workstation_list[] = {w1, w2};
142   double computation_amount[] = {computation_amount1, computation_amount2};
143   double communication_amount[] =
144     {
145       0, communication_amount12,
146       communication_amount21, 0
147     };
148   double rate = -1.0;
149
150   /* estimated time */
151   SD_task_t task = taskD;
152   INFO2("Estimated time for '%s': %f", SD_task_get_name(task),
153         SD_task_get_execution_time(task, workstation_number, workstation_list,
154                                    computation_amount, communication_amount, rate));
155
156   /* let's launch the simulation! */
157
158   SD_task_schedule(taskA, workstation_number, workstation_list,
159                    computation_amount, communication_amount, rate);
160   SD_task_schedule(taskB, workstation_number, workstation_list,
161                    computation_amount, communication_amount, rate);
162   SD_task_schedule(taskC, workstation_number, workstation_list,
163                    computation_amount, communication_amount, rate);
164   SD_task_schedule(taskD, workstation_number, workstation_list,
165                    computation_amount, communication_amount, rate);
166
167   SD_task_t *changed_tasks;
168
169   changed_tasks = SD_simulate(-1.0);
170   for (i = 0; changed_tasks[i] != NULL; i++) {
171     INFO3("Task '%s' start time: %f, finish time: %f",
172           SD_task_get_name(changed_tasks[i]),
173           SD_task_get_start_time(changed_tasks[i]),
174           SD_task_get_finish_time(changed_tasks[i]));
175   }
176   
177   xbt_assert0(changed_tasks[0] == taskD &&
178               changed_tasks[1] == taskB &&
179               changed_tasks[2] == NULL,
180               "Unexpected simulation results");
181
182   xbt_free(changed_tasks);
183
184   DEBUG0("Destroying tasks...");
185
186   SD_task_destroy(taskA);
187   SD_task_destroy(taskB);
188   SD_task_destroy(taskC);
189   SD_task_destroy(taskD);
190
191   DEBUG0("Tasks destroyed. Exiting SimDag...");
192
193   SD_exit();
194   return 0;
195 }
196