Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Hack so that SD_simulate(2.0) stops after 2 seconds...
[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
24   s_SD_task_t task;
25   
26   xbt_assert0( !SD_INITIALISED() , "SD_init() already called");
27
28   sd_global = xbt_new(s_SD_global_t, 1);
29   sd_global->workstations = xbt_dict_new();
30   sd_global->workstation_count = 0;
31   sd_global->workstation_list = NULL;
32   sd_global->links = xbt_dict_new();
33   sd_global->link_count = 0;
34   sd_global->link_list = NULL;
35   sd_global->recyclable_route = NULL;
36   sd_global->watch_point_reached = 0;
37
38   sd_global->not_scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
39   sd_global->scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
40   sd_global->ready_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
41   sd_global->in_fifo_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
42   sd_global->running_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
43   sd_global->done_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
44   sd_global->failed_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
45   sd_global->task_number = 0;
46
47   surf_init(argc, argv);
48 }
49
50 /**
51  * \brief Reinits the application part of the simulation (experimental feature)
52  * 
53  * This function allows you to run several simulations on the same platform 
54  * by resetting the part describing the application. 
55  * 
56  * @warning: this function is still experimental and not perfect. For example,
57  * the simulation clock (and traces usage) is not reset. So, do not use it if
58  * you use traces in your simulation, and do not use absolute timing after using it.
59  * That being said, this function is still precious if you want to compare a bunch of
60  * heuristics on the same platforms.
61  */
62 void SD_application_reinit(void) {
63    
64   s_SD_task_t task;
65    
66   if (SD_INITIALISED()) {
67     DEBUG0("Recreating the swags...");
68     xbt_swag_free(sd_global->not_scheduled_task_set);
69     xbt_swag_free(sd_global->scheduled_task_set);
70     xbt_swag_free(sd_global->ready_task_set);
71     xbt_swag_free(sd_global->in_fifo_task_set);
72     xbt_swag_free(sd_global->running_task_set);
73     xbt_swag_free(sd_global->done_task_set);
74     xbt_swag_free(sd_global->failed_task_set);
75
76     sd_global->not_scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
77     sd_global->scheduled_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
78     sd_global->ready_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
79     sd_global->in_fifo_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
80     sd_global->running_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
81     sd_global->done_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
82     sd_global->failed_task_set = xbt_swag_new(xbt_swag_offset(task, state_hookup));
83     sd_global->task_number = 0;
84   } else {
85     WARN0("SD_application_reinit called before initialization of SimDag");
86     /* we cannot use exceptions here because xbt is not running! */
87   }
88
89 }
90
91
92
93 /**
94  * \brief Creates the environment
95  *
96  * The environment (i.e. the \ref SD_workstation_management "workstations" and the
97  * \ref SD_link_management "links") is created with the data stored in the given XML
98  * platform file.
99  *
100  * \param platform_file name of an XML file describing the environment to create
101  * \see SD_workstation_management, SD_link_management
102  *
103  * The XML file follows this DTD:
104  *
105  *     \include surfxml.dtd
106  *
107  * Here is a small example of such a platform: 
108  *
109  *     \include small_platform.xml
110  */
111 void SD_create_environment(const char *platform_file) {
112   xbt_dict_cursor_t cursor = NULL;
113   char *name = NULL;
114   void *surf_workstation = NULL;
115   void *surf_link = NULL;
116
117   SD_CHECK_INIT_DONE();
118
119   DEBUG0("SD_create_environment");
120
121   surf_timer_resource_init(platform_file);  /* tell Surf to create the environnement */
122
123   DEBUG0("Calling surf_workstation_resource_init");
124   surf_workstation_resource_init_KCCFLN05(platform_file);
125   /*  surf_workstation_resource_init_CLM03(platform_file); */
126
127   /* now let's create the SD wrappers for workstations and links */
128   DEBUG0("Creating SimDags hosts and links");
129   xbt_dict_foreach(workstation_set, cursor, name, surf_workstation) {
130     __SD_workstation_create(surf_workstation, NULL);
131   }
132
133   xbt_dict_foreach(network_link_set, cursor, name, surf_link) {
134     __SD_link_create(surf_link, NULL);
135   }
136
137   DEBUG2("Workstation number: %d, link number: %d", SD_workstation_get_number(), SD_link_get_number());
138 }
139
140 /**
141  * \brief Launches the simulation.
142  *
143  * The function will execute the \ref SD_READY ready tasks.
144  * The simulation will be stopped when its time reaches \a how_long,
145  * when a watch point is reached, or when no more task can be executed.
146  * Then you can call SD_simulate() again.
147  * 
148  * \param how_long maximum duration of the simulation (a negative value means no time limit)
149  * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
150  * \see SD_task_schedule(), SD_task_watch()
151  */
152 SD_task_t* SD_simulate(double how_long)
153 {
154   double total_time = 0.0; /* we stop the simulation when total_time >= how_long */
155   double elapsed_time = 0.0;
156   SD_task_t task, dst;
157   SD_dependency_t dependency;
158   surf_action_t action;
159   SD_task_t *changed_tasks = NULL;
160   int changed_task_number = 0;
161   int changed_task_capacity = sd_global->task_number + 1;
162   int i;
163   static int first_time = 1;
164
165   SD_CHECK_INIT_DONE();
166
167   INFO0("Starting simulation...");
168
169   /* create the array that will be returned */
170   changed_tasks = xbt_new(SD_task_t, changed_task_capacity);
171   changed_tasks[0] = NULL;
172
173   if (first_time) {
174     surf_solve(); /* Takes traces into account. Returns 0.0 */
175     first_time = 0;
176   }
177
178   if(how_long>0) {
179     surf_timer_resource->extension_public->set(surf_get_clock()+how_long,
180                                                NULL,NULL);
181   }
182   sd_global->watch_point_reached = 0;
183
184   /* explore the ready tasks */
185   xbt_swag_foreach(task, sd_global->ready_task_set) {
186     INFO1("Executing task '%s'", SD_task_get_name(task));
187     if ((task->state_changed = __SD_task_try_to_run(task))) {
188       changed_tasks[changed_task_number++] = task; /* replace NULL by the task */
189       /*
190       if (changed_task_number == changed_task_capacity) {
191         changed_task_capacity *= 2;
192         changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
193       }
194       */
195       changed_tasks[changed_task_number] = NULL;
196     }
197   }
198
199   /* main loop */
200   elapsed_time = 0.0;
201   while (elapsed_time >= 0.0 &&
202          (how_long < 0.0 || total_time < how_long) &&
203          !sd_global->watch_point_reached) {
204     /* dumb variables */
205     void *fun = NULL;
206     void *arg = NULL;
207
208
209     DEBUG1("Total time: %f", total_time);
210
211     elapsed_time = surf_solve();
212     DEBUG1("surf_solve() returns %f", elapsed_time);
213     if (elapsed_time > 0.0)
214       total_time += elapsed_time;
215
216     /* let's see which tasks are done */
217     while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.done_action_set))) {
218       task = action->data;
219       INFO1("Task '%s' done", SD_task_get_name(task));
220       DEBUG0("Calling __SD_task_just_done");
221       __SD_task_just_done(task);
222       DEBUG1("__SD_task_just_done called on task '%s'", SD_task_get_name(task));
223
224       /* the state has changed */
225       if (!task->state_changed) {
226         task->state_changed = 1;
227         changed_tasks[changed_task_number++] = task;
228         /*
229         if (changed_task_number == changed_task_capacity) {
230           changed_task_capacity *= 2;
231           changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
232         }
233         */
234         changed_tasks[changed_task_number] = NULL;
235       }
236
237       /* remove the dependencies after this task */
238       while (xbt_dynar_length(task->tasks_after) > 0) {
239         xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
240         dst = dependency->dst;
241         SD_task_dependency_remove(task, dst);
242         
243         /* is dst ready now? */
244         if (__SD_task_is_ready(dst) && !sd_global->watch_point_reached) {
245           INFO1("Executing task '%s'", SD_task_get_name(dst));
246           if (__SD_task_try_to_run(dst)) {
247             changed_tasks[changed_task_number++] = dst;
248             /*
249             if (changed_task_number == changed_task_capacity) {
250               changed_task_capacity *= 2;
251               changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
252             }
253             */
254             changed_tasks[changed_task_number] = NULL;
255           }
256         }
257       }
258     }
259
260     /* let's see which tasks have just failed */
261     while ((action = xbt_swag_extract(surf_workstation_resource->common_public->states.failed_action_set))) {
262       task = action->data;
263       INFO1("Task '%s' failed", SD_task_get_name(task));
264       __SD_task_set_state(task, SD_FAILED);
265       surf_workstation_resource->common_public->action_free(action);
266       task->surf_action = NULL;
267
268       if (!task->state_changed) {
269         task->state_changed = 1;
270         changed_tasks[changed_task_number++] = task;
271         /*
272         if (changed_task_number == changed_task_capacity) {
273           changed_task_capacity *= 2;
274           changed_tasks = xbt_realloc(changed_tasks, sizeof(SD_task_t) * changed_task_capacity);
275         }
276         */
277         changed_tasks[changed_task_number] = NULL;
278       }
279     }
280
281     while (surf_timer_resource->extension_public->get(&fun,(void*)&arg)) {
282     }
283   }
284
285   /* we must reset every task->state_changed */
286   i = 0;
287   while (changed_tasks[i] != NULL) {
288     changed_tasks[i]->state_changed = 0;
289     i++;
290   }
291
292   INFO0("Simulation finished");
293   DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d", elapsed_time, total_time, sd_global->watch_point_reached);
294   DEBUG1("current time = %f", surf_get_clock());
295
296   return changed_tasks;
297 }
298
299 /**
300  * \brief Returns the current clock
301  *
302  * \return the current clock, in second
303  */
304 double SD_get_clock(void) {
305   SD_CHECK_INIT_DONE();
306
307   return surf_get_clock();
308 }
309
310 /**
311  * \brief Destroys all SD internal data
312  *
313  * This function should be called when the simulation is over. Don't forget also to destroy
314  * the tasks.
315  *
316  * \see SD_init(), SD_task_destroy()
317  */
318 void SD_exit(void) {
319   if (SD_INITIALISED()) {
320     DEBUG0("Destroying workstation and link dictionaries...");
321     xbt_dict_free(&sd_global->workstations);
322     xbt_dict_free(&sd_global->links);
323
324     DEBUG0("Destroying workstation and link arrays if necessary...");
325     if (sd_global->workstation_list != NULL)
326       xbt_free(sd_global->workstation_list);
327
328     if (sd_global->link_list != NULL)
329       xbt_free(sd_global->link_list);
330
331     if (sd_global->recyclable_route != NULL)
332       xbt_free(sd_global->recyclable_route);
333
334     DEBUG0("Destroying the swags...");
335     xbt_swag_free(sd_global->not_scheduled_task_set);
336     xbt_swag_free(sd_global->scheduled_task_set);
337     xbt_swag_free(sd_global->ready_task_set);
338     xbt_swag_free(sd_global->in_fifo_task_set);
339     xbt_swag_free(sd_global->running_task_set);
340     xbt_swag_free(sd_global->done_task_set);
341     xbt_swag_free(sd_global->failed_task_set);
342
343     xbt_free(sd_global);
344     sd_global = NULL;
345
346     DEBUG0("Exiting Surf...");
347     surf_exit();
348   }
349   else {
350     WARN0("SD_exit() called, but SimDag is not running");
351     /* we cannot use exceptions here because xbt is not running! */
352   }
353 }