Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2481b2c3f59f21b7d016df36a6b5bad7edaf93c6
[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 #include "instr/instr_private.h"
16 #include "surf/surfxml_parse.h"
17 #ifdef HAVE_LUA
18 #include <lua.h>
19 #include <lauxlib.h>
20 #include <lualib.h>
21 #endif
22
23 #ifdef HAVE_JEDULE
24 #include "instr/jedule/jedule_sd_binding.h"
25 #endif
26
27 XBT_LOG_NEW_CATEGORY(sd, "Logging specific to SimDag");
28 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_kernel, sd,
29                                 "Logging specific to SimDag (kernel)");
30
31 SD_global_t sd_global = NULL;
32
33 XBT_LOG_EXTERNAL_CATEGORY(sd_kernel);
34 XBT_LOG_EXTERNAL_CATEGORY(sd_task);
35 XBT_LOG_EXTERNAL_CATEGORY(sd_workstation);
36
37 /**
38  * \brief Initialises SD internal data
39  *
40  * This function must be called before any other SD function. Then you
41  * should call SD_create_environment().
42  *
43  * \param argc argument number
44  * \param argv argument list
45  * \see SD_create_environment(), SD_exit()
46  */
47 void SD_init(int *argc, char **argv)
48 {
49 #ifdef HAVE_TRACING
50   TRACE_global_init(argc, argv);
51 #endif
52
53   s_SD_task_t task;
54
55   xbt_assert0(!SD_INITIALISED(), "SD_init() already called");
56
57   /* Connect our log channels: that must be done manually under windows */
58   XBT_LOG_CONNECT(sd_kernel, sd);
59   XBT_LOG_CONNECT(sd_task, sd);
60   XBT_LOG_CONNECT(sd_workstation, sd);
61
62
63   sd_global = xbt_new(s_SD_global_t, 1);
64   sd_global->workstations = xbt_dict_new();
65   sd_global->workstation_count = 0;
66   sd_global->workstation_list = NULL;
67   sd_global->links = xbt_dict_new();
68   sd_global->link_count = 0;
69   sd_global->link_list = NULL;
70   sd_global->recyclable_route = NULL;
71   sd_global->watch_point_reached = 0;
72
73   sd_global->not_scheduled_task_set =
74       xbt_swag_new(xbt_swag_offset(task, state_hookup));
75   sd_global->schedulable_task_set =
76       xbt_swag_new(xbt_swag_offset(task, state_hookup));
77   sd_global->scheduled_task_set =
78       xbt_swag_new(xbt_swag_offset(task, state_hookup));
79   sd_global->runnable_task_set =
80       xbt_swag_new(xbt_swag_offset(task, state_hookup));
81   sd_global->in_fifo_task_set =
82       xbt_swag_new(xbt_swag_offset(task, state_hookup));
83   sd_global->running_task_set =
84       xbt_swag_new(xbt_swag_offset(task, state_hookup));
85   sd_global->done_task_set =
86       xbt_swag_new(xbt_swag_offset(task, state_hookup));
87   sd_global->failed_task_set =
88       xbt_swag_new(xbt_swag_offset(task, state_hookup));
89   sd_global->task_number = 0;
90
91   surf_init(argc, argv);
92
93   xbt_cfg_setdefault_string(_surf_cfg_set, "workstation/model",
94                             "ptask_L07");
95
96 #ifdef HAVE_TRACING
97   TRACE_start ();
98 #endif
99
100 #ifdef HAVE_JEDULE
101   jedule_sd_init();
102 #endif
103
104   XBT_DEBUG("ADD SD LEVELS");
105   SD_HOST_LEVEL = xbt_lib_add_level(host_lib,free);
106   SD_LINK_LEVEL = xbt_lib_add_level(link_lib,free);
107 }
108
109 /**
110  * \brief Reinits the application part of the simulation (experimental feature)
111  *
112  * This function allows you to run several simulations on the same platform
113  * by resetting the part describing the application.
114  *
115  * @warning: this function is still experimental and not perfect. For example,
116  * the simulation clock (and traces usage) is not reset. So, do not use it if
117  * you use traces in your simulation, and do not use absolute timing after using it.
118  * That being said, this function is still precious if you want to compare a bunch of
119  * heuristics on the same platforms.
120  */
121 void SD_application_reinit(void)
122 {
123
124   s_SD_task_t task;
125
126   if (SD_INITIALISED()) {
127     XBT_DEBUG("Recreating the swags...");
128     xbt_swag_free(sd_global->not_scheduled_task_set);
129     xbt_swag_free(sd_global->schedulable_task_set);
130     xbt_swag_free(sd_global->scheduled_task_set);
131     xbt_swag_free(sd_global->runnable_task_set);
132     xbt_swag_free(sd_global->in_fifo_task_set);
133     xbt_swag_free(sd_global->running_task_set);
134     xbt_swag_free(sd_global->done_task_set);
135     xbt_swag_free(sd_global->failed_task_set);
136
137     sd_global->not_scheduled_task_set =
138         xbt_swag_new(xbt_swag_offset(task, state_hookup));
139     sd_global->schedulable_task_set =
140         xbt_swag_new(xbt_swag_offset(task, state_hookup));
141     sd_global->scheduled_task_set =
142         xbt_swag_new(xbt_swag_offset(task, state_hookup));
143     sd_global->runnable_task_set =
144         xbt_swag_new(xbt_swag_offset(task, state_hookup));
145     sd_global->in_fifo_task_set =
146         xbt_swag_new(xbt_swag_offset(task, state_hookup));
147     sd_global->running_task_set =
148         xbt_swag_new(xbt_swag_offset(task, state_hookup));
149     sd_global->done_task_set =
150         xbt_swag_new(xbt_swag_offset(task, state_hookup));
151     sd_global->failed_task_set =
152         xbt_swag_new(xbt_swag_offset(task, state_hookup));
153     sd_global->task_number = 0;
154
155 #ifdef HAVE_JEDULE
156     jedule_sd_cleanup();
157     jedule_sd_init();
158 #endif
159
160   } else {
161     XBT_WARN("SD_application_reinit called before initialization of SimDag");
162     /* we cannot use exceptions here because xbt is not running! */
163   }
164
165 }
166
167 /**
168  * \brief Creates the environment
169  *
170  * The environment (i.e. the \ref SD_workstation_management "workstations" and the
171  * \ref SD_link_management "links") is created with the data stored in the given XML
172  * platform file.
173  *
174  * \param platform_file name of an XML file describing the environment to create
175  * \see SD_workstation_management, SD_link_management
176  *
177  * The XML file follows this DTD:
178  *
179  *     \include simgrid.dtd
180  *
181  * Here is a small example of such a platform:
182  *
183  *     \include small_platform.xml
184  */
185 void SD_create_environment(const char *platform_file)
186 {
187   xbt_dict_cursor_t cursor = NULL;
188   char *name = NULL;
189   void *surf_workstation = NULL;
190   void *surf_link = NULL;
191
192   platform_filename = bprintf("%s",platform_file);
193
194   // Reset callbacks
195   surf_parse_reset_callbacks();
196   // Add config callbacks
197   surf_parse_add_callback_config();
198   SD_CHECK_INIT_DONE();
199   parse_platform_file(platform_file);
200   surf_config_models_create_elms();
201
202   /* now let's create the SD wrappers for workstations and links */
203   xbt_dict_foreach(surf_model_resource_set(surf_workstation_model), cursor,
204                    name, surf_workstation) {
205     __SD_workstation_create(surf_workstation, NULL);
206   }
207
208   xbt_dict_foreach(surf_model_resource_set(surf_network_model), cursor,
209                    name, surf_link) {
210     __SD_link_create(surf_link, NULL);
211   }
212
213   XBT_DEBUG("Workstation number: %d, link number: %d",
214          SD_workstation_get_number(), SD_link_get_number());
215 #ifdef HAVE_JEDULE
216   jedule_setup_platform();
217 #endif
218 }
219
220 /**
221  * \brief Launches the simulation.
222  *
223  * The function will execute the \ref SD_RUNNABLE runnable tasks.
224  * If \a how_long is positive, then the simulation will be stopped either
225  * when time reaches \a how_long or when a watch point is reached.
226  * A nonpositive value for \a how_long means no time limit, in which case
227  * the simulation will be stopped either when a watch point is reached or
228  * when no more task can be executed.
229  * Then you can call SD_simulate() again.
230  *
231  * \param how_long maximum duration of the simulation (a negative value means no time limit)
232  * \return a NULL-terminated array of \ref SD_task_t whose state has changed.
233  * \see SD_task_schedule(), SD_task_watch()
234  */
235 xbt_dynar_t SD_simulate(double how_long)
236 {
237   double total_time = 0.0;      /* we stop the simulation when total_time >= how_long */
238   double elapsed_time = 0.0;
239   SD_task_t task, task_safe, dst;
240   SD_dependency_t dependency;
241   surf_action_t action;
242   xbt_dynar_t changed_tasks = xbt_dynar_new(sizeof(SD_task_t), NULL);
243   unsigned int iter, depcnt;
244   static int first_time = 1;
245
246   SD_CHECK_INIT_DONE();
247
248    if (first_time) {
249     XBT_VERB("Starting simulation...");
250
251     surf_presolve();            /* Takes traces into account */
252     first_time = 0;
253   }
254
255   sd_global->watch_point_reached = 0;
256
257   /* explore the runnable tasks */
258   xbt_swag_foreach_safe(task, task_safe, sd_global->runnable_task_set) {
259     XBT_VERB("Executing task '%s'", SD_task_get_name(task));
260     if (__SD_task_try_to_run(task)
261         && !xbt_dynar_member(changed_tasks, &task))
262       xbt_dynar_push(changed_tasks, &task);
263   }
264
265   /* main loop */
266   elapsed_time = 0.0;
267   while (elapsed_time >= 0.0 &&
268          (how_long < 0.0 || total_time < how_long) &&
269          !sd_global->watch_point_reached) {
270     surf_model_t model = NULL;
271     /* dumb variables */
272
273
274     XBT_DEBUG("Total time: %f", total_time);
275
276     elapsed_time = surf_solve(how_long > 0 ? surf_get_clock() + how_long : -1.0);
277     XBT_DEBUG("surf_solve() returns %f", elapsed_time);
278     if (elapsed_time > 0.0)
279       total_time += elapsed_time;
280
281     /* let's see which tasks are done */
282     xbt_dynar_foreach(model_list, iter, model) {
283       while ((action = xbt_swag_extract(model->states.done_action_set))) {
284         task = action->data;
285         task->start_time =
286             surf_workstation_model->
287             action_get_start_time(task->surf_action);
288         task->finish_time = surf_get_clock();
289         XBT_VERB("Task '%s' done", SD_task_get_name(task));
290         XBT_DEBUG("Calling __SD_task_just_done");
291         __SD_task_just_done(task);
292         XBT_DEBUG("__SD_task_just_done called on task '%s'",
293                SD_task_get_name(task));
294
295         /* the state has changed */
296         if (!xbt_dynar_member(changed_tasks, &task))
297           xbt_dynar_push(changed_tasks, &task);
298
299         /* remove the dependencies after this task */
300         xbt_dynar_foreach(task->tasks_after, depcnt, dependency) {
301           dst = dependency->dst;
302           if (dst->unsatisfied_dependencies > 0)
303             dst->unsatisfied_dependencies--;
304           if (dst->is_not_ready > 0)
305             dst->is_not_ready--;
306
307           if (!(dst->unsatisfied_dependencies)) {
308             if (__SD_task_is_scheduled(dst))
309               __SD_task_set_state(dst, SD_RUNNABLE);
310             else
311               __SD_task_set_state(dst, SD_SCHEDULABLE);
312           }
313
314           if (SD_task_get_kind(dst) == SD_TASK_COMM_E2E) {
315             SD_dependency_t comm_dep;
316             SD_task_t comm_dst;
317             xbt_dynar_get_cpy(dst->tasks_after, 0, &comm_dep);
318             comm_dst = comm_dep->dst;
319             if (__SD_task_is_not_scheduled(comm_dst) &&
320                 comm_dst->is_not_ready > 0) {
321               comm_dst->is_not_ready--;
322
323               if (!(comm_dst->is_not_ready)) {
324                 __SD_task_set_state(comm_dst, SD_SCHEDULABLE);
325               }
326             }
327           }
328
329           /* is dst runnable now? */
330           if (__SD_task_is_runnable(dst)
331               && !sd_global->watch_point_reached) {
332             XBT_VERB("Executing task '%s'", SD_task_get_name(dst));
333             if (__SD_task_try_to_run(dst) &&
334                 !xbt_dynar_member(changed_tasks, &task))
335               xbt_dynar_push(changed_tasks, &task);
336           }
337         }
338       }
339
340       /* let's see which tasks have just failed */
341       while ((action = xbt_swag_extract(model->states.failed_action_set))) {
342         task = action->data;
343         task->start_time =
344             surf_workstation_model->
345             action_get_start_time(task->surf_action);
346         task->finish_time = surf_get_clock();
347         XBT_VERB("Task '%s' failed", SD_task_get_name(task));
348         __SD_task_set_state(task, SD_FAILED);
349         surf_workstation_model->action_unref(action);
350         task->surf_action = NULL;
351
352         if (!xbt_dynar_member(changed_tasks, &task))
353           xbt_dynar_push(changed_tasks, &task);
354       }
355     }
356   }
357
358   if (!sd_global->watch_point_reached && how_long<0){
359     if (xbt_swag_size(sd_global->done_task_set) < sd_global->task_number){
360         XBT_WARN("Simulation is finished but some tasks are still not done");
361         xbt_swag_foreach_safe (task, task_safe,sd_global->not_scheduled_task_set){
362                 XBT_WARN("%s is in SD_NOT_SCHEDULED state", SD_task_get_name(task));
363                 }
364         xbt_swag_foreach_safe (task, task_safe,sd_global->schedulable_task_set){
365                 XBT_WARN("%s is in SD_SCHEDULABLE state", SD_task_get_name(task));
366         }
367         xbt_swag_foreach_safe (task, task_safe,sd_global->scheduled_task_set){
368                 XBT_WARN("%s is in SD_SCHEDULED state", SD_task_get_name(task));
369         }
370     }
371   }
372
373   XBT_DEBUG("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
374          elapsed_time, total_time, sd_global->watch_point_reached);
375   XBT_DEBUG("current time = %f", surf_get_clock());
376
377   return changed_tasks;
378 }
379
380 /**
381  * \brief Returns the current clock
382  *
383  * \return the current clock, in second
384  */
385 double SD_get_clock(void)
386 {
387   SD_CHECK_INIT_DONE();
388
389   return surf_get_clock();
390 }
391
392 /**
393  * \brief Destroys all SD internal data
394  *
395  * This function should be called when the simulation is over. Don't forget also to destroy
396  * the tasks.
397  *
398  * \see SD_init(), SD_task_destroy()
399  */
400 void SD_exit(void)
401 {
402 #ifdef HAVE_TRACING
403   TRACE_surf_release();
404 #endif
405   if (SD_INITIALISED()) {
406     XBT_DEBUG("Destroying workstation and link dictionaries...");
407     xbt_dict_free(&sd_global->workstations);
408     xbt_dict_free(&sd_global->links);
409
410     XBT_DEBUG("Destroying workstation and link arrays if necessary...");
411     if (sd_global->workstation_list != NULL)
412       xbt_free(sd_global->workstation_list);
413
414     if (sd_global->link_list != NULL)
415       xbt_free(sd_global->link_list);
416
417     if (sd_global->recyclable_route != NULL)
418       xbt_free(sd_global->recyclable_route);
419
420     XBT_DEBUG("Destroying the swags...");
421     xbt_swag_free(sd_global->not_scheduled_task_set);
422     xbt_swag_free(sd_global->schedulable_task_set);
423     xbt_swag_free(sd_global->scheduled_task_set);
424     xbt_swag_free(sd_global->runnable_task_set);
425     xbt_swag_free(sd_global->in_fifo_task_set);
426     xbt_swag_free(sd_global->running_task_set);
427     xbt_swag_free(sd_global->done_task_set);
428     xbt_swag_free(sd_global->failed_task_set);
429
430     xbt_free(sd_global);
431     sd_global = NULL;
432
433 #ifdef HAVE_TRACING
434   TRACE_end();
435 #endif
436 #ifdef HAVE_JEDULE
437   jedule_sd_dump();
438   jedule_sd_cleanup();
439 #endif
440
441     XBT_DEBUG("Exiting Surf...");
442     surf_exit();
443   } else {
444     XBT_WARN("SD_exit() called, but SimDag is not running");
445     /* we cannot use exceptions here because xbt is not running! */
446   }
447 }
448
449 /**
450  * \bried load script file
451  */
452
453 void SD_load_environment_script(const char *script_file)
454 {
455 #ifdef HAVE_LUA
456   lua_State *L = lua_open();
457   luaL_openlibs(L);
458
459   if (luaL_loadfile(L, script_file) || lua_pcall(L, 0, 0, 0)) {
460     printf("error: %s\n", lua_tostring(L, -1));
461     return;
462   }
463 #else
464   xbt_die
465       ("Lua is not available!! to call SD_load_environment_script, lua should be available...");
466 #endif
467   return;
468 }