Logo AND Algorithmique Numérique Distribuée

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