Logo AND Algorithmique Numérique Distribuée

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