Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
07d8b17a62b416a30b55e72458e28772f1d909a1
[simgrid.git] / examples / simdag / properties / sd_prop.c
1 /*      $Id$     */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include "simdag/simdag.h"
6 #include "xbt/ex.h"
7 #include "xbt/log.h"
8 #include "xbt/dynar.h"
9 #include "xbt/dict.h"
10
11 XBT_LOG_NEW_DEFAULT_CATEGORY(test,
12                              "Property test");
13
14 int main(int argc, char **argv) {
15   int i;
16   const char * platform_file;
17   const SD_workstation_t *workstations;
18   SD_workstation_t w1;
19   SD_workstation_t w2;
20   const char *name1;
21   const char *name2;
22   xbt_dict_t props;
23   xbt_dict_cursor_t cursor = NULL;
24   char *key,*data;
25   char noexist[]="NoProp";
26   const char *value;
27   char exist[]="Hdd";
28   const double computation_amount1 = 2000000;
29   const double computation_amount2 = 1000000;
30   const double communication_amount12 = 2000000;
31   const double communication_amount21 = 3000000;
32   const SD_link_t *route;
33   int route_size;
34   SD_task_t taskA, taskB, taskC, taskD, task;
35   const int workstation_number = 2;
36   SD_workstation_t workstation_list[2];
37   double computation_amount[2];
38   double communication_amount[4] = {0};
39   double rate = -1.0;
40   SD_task_t *changed_tasks;
41
42   /* initialisation of SD */
43   SD_init(&argc, argv);
44         
45   platform_file = argv[1];
46
47   /*  xbt_log_control_set("sd.thres=debug"); */
48
49   if (argc < 2) {
50     INFO1("Usage: %s platform_file", argv[0]);
51     INFO1("example: %s sd_platform.xml", argv[0]);
52     exit(1);
53   }
54
55   /* creation of the environment */
56   SD_create_environment(platform_file);
57
58   /* test the estimation functions */
59   workstations = SD_workstation_get_list();
60   w1 = workstations[0];
61   w2 = workstations[1];
62   SD_workstation_set_access_mode(w2, SD_WORKSTATION_SEQUENTIAL_ACCESS);
63   name1 = SD_workstation_get_name(w1);
64   name2 = SD_workstation_get_name(w2);
65
66
67   /* The host properties can be retrived from all interfaces */
68   
69   INFO1("Property list for workstation %s", name1);
70   /* Get the property list of the workstation 1 */
71   props = SD_workstation_get_properties(w1);
72   
73
74     /* Trying to set a new property */
75   xbt_dict_set(props, xbt_strdup("NewProp"), strdup("newValue"), free);
76
77   /* Print the properties of the workstation 1 */
78   xbt_dict_foreach(props,cursor,key,data) {
79     INFO2("\tProperty: %s has value: %s",key,data);
80   }
81  
82   /* Try to get a property that does not exist */
83   
84   value = SD_workstation_get_property_value(w1,noexist);
85   if ( value == NULL) 
86     INFO1("\tProperty: %s is undefined", noexist);
87   else
88     INFO2("\tProperty: %s has value: %s", noexist, value);
89
90
91   INFO1("Property list for workstation %s", name2);
92   /* Get the property list of the workstation 2 */
93   props = SD_workstation_get_properties(w2);
94   cursor = NULL;
95
96   /* Print the properties of the workstation 2 */
97   xbt_dict_foreach(props,cursor,key,data) {
98     INFO2("\tProperty: %s on host: %s",key,data);
99   }
100
101   /* Modify an existing property test. First check it exists */\
102   INFO0("Modify an existing property");
103   
104   value = SD_workstation_get_property_value(w2,exist);
105   if ( value == NULL) 
106     INFO1("\tProperty: %s is undefined", exist);
107   else {
108     INFO2("\tProperty: %s old value: %s", exist, value);
109     xbt_dict_set(props, exist, strdup("250"), free);  
110   }
111  
112   /* Test if we have changed the value */
113   value = SD_workstation_get_property_value(w2,exist);
114   if ( value == NULL) 
115     INFO1("\tProperty: %s is undefined", exist);
116   else
117     INFO2("\tProperty: %s new value: %s", exist, value);
118
119   /* NOTE: The link properties can be retrieved only from the SimDag interface */
120   route = SD_route_get_list(w1, w2);
121   route_size = SD_route_get_size(w1, w2);
122   for (i = 0; i < route_size; i++) {
123     xbt_dict_cursor_t cursor = NULL;
124     char *key,*data;
125         char noexist1[]="Other";
126     props = SD_link_get_properties(route[i]);
127     
128
129     /* Print the properties of the current link */
130     xbt_dict_foreach(props,cursor,key,data) {
131     INFO3("\tLink %s property: %s has value: %s",SD_link_get_name(route[i]),key,data);
132
133     /* Try to get a property that does not exist */
134     
135     value = SD_link_get_property_value(route[i], noexist1);
136     if ( value == NULL) 
137       INFO2("\tProperty: %s for link %s is undefined", noexist, SD_link_get_name(route[i]));
138     else
139       INFO3("\tLink %s property: %s has value: %s",SD_link_get_name(route[i]),noexist,value);
140   }
141
142   }
143   /* creation of the tasks and their dependencies */
144   taskA = SD_task_create("Task A", NULL, 10.0);
145   taskB = SD_task_create("Task B", NULL, 40.0);
146   taskC = SD_task_create("Task C", NULL, 30.0);
147   taskD = SD_task_create("Task D", NULL, 60.0);
148   
149
150   SD_task_dependency_add(NULL, NULL, taskB, taskA);
151   SD_task_dependency_add(NULL, NULL, taskC, taskA);
152   SD_task_dependency_add(NULL, NULL, taskD, taskB);
153   SD_task_dependency_add(NULL, NULL, taskD, taskC);
154
155   /* watch points */
156   SD_task_watch(taskD, SD_DONE);
157   SD_task_watch(taskB, SD_DONE);
158   SD_task_unwatch(taskD, SD_DONE);
159   
160
161   /* scheduling parameters */
162   workstation_list[0] = w1;
163    workstation_list[1] = w2;
164   computation_amount[0] = computation_amount1;
165   computation_amount[1] = computation_amount2;
166   
167   communication_amount[1] = communication_amount12;
168   communication_amount[2] = communication_amount21;
169    
170  
171
172   /* estimated time */
173   task = taskD;
174   INFO2("Estimated time for '%s': %f", SD_task_get_name(task),
175         SD_task_get_execution_time(task, workstation_number, workstation_list,
176                                    computation_amount, communication_amount, rate));
177
178   /* let's launch the simulation! */
179
180   SD_task_schedule(taskA, workstation_number, workstation_list,
181                    computation_amount, communication_amount, rate);
182   SD_task_schedule(taskB, workstation_number, workstation_list,
183                    computation_amount, communication_amount, rate);
184   SD_task_schedule(taskC, workstation_number, workstation_list,
185                    computation_amount, communication_amount, rate);
186   SD_task_schedule(taskD, workstation_number, workstation_list,
187                    computation_amount, communication_amount, rate);
188
189   changed_tasks = SD_simulate(-1.0);
190   for (i = 0; changed_tasks[i] != NULL; i++) {
191     INFO3("Task '%s' start time: %f, finish time: %f",
192           SD_task_get_name(changed_tasks[i]),
193           SD_task_get_start_time(changed_tasks[i]),
194           SD_task_get_finish_time(changed_tasks[i]));
195   }
196   
197   xbt_assert0(changed_tasks[0] == taskD &&
198               changed_tasks[1] == taskB &&
199               changed_tasks[2] == NULL,
200               "Unexpected simulation results");
201
202   xbt_free(changed_tasks);
203
204   DEBUG0("Destroying tasks...");
205
206   SD_task_destroy(taskA);
207   SD_task_destroy(taskB);
208   SD_task_destroy(taskC);
209   SD_task_destroy(taskD);
210
211   DEBUG0("Tasks destroyed. Exiting SimDag...");
212
213   SD_exit();
214   return 0;
215 }
216