Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bfcb6330f95c03af2db5636343e0f138f8cef0ad
[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(surf_model_resource_set(surf_workstation_model), cursor,
166                    name, surf_workstation) {
167     __SD_workstation_create(surf_workstation, NULL);
168   }
169
170   xbt_dict_foreach(link_set, cursor, name, surf_link) {
171     __SD_link_create(surf_link, NULL);
172   }
173
174   DEBUG2("Workstation number: %d, link number: %d",
175          SD_workstation_get_number(), SD_link_get_number());
176 }
177
178 /**
179  * \brief Launches the simulation.
180  *
181  * The function will execute the \ref SD_READY ready tasks.
182  * The simulation will be stopped when its time reaches \a how_long,
183  * when a watch point is reached, or when no more task can be executed.
184  * Then you can call SD_simulate() again.
185  *
186  * \param how_long maximum duration of the simulation (a negative value means no time limit)
187  * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
188  * \see SD_task_schedule(), SD_task_watch()
189  */
190 SD_task_t *SD_simulate(double how_long)
191 {
192   double total_time = 0.0;      /* we stop the simulation when total_time >= how_long */
193   double elapsed_time = 0.0;
194   SD_task_t task, task_safe, dst;
195   SD_dependency_t dependency;
196   surf_action_t action;
197   SD_task_t *res = NULL;
198   xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
199   unsigned int iter;
200   static int first_time = 1;
201
202   SD_CHECK_INIT_DONE();
203
204   INFO0("Starting simulation...");
205
206   if (first_time) {
207     surf_presolve();            /* Takes traces into account */
208     first_time = 0;
209   }
210
211   if (how_long > 0) {
212     surf_timer_model->extension.timer.set(surf_get_clock() + how_long,
213                                           NULL, NULL);
214   }
215   sd_global->watch_point_reached = 0;
216
217   /* explore the ready tasks */
218   xbt_swag_foreach_safe(task, task_safe, sd_global->ready_task_set) {
219     INFO1("Executing task '%s'", SD_task_get_name(task));
220     if (__SD_task_try_to_run(task) && !xbt_dynar_member(changed_tasks, &task))
221       xbt_dynar_push(changed_tasks, &task);
222   }
223
224   /* main loop */
225   elapsed_time = 0.0;
226   while (elapsed_time >= 0.0 &&
227          (how_long < 0.0 || total_time < how_long) &&
228          !sd_global->watch_point_reached) {
229     surf_model_t model = NULL;
230     /* dumb variables */
231     void *fun = NULL;
232     void *arg = NULL;
233
234
235     DEBUG1("Total time: %f", total_time);
236
237     elapsed_time = surf_solve();
238     DEBUG1("surf_solve() returns %f", elapsed_time);
239     if (elapsed_time > 0.0)
240       total_time += elapsed_time;
241
242     /* let's see which tasks are done */
243     xbt_dynar_foreach(model_list, iter, model) {
244       while ((action =
245               xbt_swag_extract(model->common_public.states.
246                                done_action_set))) {
247         task = action->data;
248         INFO1("Task '%s' done", SD_task_get_name(task));
249         DEBUG0("Calling __SD_task_just_done");
250         __SD_task_just_done(task);
251         DEBUG1("__SD_task_just_done called on task '%s'",
252                SD_task_get_name(task));
253
254         /* the state has changed */
255         if (!xbt_dynar_member(changed_tasks, &task))
256           xbt_dynar_push(changed_tasks, &task);
257
258         /* remove the dependencies after this task */
259         while (xbt_dynar_length(task->tasks_after) > 0) {
260           xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
261           dst = dependency->dst;
262           SD_task_dependency_remove(task, dst);
263
264           /* is dst ready now? */
265           if (__SD_task_is_ready(dst) && !sd_global->watch_point_reached) {
266             INFO1("Executing task '%s'", SD_task_get_name(dst));
267             if (__SD_task_try_to_run(dst) &&
268                 !xbt_dynar_member(changed_tasks, &task))
269               xbt_dynar_push(changed_tasks, &task);
270           }
271         }
272       }
273
274       /* let's see which tasks have just failed */
275       while ((action =
276               xbt_swag_extract(model->common_public.states.
277                                failed_action_set))) {
278         task = action->data;
279         INFO1("Task '%s' failed", SD_task_get_name(task));
280         __SD_task_set_state(task, SD_FAILED);
281         surf_workstation_model->common_public.action_free(action);
282         task->surf_action = NULL;
283
284         if (!xbt_dynar_member(changed_tasks, &task))
285           xbt_dynar_push(changed_tasks, &task);
286       }
287     }
288
289     while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
290     }
291   }
292
293   res = xbt_new0(SD_task_t, (xbt_dynar_length(changed_tasks) + 1));
294
295   xbt_dynar_foreach(changed_tasks, iter, task) {
296     res[iter] = task;
297   }
298   xbt_dynar_free(&changed_tasks);
299
300   INFO0("Simulation finished");
301   DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
302          elapsed_time, total_time, sd_global->watch_point_reached);
303   DEBUG1("current time = %f", surf_get_clock());
304
305   return res;
306 }
307
308 /**
309  * \brief Returns the current clock
310  *
311  * \return the current clock, in second
312  */
313 double SD_get_clock(void)
314 {
315   SD_CHECK_INIT_DONE();
316
317   return surf_get_clock();
318 }
319
320 /**
321  * \brief Destroys all SD internal data
322  *
323  * This function should be called when the simulation is over. Don't forget also to destroy
324  * the tasks.
325  *
326  * \see SD_init(), SD_task_destroy()
327  */
328 void SD_exit(void)
329 {
330   if (SD_INITIALISED()) {
331     DEBUG0("Destroying workstation and link dictionaries...");
332     xbt_dict_free(&sd_global->workstations);
333     xbt_dict_free(&sd_global->links);
334
335     DEBUG0("Destroying workstation and link arrays if necessary...");
336     if (sd_global->workstation_list != NULL)
337       xbt_free(sd_global->workstation_list);
338
339     if (sd_global->link_list != NULL)
340       xbt_free(sd_global->link_list);
341
342     if (sd_global->recyclable_route != NULL)
343       xbt_free(sd_global->recyclable_route);
344
345     DEBUG0("Destroying the swags...");
346     xbt_swag_free(sd_global->not_scheduled_task_set);
347     xbt_swag_free(sd_global->scheduled_task_set);
348     xbt_swag_free(sd_global->ready_task_set);
349     xbt_swag_free(sd_global->in_fifo_task_set);
350     xbt_swag_free(sd_global->running_task_set);
351     xbt_swag_free(sd_global->done_task_set);
352     xbt_swag_free(sd_global->failed_task_set);
353
354     xbt_free(sd_global);
355     sd_global = NULL;
356
357     DEBUG0("Exiting Surf...");
358     surf_exit();
359   } else {
360     WARN0("SD_exit() called, but SimDag is not running");
361     /* we cannot use exceptions here because xbt is not running! */
362   }
363 }