Logo AND Algorithmique Numérique Distribuée

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