Logo AND Algorithmique Numérique Distribuée

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