Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
no need to include lua stuff here?
[simgrid.git] / src / simdag / sd_global.cpp
1 /* Copyright (c) 2006-2016. 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 "src/surf/surf_interface.hpp"
8 #include "src/simdag/simdag_private.h"
9 #include "instr/instr_interface.h"
10 #include "xbt/sysdep.h"
11 #include "xbt/dynar.h"
12 #include "surf/surf.h"
13 #include "simgrid/sg_config.h"
14 #include "simgrid/host.h"
15 #include "xbt/ex.h"
16 #include "xbt/log.h"
17 #include "xbt/str.h"
18 #include "xbt/config.h"
19 #include "surf/surfxml_parse.h"
20
21 #ifdef HAVE_JEDULE
22 #include "simgrid/jedule/jedule_sd_binding.h"
23 #endif
24
25 XBT_LOG_NEW_CATEGORY(sd, "Logging specific to SimDag");
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_kernel, sd,
27                                 "Logging specific to SimDag (kernel)");
28
29 SD_global_t sd_global = NULL;
30
31 /**
32  * \brief Initializes 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   TRACE_global_init(argc, argv);
44
45   xbt_assert(sd_global == NULL, "SD_init() already called");
46
47   sd_global = xbt_new(s_SD_global_t, 1);
48   sd_global->watch_point_reached = 0;
49
50   sd_global->task_mallocator=xbt_mallocator_new(65536, SD_task_new_f,
51                                                 SD_task_free_f,
52                                                 SD_task_recycle_f);
53
54   sd_global->initial_task_set = xbt_dynar_new(sizeof(SD_task_t), NULL);
55   sd_global->executable_task_set = xbt_dynar_new(sizeof(SD_task_t), NULL);
56   sd_global->completed_task_set = xbt_dynar_new(sizeof(SD_task_t), NULL);
57   sd_global->return_set = xbt_dynar_new(sizeof(SD_task_t), NULL);
58
59   sd_global->task_number = 0;
60
61   surf_init(argc, argv);
62
63   xbt_cfg_setdefault_string(_sg_cfg_set, "host/model",
64                             "ptask_L07");
65
66 #ifdef HAVE_JEDULE
67   jedule_sd_init();
68 #endif
69
70   if (_sg_cfg_exit_asap) {
71     SD_exit();
72     exit(0);
73   }
74 }
75
76 /** \brief set a configuration variable
77  *
78  * Do --help on any simgrid binary to see the list of currently existing
79  * configuration variables, and see Section @ref options.
80  *
81  * Example:
82  * SD_config("host/model","default");
83  */
84 void SD_config(const char *key, const char *value){
85   xbt_assert(sd_global,"ERROR: Please call SD_init() before using SD_config()");
86   xbt_cfg_set_as_string(_sg_cfg_set, key, value);
87 }
88
89 /**
90  * \brief Reinits the application part of the simulation (experimental feature)
91  *
92  * This function allows you to run several simulations on the same platform
93  * by resetting the part describing the application.
94  *
95  * @warning: this function is still experimental and not perfect. For example,
96  * the simulation clock (and traces usage) is not reset. So, do not use it if
97  * you use traces in your simulation, and do not use absolute timing after
98  * using it.
99  * That being said, this function is still precious if you want to compare a
100  * bunch of heuristics on the same platforms.
101  */
102 void SD_application_reinit(void)
103 {
104
105 //  s_SD_task_t task;
106
107 //  SD_task_t done_task, next_done_task;
108   xbt_die("This function is not working since the C++ links and others. Please report the problem if you really need that function.");
109
110    XBT_DEBUG("Recreating the swags...");
111
112 //  xbt_swag_foreach_safe(done_task, next_done_task, sd_global->done_task_set){
113 //    if (xbt_dynar_is_empty(done_task->tasks_before)){
114 //      __SD_task_set_state(done_task, SD_SCHEDULABLE);
115 //    } else{
116 //      __SD_task_set_state(done_task, SD_NOT_SCHEDULED);
117 //      done_task->unsatisfied_dependencies =
118 //        xbt_dynar_length(done_task->tasks_before);
119 //      done_task->is_not_ready = done_task->unsatisfied_dependencies;
120 //    }
121 //    free(done_task->workstation_list);
122 //    done_task->workstation_list = NULL;
123 //    done_task->workstation_nb = 0;
124 //  }
125
126   sd_global->task_number = 0;
127
128
129 #ifdef HAVE_JEDULE
130   jedule_sd_cleanup();
131   jedule_sd_init();
132 #endif
133 }
134
135 /**
136  * \brief Creates the environment
137  *
138  * The environment (i.e. the \ref sg_host_management "hosts"
139  * and the \ref SD_link_management "links") is created with the data stored
140  * in the given XML platform file.
141  *
142  * \param platform_file name of an XML file describing the environment to create
143  * \see sg_host_management, SD_link_management
144  *
145  * The XML file follows this DTD:
146  *
147  *     \include simgrid.dtd
148  *
149  * Here is a small example of such a platform:
150  *
151  *     \include small_platform.xml
152  */
153 void SD_create_environment(const char *platform_file)
154 {
155   parse_platform_file(platform_file);
156
157   XBT_DEBUG("Workstation number: %zu, link number: %d",
158          sg_host_count(), sg_link_count());
159 #ifdef HAVE_JEDULE
160   jedule_setup_platform();
161 #endif
162 }
163
164 /**
165  * \brief Launches the simulation.
166  *
167  * The function will execute the \ref SD_RUNNABLE runnable tasks.
168  * If \a how_long is positive, then the simulation will be stopped either
169  * when time reaches \a how_long or when a watch point is reached.
170  * A non-positive value for \a how_long means no time limit, in which case
171  * the simulation will be stopped either when a watch point is reached or
172  * when no more task can be executed.
173  * Then you can call SD_simulate() again.
174  *
175  * \param how_long maximum duration of the simulation (a negative value means
176  * no time limit)
177  * \return a dynar of \ref SD_task_t whose state has changed.
178  * \see SD_task_schedule(), SD_task_watch()
179  */
180
181 xbt_dynar_t SD_simulate(double how_long) {
182   /* we stop the simulation when total_time >= how_long */
183   double total_time = 0.0;
184   double elapsed_time = 0.0;
185   SD_task_t task, dst;
186   SD_dependency_t dependency;
187   surf_action_t action;
188   unsigned int iter, depcnt;
189   static int first_time = 1;
190
191   if (first_time) {
192     XBT_VERB("Starting simulation...");
193
194     surf_presolve();            /* Takes traces into account */
195     first_time = 0;
196   }
197
198   XBT_VERB("Run simulation for %f seconds", how_long);
199   sd_global->watch_point_reached = 0;
200
201   xbt_dynar_reset(sd_global->return_set);
202
203   /* explore the runnable tasks */
204   xbt_dynar_foreach(sd_global->executable_task_set , iter, task) {
205     XBT_VERB("Executing task '%s'", SD_task_get_name(task));
206     SD_task_run(task);
207     xbt_dynar_push(sd_global->return_set, &task);
208     iter--;
209   }
210
211   /* main loop */
212   elapsed_time = 0.0;
213   while (elapsed_time >= 0.0 &&
214          (how_long < 0.0 || 0.00001 < (how_long -total_time)) &&
215          !sd_global->watch_point_reached) {
216     surf_model_t model = NULL;
217     /* dumb variables */
218
219
220     XBT_DEBUG("Total time: %f", total_time);
221
222     elapsed_time = surf_solve(how_long > 0 ? surf_get_clock() + how_long - total_time: -1.0);
223     XBT_DEBUG("surf_solve() returns %f", elapsed_time);
224     if (elapsed_time > 0.0)
225       total_time += elapsed_time;
226
227     /* let's see which tasks are done */
228     xbt_dynar_foreach(all_existing_models, iter, model) {
229       while ((action = surf_model_extract_done_action_set(model))) {
230         task = (SD_task_t) action->getData();
231         task->start_time = task->surf_action->getStartTime();
232
233         task->finish_time = surf_get_clock();
234         XBT_VERB("Task '%s' done", SD_task_get_name(task));
235         SD_task_set_state(task, SD_DONE);
236         task->surf_action->unref();
237         task->surf_action = NULL;
238
239         /* the state has changed. Add it only if it's the first change */
240         if (xbt_dynar_search_or_negative(sd_global->return_set, &task) < 0) {
241           xbt_dynar_push(sd_global->return_set, &task);
242         }
243
244         /* remove the dependencies after this task */
245         xbt_dynar_foreach(task->tasks_after, depcnt, dependency) {
246           dst = dependency->dst;
247           if (dst->unsatisfied_dependencies > 0)
248             dst->unsatisfied_dependencies--;
249           if (dst->is_not_ready > 0)
250             dst->is_not_ready--;
251
252           XBT_DEBUG("Released a dependency on %s: %d remain(s). Became schedulable if %d=0",
253              SD_task_get_name(dst), dst->unsatisfied_dependencies,
254              dst->is_not_ready);
255
256           if (!(dst->unsatisfied_dependencies)) {
257             if (SD_task_get_state(dst) == SD_SCHEDULED)
258               SD_task_set_state(dst, SD_RUNNABLE);
259             else
260               SD_task_set_state(dst, SD_SCHEDULABLE);
261           }
262
263           if (SD_task_get_state(dst) == SD_NOT_SCHEDULED &&
264               !(dst->is_not_ready)) {
265             SD_task_set_state(dst, SD_SCHEDULABLE);
266           }
267
268           if (SD_task_get_kind(dst) == SD_TASK_COMM_E2E) {
269             SD_dependency_t comm_dep;
270             SD_task_t comm_dst;
271             xbt_dynar_get_cpy(dst->tasks_after, 0, &comm_dep);
272             comm_dst = comm_dep->dst;
273             if (SD_task_get_state(comm_dst) == SD_NOT_SCHEDULED &&
274                 comm_dst->is_not_ready > 0) {
275               comm_dst->is_not_ready--;
276
277             XBT_DEBUG("%s is a transfer, %s may be ready now if %d=0",
278                SD_task_get_name(dst), SD_task_get_name(comm_dst),
279                comm_dst->is_not_ready);
280
281               if (!(comm_dst->is_not_ready)) {
282                 SD_task_set_state(comm_dst, SD_SCHEDULABLE);
283               }
284             }
285           }
286
287           /* is dst runnable now? */
288           if (SD_task_get_state(dst) == SD_RUNNABLE
289               && !sd_global->watch_point_reached) {
290             XBT_VERB("Executing task '%s'", SD_task_get_name(dst));
291             SD_task_run(dst);
292             xbt_dynar_push(sd_global->return_set, &dst);
293           }
294         }
295       }
296
297       /* let's see which tasks have just failed */
298       while ((action = surf_model_extract_failed_action_set(model))) {
299         task = (SD_task_t) action->getData();
300         task->start_time = surf_action_get_start_time(task->surf_action);
301         task->finish_time = surf_get_clock();
302         XBT_VERB("Task '%s' failed", SD_task_get_name(task));
303         SD_task_set_state(task, SD_FAILED);
304         action->unref();
305         task->surf_action = NULL;
306
307         xbt_dynar_push(sd_global->return_set, &task);
308       }
309     }
310   }
311
312   if (!sd_global->watch_point_reached && how_long<0){
313     if ((int) xbt_dynar_length(sd_global->completed_task_set) <
314          sd_global->task_number){
315         XBT_WARN("Simulation is finished but %d tasks are still not done",
316             (sd_global->task_number -
317              (int) xbt_dynar_length(sd_global->completed_task_set)));
318         static const char* state_names[] =
319               { "SD_NOT_SCHEDULED", "SD_SCHEDULABLE", "SD_SCHEDULED",
320                 "SD_RUNNABLE", "SD_RUNNING", "SD_DONE",
321                 "SD_FAILED" };
322         xbt_dynar_foreach(sd_global->initial_task_set, iter, task){
323           XBT_WARN("%s is in %s state", SD_task_get_name(task),
324                    state_names[SD_task_get_state(task)]);
325         }
326     }
327   }
328
329   XBT_DEBUG("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
330          elapsed_time, total_time, sd_global->watch_point_reached);
331   XBT_DEBUG("current time = %f", surf_get_clock());
332
333   return sd_global->return_set;
334 }
335
336 /**
337  * \brief Returns the current clock
338  *
339  * \return the current clock, in second
340  */
341 double SD_get_clock(void) {
342   return surf_get_clock();
343 }
344
345 /**
346  * \brief Destroys all SD internal data
347  *
348  * This function should be called when the simulation is over. Don't forget
349  * to destroy too.
350  *
351  * \see SD_init(), SD_task_destroy()
352  */
353 void SD_exit(void)
354 {
355   TRACE_surf_resource_utilization_release();
356
357   xbt_mallocator_free(sd_global->task_mallocator);
358
359   XBT_DEBUG("Destroying the dynars ...");
360   xbt_dynar_free_container(&(sd_global->initial_task_set));
361   xbt_dynar_free_container(&(sd_global->executable_task_set));
362   xbt_dynar_free_container(&(sd_global->completed_task_set));
363   xbt_dynar_free_container(&(sd_global->return_set));
364
365   TRACE_end();
366
367   xbt_free(sd_global);
368   sd_global = NULL;
369
370 #ifdef HAVE_JEDULE
371   jedule_sd_cleanup();
372   jedule_sd_exit();
373 #endif
374
375   XBT_DEBUG("Exiting Surf...");
376   surf_exit();
377 }