Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update SD_init() and SD_exit()
[simgrid.git] / src / simdag / sd_global.c
1 #include "private.h"
2 #include "xbt/sysdep.h"
3 #include "xbt/dynar.h"
4 #include "surf/surf.h"
5
6 XBT_LOG_NEW_CATEGORY(sd,"Logging specific to SimDag");
7 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_kernel,sd,
8                                 "Logging specific to SimDag (kernel)");
9
10 SD_global_t sd_global = NULL;
11
12 /**
13  * \brief Initialises SD internal data
14  *
15  * This function must be called before any other SD function. Then you
16  * should call SD_create_environment().
17  *
18  * \param argc argument number
19  * \param argv argument list
20  * \see SD_create_environment(), SD_exit()
21  */
22 void SD_init(int *argc, char **argv) {
23   if (SD_INITIALISED()) {
24     xbt_assert0(0, "SD_init() already called");
25   }
26
27   sd_global = xbt_new0(s_SD_global_t, 1);
28   sd_global->workstations = xbt_dict_new();
29   sd_global->workstation_count = 0;
30   sd_global->workstation_list = NULL;
31   sd_global->links = xbt_dict_new();
32   sd_global->link_count = 0;
33   sd_global->link_list = NULL;
34   sd_global->watch_point_reached = 0;
35
36   s_SD_task_t task;
37   sd_global->not_scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
38   sd_global->scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
39   sd_global->ready_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
40   sd_global->running_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
41   sd_global->done_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
42   sd_global->failed_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
43
44   surf_init(argc, argv);
45 }
46
47 /**
48  * \brief Creates the environment
49  *
50  * The environment (i.e. the \ref SD_workstation_management "workstations" and the
51  * \ref SD_link_management "links") is created with the data stored in the given XML
52  * platform file.
53  *
54  * \param platform_file name of an XML file describing the environment to create
55  * \see SD_workstation_management, SD_link_management
56  *
57  * The XML file follows this DTD:
58  *
59  *     \include surfxml.dtd
60  *
61  * Here is a small example of such a platform: 
62  *
63  *     \include small_platform.xml
64  */
65 void SD_create_environment(const char *platform_file) {
66   xbt_dict_cursor_t cursor = NULL;
67   char *name = NULL;
68   void *surf_workstation = NULL;
69   void *surf_link = NULL;
70
71   SD_CHECK_INIT_DONE();
72
73   surf_timer_resource_init(platform_file);  /* tell Surf to create the environnement */
74
75   surf_workstation_resource_init_KCCFLN05(platform_file);
76 /*   surf_workstation_resource_init_CLM03(platform_file); */
77
78   /* now let's create the SD wrappers for workstations and links */
79   xbt_dict_foreach(workstation_set, cursor, name, surf_workstation) {
80     __SD_workstation_create(surf_workstation, NULL);
81   }
82
83   xbt_dict_foreach(network_link_set, cursor, name, surf_link) {
84     __SD_link_create(surf_link, NULL);
85   }
86 }
87
88 /**
89  * \brief Launches the simulation.
90  *
91  * The function will execute the \ref SD_READY ready tasks.
92  * The simulation will be stopped when its time reaches \a how_long,
93  * when a watch point is reached, or when no more task can be executed.
94  * Then you can call SD_simulate() again.
95  * 
96  * \param how_long maximum duration of the simulation (a negative value means no time limit)
97  * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
98  * \see SD_task_schedule(), SD_task_watch()
99  */
100 SD_task_t* SD_simulate(double how_long)
101 {
102   double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
103   double elapsed_time = 0.0;
104   SD_task_t task, dst;
105   SD_dependency_t dependency;
106   surf_action_t action;
107   SD_task_t *changed_tasks = NULL;
108   int changed_task_number = 0;
109   int changed_task_capacity = 16; /* will be increased if necessary */
110   int i;
111   static int first_time = 1;
112
113   SD_CHECK_INIT_DONE();
114
115   INFO0("Starting simulation...");
116
117   /* create the array that will be returned */
118   changed_tasks = xbt_new0(SD_task_t, changed_task_capacity);
119   changed_tasks[0] = NULL;
120
121   if (first_time) {
122     surf_solve(); /* Takes traces into account. Returns 0.0 */
123     first_time = 0;
124   }
125
126   sd_global->watch_point_reached = 0;
127
128   /* explore the ready tasks */
129   xbt_swag_foreach(task, sd_global->ready_task_set) {
130     INFO1("Executing task '%s'", SD_task_get_name(task));
131     __SD_task_run(task);
132     surf_workstation_resource->common_public->action_set_data(task->surf_action, task);
133     task->state_changed = 1;
134     
135     changed_tasks[changed_task_number++] = task; /* replace NULL by the task */
136     if (changed_task_number == changed_task_capacity) {
137       changed_task_capacity *= 2;
138       changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
139     }
140     changed_tasks[changed_task_number] = NULL;
141   }
142
143   /* main loop */
144   elapsed_time = 0.0;
145   while (elapsed_time >= 0.0 &&
146          (how_long < 0.0 || total_time < how_long) &&
147          !sd_global->watch_point_reached) {
148
149     DEBUG1("Total time: %f", total_time);
150
151     elapsed_time = surf_solve();
152     DEBUG1("surf_solve() returns %f", elapsed_time);
153     if (elapsed_time > 0.0)
154       total_time += elapsed_time;
155
156     /* let's see which tasks are done */
157     while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.done_action_set))) {
158       task = action->data;
159       INFO1("Task '%s' done", SD_task_get_name(task));
160       __SD_task_set_state(task, SD_DONE);
161       surf_workstation_resource->common_public->action_free(action);
162       task->surf_action = NULL;
163
164       /* the state has changed */
165       if (!task->state_changed) {
166         task->state_changed = 1;
167         changed_tasks[changed_task_number++] = task;
168         if (changed_task_number == changed_task_capacity) {
169           changed_task_capacity *= 2;
170           changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
171         }
172         changed_tasks[changed_task_number] = NULL;
173       }
174
175       /* remove the dependencies after this task */
176       while (xbt_dynar_length(task->tasks_after) > 0) {
177         xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
178         dst = dependency->dst;
179         SD_task_dependency_remove(task, dst);
180         
181         /* is dst ready now? */
182         if (__SD_task_is_ready(dst) && !sd_global->watch_point_reached) {
183           INFO1("Executing task '%s'", SD_task_get_name(dst));
184           dst->surf_action = __SD_task_run(dst);
185           surf_workstation_resource->common_public->action_set_data(dst->surf_action, dst);
186           dst->state_changed = 1;
187           
188           changed_tasks[changed_task_number++] = dst;
189           if (changed_task_number == changed_task_capacity) {
190             changed_task_capacity *= 2;
191             changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
192           }
193           changed_tasks[changed_task_number] = NULL;
194         }
195       }
196     }
197
198     /* let's see which tasks have just failed */
199     while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.failed_action_set))) {
200       task = action->data;
201       INFO1("Task '%s' failed", SD_task_get_name(task));
202       __SD_task_set_state(task, SD_FAILED);
203       surf_workstation_resource->common_public->action_free(action);
204       task->surf_action = NULL;
205
206       if (!task->state_changed) {
207         task->state_changed = 1;
208         changed_tasks[changed_task_number++] = task;
209         if (changed_task_number == changed_task_capacity) {
210           changed_task_capacity *= 2;
211           changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
212         }
213         changed_tasks[changed_task_number] = NULL;
214       }
215     }
216   }
217
218   /* we must reset every task->state_changed */
219   i = 0;
220   while (changed_tasks[i] != NULL) {
221     changed_tasks[i]->state_changed = 0;
222     i++;
223   }
224
225   INFO0("Simulation finished");
226   DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d", elapsed_time, total_time, sd_global->watch_point_reached);
227   DEBUG1("current time = %f", surf_get_clock());
228
229   return changed_tasks;
230 }
231
232 /**
233  * \brief Returns the current clock
234  *
235  * \return the current clock, in second
236  */
237 double SD_get_clock(void) {
238   SD_CHECK_INIT_DONE();
239
240   return surf_get_clock();
241 }
242
243 /**
244  * \brief Destroys all SD internal data
245  *
246  * This function should be called when the simulation is over. Don't forget also to destroy
247  * the tasks.
248  *
249  * \see SD_init(), SD_task_destroy()
250  */
251 void SD_exit(void) {
252   if (SD_INITIALISED()) {
253     DEBUG0("Destroying workstation and link dictionaries...");
254     xbt_dict_free(&sd_global->workstations);
255     xbt_dict_free(&sd_global->links);
256
257     DEBUG0("Destroying workstation and link arrays if necessary...");
258     if (sd_global->workstation_list != NULL)
259       xbt_free(sd_global->workstation_list);
260
261     if (sd_global->link_list != NULL)
262       xbt_free(sd_global->link_list);
263
264     DEBUG0("Destroying the swags...");
265     xbt_swag_free(sd_global->not_scheduled_task_set);
266     xbt_swag_free(sd_global->scheduled_task_set);
267     xbt_swag_free(sd_global->ready_task_set);
268     xbt_swag_free(sd_global->running_task_set);
269     xbt_swag_free(sd_global->done_task_set);
270     xbt_swag_free(sd_global->failed_task_set);
271
272     xbt_free(sd_global);
273     sd_global = NULL;
274
275     DEBUG0("Exiting Surf...");
276     surf_exit();
277   }
278   else {
279     fprintf(stderr, "Warning: SD_exit() called while SimDag was not running\n");
280     /* we cannot use assertions here because xbt is not running! */
281   }
282 }