Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fixed the pthread context implementation, now it compiles and runs
[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_config_models_setup(platform_file);
160
161   parse_platform_file(platform_file);
162
163   /* now let's create the SD wrappers for workstations and links */
164   xbt_dict_foreach(surf_model_resource_set(surf_workstation_model), cursor,
165                    name, surf_workstation) {
166     __SD_workstation_create(surf_workstation, NULL);
167   }
168
169   xbt_dict_foreach(surf_model_resource_set(surf_network_model), 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.timer.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 = xbt_swag_extract(model->states.done_action_set))) {
244         task = action->data;
245         INFO1("Task '%s' done", SD_task_get_name(task));
246         DEBUG0("Calling __SD_task_just_done");
247         __SD_task_just_done(task);
248         DEBUG1("__SD_task_just_done called on task '%s'",
249                SD_task_get_name(task));
250
251         /* the state has changed */
252         if (!xbt_dynar_member(changed_tasks, &task))
253           xbt_dynar_push(changed_tasks, &task);
254
255         /* remove the dependencies after this task */
256         while (xbt_dynar_length(task->tasks_after) > 0) {
257           xbt_dynar_get_cpy(task->tasks_after, 0, &dependency);
258           dst = dependency->dst;
259           SD_task_dependency_remove(task, dst);
260
261           /* is dst ready now? */
262           if (__SD_task_is_ready(dst) && !sd_global->watch_point_reached) {
263             INFO1("Executing task '%s'", SD_task_get_name(dst));
264             if (__SD_task_try_to_run(dst) &&
265                 !xbt_dynar_member(changed_tasks, &task))
266               xbt_dynar_push(changed_tasks, &task);
267           }
268         }
269       }
270
271       /* let's see which tasks have just failed */
272       while ((action = xbt_swag_extract(model->states.failed_action_set))) {
273         task = action->data;
274         INFO1("Task '%s' failed", SD_task_get_name(task));
275         __SD_task_set_state(task, SD_FAILED);
276         surf_workstation_model->action_unref(action);
277         task->surf_action = NULL;
278
279         if (!xbt_dynar_member(changed_tasks, &task))
280           xbt_dynar_push(changed_tasks, &task);
281       }
282     }
283
284     while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
285     }
286   }
287
288   res = xbt_new0(SD_task_t, (xbt_dynar_length(changed_tasks) + 1));
289
290   xbt_dynar_foreach(changed_tasks, iter, task) {
291     res[iter] = task;
292   }
293   xbt_dynar_free(&changed_tasks);
294
295   INFO0("Simulation finished");
296   DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
297          elapsed_time, total_time, sd_global->watch_point_reached);
298   DEBUG1("current time = %f", surf_get_clock());
299
300   return res;
301 }
302
303 /**
304  * \brief Returns the current clock
305  *
306  * \return the current clock, in second
307  */
308 double SD_get_clock(void)
309 {
310   SD_CHECK_INIT_DONE();
311
312   return surf_get_clock();
313 }
314
315 /**
316  * \brief Destroys all SD internal data
317  *
318  * This function should be called when the simulation is over. Don't forget also to destroy
319  * the tasks.
320  *
321  * \see SD_init(), SD_task_destroy()
322  */
323 void SD_exit(void)
324 {
325   if (SD_INITIALISED()) {
326     DEBUG0("Destroying workstation and link dictionaries...");
327     xbt_dict_free(&sd_global->workstations);
328     xbt_dict_free(&sd_global->links);
329
330     DEBUG0("Destroying workstation and link arrays if necessary...");
331     if (sd_global->workstation_list != NULL)
332       xbt_free(sd_global->workstation_list);
333
334     if (sd_global->link_list != NULL)
335       xbt_free(sd_global->link_list);
336
337     if (sd_global->recyclable_route != NULL)
338       xbt_free(sd_global->recyclable_route);
339
340     DEBUG0("Destroying the swags...");
341     xbt_swag_free(sd_global->not_scheduled_task_set);
342     xbt_swag_free(sd_global->scheduled_task_set);
343     xbt_swag_free(sd_global->ready_task_set);
344     xbt_swag_free(sd_global->in_fifo_task_set);
345     xbt_swag_free(sd_global->running_task_set);
346     xbt_swag_free(sd_global->done_task_set);
347     xbt_swag_free(sd_global->failed_task_set);
348
349     xbt_free(sd_global);
350     sd_global = NULL;
351
352     DEBUG0("Exiting Surf...");
353     surf_exit();
354   } else {
355     WARN0("SD_exit() called, but SimDag is not running");
356     /* we cannot use exceptions here because xbt is not running! */
357   }
358 }