Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a SD_workstation_get_current_task function
[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->scheduled_task_set =
62     xbt_swag_new(xbt_swag_offset(task, state_hookup));
63   sd_global->ready_task_set =
64     xbt_swag_new(xbt_swag_offset(task, state_hookup));
65   sd_global->in_fifo_task_set =
66     xbt_swag_new(xbt_swag_offset(task, state_hookup));
67   sd_global->running_task_set =
68     xbt_swag_new(xbt_swag_offset(task, state_hookup));
69   sd_global->done_task_set =
70     xbt_swag_new(xbt_swag_offset(task, state_hookup));
71   sd_global->failed_task_set =
72     xbt_swag_new(xbt_swag_offset(task, state_hookup));
73   sd_global->task_number = 0;
74
75   surf_init(argc, argv);
76   xbt_cfg_set_string(_surf_cfg_set, "workstation/model", "ptask_L07");
77 }
78
79 /**
80  * \brief Reinits the application part of the simulation (experimental feature)
81  *
82  * This function allows you to run several simulations on the same platform
83  * by resetting the part describing the application.
84  *
85  * @warning: this function is still experimental and not perfect. For example,
86  * the simulation clock (and traces usage) is not reset. So, do not use it if
87  * you use traces in your simulation, and do not use absolute timing after using it.
88  * That being said, this function is still precious if you want to compare a bunch of
89  * heuristics on the same platforms.
90  */
91 void SD_application_reinit(void)
92 {
93
94   s_SD_task_t task;
95
96   if (SD_INITIALISED()) {
97     DEBUG0("Recreating the swags...");
98     xbt_swag_free(sd_global->not_scheduled_task_set);
99     xbt_swag_free(sd_global->scheduled_task_set);
100     xbt_swag_free(sd_global->ready_task_set);
101     xbt_swag_free(sd_global->in_fifo_task_set);
102     xbt_swag_free(sd_global->running_task_set);
103     xbt_swag_free(sd_global->done_task_set);
104     xbt_swag_free(sd_global->failed_task_set);
105
106     sd_global->not_scheduled_task_set =
107       xbt_swag_new(xbt_swag_offset(task, state_hookup));
108     sd_global->scheduled_task_set =
109       xbt_swag_new(xbt_swag_offset(task, state_hookup));
110     sd_global->ready_task_set =
111       xbt_swag_new(xbt_swag_offset(task, state_hookup));
112     sd_global->in_fifo_task_set =
113       xbt_swag_new(xbt_swag_offset(task, state_hookup));
114     sd_global->running_task_set =
115       xbt_swag_new(xbt_swag_offset(task, state_hookup));
116     sd_global->done_task_set =
117       xbt_swag_new(xbt_swag_offset(task, state_hookup));
118     sd_global->failed_task_set =
119       xbt_swag_new(xbt_swag_offset(task, state_hookup));
120     sd_global->task_number = 0;
121   } else {
122     WARN0("SD_application_reinit called before initialization of SimDag");
123     /* we cannot use exceptions here because xbt is not running! */
124   }
125
126 }
127
128 /**
129  * \brief Creates the environment
130  *
131  * The environment (i.e. the \ref SD_workstation_management "workstations" and the
132  * \ref SD_link_management "links") is created with the data stored in the given XML
133  * platform file.
134  *
135  * \param platform_file name of an XML file describing the environment to create
136  * \see SD_workstation_management, SD_link_management
137  *
138  * The XML file follows this DTD:
139  *
140  *     \include simgrid.dtd
141  *
142  * Here is a small example of such a platform:
143  *
144  *     \include small_platform.xml
145  */
146 void SD_create_environment(const char *platform_file)
147 {
148   xbt_dict_cursor_t cursor = NULL;
149   char *name = NULL;
150   void *surf_workstation = NULL;
151   void *surf_link = NULL;
152
153   SD_CHECK_INIT_DONE();
154
155   DEBUG0("SD_create_environment");
156
157   surf_config_models_setup(platform_file);
158
159   parse_platform_file(platform_file);
160
161   /* now let's create the SD wrappers for workstations and links */
162   xbt_dict_foreach(surf_model_resource_set(surf_workstation_model), cursor,
163                    name, surf_workstation) {
164     __SD_workstation_create(surf_workstation, NULL);
165   }
166
167   xbt_dict_foreach(surf_model_resource_set(surf_network_model), cursor, name, surf_link) {
168     __SD_link_create(surf_link, NULL);
169   }
170
171   DEBUG2("Workstation number: %d, link number: %d",
172          SD_workstation_get_number(), SD_link_get_number());
173 }
174
175 /**
176  * \brief Launches the simulation.
177  *
178  * The function will execute the \ref SD_READY ready tasks.
179  * The simulation will be stopped when its time reaches \a how_long,
180  * when a watch point is reached, or when no more task can be executed.
181  * Then you can call SD_simulate() again.
182  *
183  * \param how_long maximum duration of the simulation (a negative value means no time limit)
184  * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
185  * \see SD_task_schedule(), SD_task_watch()
186  */
187 SD_task_t *SD_simulate(double how_long)
188 {
189   double total_time = 0.0;      /* we stop the simulation when total_time >= how_long */
190   double elapsed_time = 0.0;
191   SD_task_t task, task_safe, dst;
192   SD_dependency_t dependency;
193   surf_action_t action;
194   SD_task_t *res = NULL;
195   xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
196   unsigned int iter;
197   static int first_time = 1;
198
199   SD_CHECK_INIT_DONE();
200
201   INFO0("Starting simulation...");
202
203   if (first_time) {
204     surf_presolve();            /* Takes traces into account */
205     first_time = 0;
206   }
207
208   if (how_long > 0) {
209     surf_timer_model->extension.timer.set(surf_get_clock() + how_long,
210                                           NULL, NULL);
211   }
212   sd_global->watch_point_reached = 0;
213
214   /* explore the ready tasks */
215   xbt_swag_foreach_safe(task, task_safe, sd_global->ready_task_set) {
216     INFO1("Executing task '%s'", SD_task_get_name(task));
217     if (__SD_task_try_to_run(task) && !xbt_dynar_member(changed_tasks, &task))
218       xbt_dynar_push(changed_tasks, &task);
219   }
220
221   /* main loop */
222   elapsed_time = 0.0;
223   while (elapsed_time >= 0.0 &&
224          (how_long < 0.0 || total_time < how_long) &&
225          !sd_global->watch_point_reached) {
226     surf_model_t model = NULL;
227     /* dumb variables */
228     void *fun = NULL;
229     void *arg = NULL;
230
231
232     DEBUG1("Total time: %f", total_time);
233
234     elapsed_time = surf_solve();
235     DEBUG1("surf_solve() returns %f", elapsed_time);
236     if (elapsed_time > 0.0)
237       total_time += elapsed_time;
238
239     /* let's see which tasks are done */
240     xbt_dynar_foreach(model_list, iter, model) {
241       while ((action = xbt_swag_extract(model->states.done_action_set))) {
242         task = action->data;
243         task->start_time = surf_workstation_model->action_get_start_time(task->surf_action);
244         task->finish_time = surf_get_clock();
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         task->start_time = surf_workstation_model->action_get_start_time(task->surf_action);
275         task->finish_time = surf_get_clock();
276         INFO1("Task '%s' failed", SD_task_get_name(task));
277         __SD_task_set_state(task, SD_FAILED);
278         surf_workstation_model->action_unref(action);
279         task->surf_action = NULL;
280
281         if (!xbt_dynar_member(changed_tasks, &task))
282           xbt_dynar_push(changed_tasks, &task);
283       }
284     }
285
286     while (surf_timer_model->extension.timer.get(&fun, (void *) &arg)) {
287     }
288   }
289
290   res = xbt_new0(SD_task_t, (xbt_dynar_length(changed_tasks) + 1));
291
292   xbt_dynar_foreach(changed_tasks, iter, task) {
293     res[iter] = task;
294   }
295   xbt_dynar_free(&changed_tasks);
296
297   INFO0("Simulation finished");
298   DEBUG3("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
299          elapsed_time, total_time, sd_global->watch_point_reached);
300   DEBUG1("current time = %f", surf_get_clock());
301
302   return res;
303 }
304
305 /**
306  * \brief Returns the current clock
307  *
308  * \return the current clock, in second
309  */
310 double SD_get_clock(void)
311 {
312   SD_CHECK_INIT_DONE();
313
314   return surf_get_clock();
315 }
316
317 /**
318  * \brief Destroys all SD internal data
319  *
320  * This function should be called when the simulation is over. Don't forget also to destroy
321  * the tasks.
322  *
323  * \see SD_init(), SD_task_destroy()
324  */
325 void SD_exit(void)
326 {
327   if (SD_INITIALISED()) {
328     DEBUG0("Destroying workstation and link dictionaries...");
329     xbt_dict_free(&sd_global->workstations);
330     xbt_dict_free(&sd_global->links);
331
332     DEBUG0("Destroying workstation and link arrays if necessary...");
333     if (sd_global->workstation_list != NULL)
334       xbt_free(sd_global->workstation_list);
335
336     if (sd_global->link_list != NULL)
337       xbt_free(sd_global->link_list);
338
339     if (sd_global->recyclable_route != NULL)
340       xbt_free(sd_global->recyclable_route);
341
342     DEBUG0("Destroying the swags...");
343     xbt_swag_free(sd_global->not_scheduled_task_set);
344     xbt_swag_free(sd_global->scheduled_task_set);
345     xbt_swag_free(sd_global->ready_task_set);
346     xbt_swag_free(sd_global->in_fifo_task_set);
347     xbt_swag_free(sd_global->running_task_set);
348     xbt_swag_free(sd_global->done_task_set);
349     xbt_swag_free(sd_global->failed_task_set);
350
351     xbt_free(sd_global);
352     sd_global = NULL;
353
354     DEBUG0("Exiting Surf...");
355     surf_exit();
356   } else {
357     WARN0("SD_exit() called, but SimDag is not running");
358     /* we cannot use exceptions here because xbt is not running! */
359   }
360 }