Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tesh file for simdag props updated...again
[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
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   SD_workstation_t w1 = workstations[0];
35   SD_workstation_t w2 = workstations[1];
36   SD_workstation_set_access_mode(w2, SD_WORKSTATION_SEQUENTIAL_ACCESS);
37   const char *name1 = SD_workstation_get_name(w1);
38   const char *name2 = SD_workstation_get_name(w2);
39
40   /* The host properties can be retrived from all interfaces */
41   xbt_dict_t props;
42   INFO1("Property list for workstation %s", name1);
43   /* Get the property list of the workstation 1 */
44   props = SD_workstation_get_properties(w1);
45   xbt_dict_cursor_t cursor = NULL;
46   char *key,*data;
47
48     /* Trying to set a new property */
49   xbt_dict_set(props, xbt_strdup("NewProp"), strdup("newValue"), free);
50
51   /* Print the properties of the workstation 1 */
52   xbt_dict_foreach(props,cursor,key,data) {
53     INFO2("\tProperty: %s has value: %s",key,data);
54   }
55  
56   /* Try to get a property that does not exist */
57   char noexist[]="NoProp";
58   const char *value = SD_workstation_get_property_value(w1,noexist);
59   if ( value == NULL) 
60     INFO1("\tProperty: %s is undefined", noexist);
61   else
62     INFO2("\tProperty: %s has value: %s", noexist, value);
63
64
65   INFO1("Property list for workstation %s", name2);
66   /* Get the property list of the workstation 2 */
67   props = SD_workstation_get_properties(w2);
68   cursor = NULL;
69
70   /* Print the properties of the workstation 2 */
71   xbt_dict_foreach(props,cursor,key,data) {
72     INFO2("\tProperty: %s on host: %s",key,data);
73   }
74
75   /* Modify an existing property test. First check it exists */\
76   INFO0("Modify an existing property");
77   char exist[]="Hdd";
78   value = SD_workstation_get_property_value(w2,exist);
79   if ( value == NULL) 
80     INFO1("\tProperty: %s is undefined", exist);
81   else {
82     INFO2("\tProperty: %s old value: %s", exist, value);
83     xbt_dict_set(props, exist, strdup("250"), free);  
84   }
85  
86   /* Test if we have changed the value */
87   value = SD_workstation_get_property_value(w2,exist);
88   if ( value == NULL) 
89     INFO1("\tProperty: %s is undefined", exist);
90   else
91     INFO2("\tProperty: %s new value: %s", exist, value);
92     
93   
94
95   const double computation_amount1 = 2000000;
96   const double computation_amount2 = 1000000;
97   const double communication_amount12 = 2000000;
98   const double communication_amount21 = 3000000;
99
100   /* NOTE: The link properties can be retrieved only from the SimDag interface */
101   const SD_link_t *route = SD_route_get_list(w1, w2);
102   int route_size = SD_route_get_size(w1, w2);
103   for (i = 0; i < route_size; i++) {
104      
105     props = SD_link_get_properties(route[i]);
106     xbt_dict_cursor_t cursor = NULL;
107     char *key,*data;
108
109     /* Print the properties of the current link */
110     xbt_dict_foreach(props,cursor,key,data) {
111     INFO3("\tLink %s property: %s has value: %s",SD_link_get_name(route[i]),key,data);
112
113     /* Try to get a property that does not exist */
114     char noexist1[]="Other";
115     value = SD_link_get_property_value(route[i], noexist1);
116     if ( value == NULL) 
117       INFO2("\tProperty: %s for link %s is undefined", noexist, SD_link_get_name(route[i]));
118     else
119       INFO3("\tLink %s property: %s has value: %s",SD_link_get_name(route[i]),noexist,value);
120   }
121
122   }
123   /* creation of the tasks and their dependencies */
124   SD_task_t taskA = SD_task_create("Task A", NULL, 10.0);
125   SD_task_t taskB = SD_task_create("Task B", NULL, 40.0);
126   SD_task_t taskC = SD_task_create("Task C", NULL, 30.0);
127   SD_task_t taskD = SD_task_create("Task D", NULL, 60.0);
128   
129
130   SD_task_dependency_add(NULL, NULL, taskB, taskA);
131   SD_task_dependency_add(NULL, NULL, taskC, taskA);
132   SD_task_dependency_add(NULL, NULL, taskD, taskB);
133   SD_task_dependency_add(NULL, NULL, taskD, taskC);
134
135   /* watch points */
136   SD_task_watch(taskD, SD_DONE);
137   SD_task_watch(taskB, SD_DONE);
138   SD_task_unwatch(taskD, SD_DONE);
139   
140
141   /* scheduling parameters */
142
143   const int workstation_number = 2;
144   const SD_workstation_t workstation_list[] = {w1, w2};
145   double computation_amount[] = {computation_amount1, computation_amount2};
146   double communication_amount[] =
147     {
148       0, communication_amount12,
149       communication_amount21, 0
150     };
151   double rate = -1.0;
152
153   /* estimated time */
154   SD_task_t task = taskD;
155   INFO2("Estimated time for '%s': %f", SD_task_get_name(task),
156         SD_task_get_execution_time(task, workstation_number, workstation_list,
157                                    computation_amount, communication_amount, rate));
158
159   /* let's launch the simulation! */
160
161   SD_task_schedule(taskA, workstation_number, workstation_list,
162                    computation_amount, communication_amount, rate);
163   SD_task_schedule(taskB, workstation_number, workstation_list,
164                    computation_amount, communication_amount, rate);
165   SD_task_schedule(taskC, workstation_number, workstation_list,
166                    computation_amount, communication_amount, rate);
167   SD_task_schedule(taskD, workstation_number, workstation_list,
168                    computation_amount, communication_amount, rate);
169
170   SD_task_t *changed_tasks;
171
172   changed_tasks = SD_simulate(-1.0);
173   for (i = 0; changed_tasks[i] != NULL; i++) {
174     INFO3("Task '%s' start time: %f, finish time: %f",
175           SD_task_get_name(changed_tasks[i]),
176           SD_task_get_start_time(changed_tasks[i]),
177           SD_task_get_finish_time(changed_tasks[i]));
178   }
179   
180   xbt_assert0(changed_tasks[0] == taskD &&
181               changed_tasks[1] == taskB &&
182               changed_tasks[2] == NULL,
183               "Unexpected simulation results");
184
185   xbt_free(changed_tasks);
186
187   DEBUG0("Destroying tasks...");
188
189   SD_task_destroy(taskA);
190   SD_task_destroy(taskB);
191   SD_task_destroy(taskC);
192   SD_task_destroy(taskD);
193
194   DEBUG0("Tasks destroyed. Exiting SimDag...");
195
196   SD_exit();
197   return 0;
198 }
199