Logo AND Algorithmique Numérique Distribuée

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