Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'toufic' of github.com:Takishipp/simgrid
[simgrid.git] / src / simdag / sd_global.cpp
1 /* Copyright (c) 2006-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simdag_private.hpp"
7 #include "simgrid/s4u/Engine.hpp"
8 #include "simgrid/sg_config.h"
9 #include "src/include/instr/instr_interface.h"
10 #include "src/surf/surf_interface.hpp"
11
12 XBT_LOG_NEW_CATEGORY(sd, "Logging specific to SimDag");
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(sd_kernel, sd, "Logging specific to SimDag (kernel)");
14
15 simgrid::sd::Global *sd_global = nullptr;
16
17 namespace simgrid{
18 namespace sd{
19 Global::Global(){
20   watch_point_reached = false;
21   initial_tasks = new std::set<SD_task_t>();
22   runnable_tasks = new std::set<SD_task_t>();
23   completed_tasks = new std::set<SD_task_t>();
24   return_set = new std::set<SD_task_t>();
25 }
26
27 Global::~Global(){
28   delete initial_tasks;
29   delete runnable_tasks;
30   delete completed_tasks;
31   delete return_set;
32 }
33
34 std::set<SD_task_t>* simulate(double how_long){
35   XBT_VERB("Run simulation for %f seconds", how_long);
36
37   sd_global->watch_point_reached = false;
38   sd_global->return_set->clear();
39
40   /* explore the runnable tasks */
41   while(!sd_global->runnable_tasks->empty())
42     SD_task_run(*(sd_global->runnable_tasks->begin()));
43
44   double elapsed_time = 0.0;
45   double total_time = 0.0;
46   /* main loop */
47   while (elapsed_time >= 0 && (how_long < 0 || 0.00001 < (how_long -total_time)) && !sd_global->watch_point_reached) {
48
49     XBT_DEBUG("Total time: %f", total_time);
50
51     elapsed_time = surf_solve(how_long > 0 ? surf_get_clock() + how_long - total_time: -1.0);
52     XBT_DEBUG("surf_solve() returns %f", elapsed_time);
53     if (elapsed_time > 0.0)
54       total_time += elapsed_time;
55
56     /* let's see which tasks are done */
57     for (const auto& model : *all_existing_models) {
58       surf_action_t action = surf_model_extract_done_action_set(model);
59       while (action != nullptr) {
60         SD_task_t task = static_cast<SD_task_t>(action->getData());
61         XBT_VERB("Task '%s' done", SD_task_get_name(task));
62         SD_task_set_state(task, SD_DONE);
63
64         /* the state has changed. Add it only if it's the first change */
65         if (sd_global->return_set->find(task) == sd_global->return_set->end())
66           sd_global->return_set->insert(task);
67
68         /* remove the dependencies after this task */
69         for (const auto& succ : *task->successors) {
70           succ->predecessors->erase(task);
71           succ->inputs->erase(task);
72           XBT_DEBUG("Release dependency on %s: %zu remain(s). Becomes schedulable if %zu=0", SD_task_get_name(succ),
73               succ->predecessors->size()+succ->inputs->size(), succ->predecessors->size());
74
75           if (SD_task_get_state(succ) == SD_NOT_SCHEDULED && succ->predecessors->empty())
76             SD_task_set_state(succ, SD_SCHEDULABLE);
77
78           if (SD_task_get_state(succ) == SD_SCHEDULED && succ->predecessors->empty() && succ->inputs->empty())
79             SD_task_set_state(succ, SD_RUNNABLE);
80
81           if (SD_task_get_state(succ) == SD_RUNNABLE && !sd_global->watch_point_reached)
82             SD_task_run(succ);
83         }
84         task->successors->clear();
85
86         for (const auto& output : *task->outputs) {
87           output->start_time = task->finish_time;
88           output->predecessors->erase(task);
89           if (SD_task_get_state(output) == SD_SCHEDULED)
90              SD_task_set_state(output, SD_RUNNABLE);
91           else
92              SD_task_set_state(output, SD_SCHEDULABLE);
93
94           SD_task_t comm_dst = *(output->successors->begin());
95           if (SD_task_get_state(comm_dst) == SD_NOT_SCHEDULED && comm_dst->predecessors->empty()){
96             XBT_DEBUG("%s is a transfer, %s may be ready now if %zu=0",
97                 SD_task_get_name(output), SD_task_get_name(comm_dst), comm_dst->predecessors->size());
98             SD_task_set_state(comm_dst, SD_SCHEDULABLE);
99           }
100           if (SD_task_get_state(output) == SD_RUNNABLE && !sd_global->watch_point_reached)
101             SD_task_run(output);
102         }
103         task->outputs->clear();
104         action = surf_model_extract_done_action_set(model);
105       }
106
107       /* let's see which tasks have just failed */
108       action = surf_model_extract_failed_action_set(model);
109       while (action != nullptr) {
110         SD_task_t task = static_cast<SD_task_t>(action->getData());
111         XBT_VERB("Task '%s' failed", SD_task_get_name(task));
112         SD_task_set_state(task, SD_FAILED);
113         sd_global->return_set->insert(task);
114         action = surf_model_extract_failed_action_set(model);
115       }
116     }
117   }
118
119   if (!sd_global->watch_point_reached && how_long < 0 && !sd_global->initial_tasks->empty()) {
120     XBT_WARN("Simulation is finished but %zu tasks are still not done", sd_global->initial_tasks->size());
121     for (const auto& t : *sd_global->initial_tasks)
122       XBT_WARN("%s is in %s state", SD_task_get_name(t), __get_state_name(SD_task_get_state(t)));
123   }
124
125   XBT_DEBUG("elapsed_time = %f, total_time = %f, watch_point_reached = %d",
126              elapsed_time, total_time, sd_global->watch_point_reached);
127   XBT_DEBUG("current time = %f", surf_get_clock());
128
129   return sd_global->return_set;
130 }
131 }
132 }
133
134 /**
135  * \brief helper for pretty printing of task state
136  * \param state the state of a task
137  * \return the equivalent as a readable string
138  */
139 const char *__get_state_name(e_SD_task_state_t state){
140   static std::string state_names[7] =
141     { "not scheduled", "schedulable", "scheduled", "runnable","running", "done", "failed" };
142   return state_names[static_cast<int>(log2(static_cast<double>(state)))].data();
143 }
144
145 /**
146  * \brief Initializes SD internal data
147  *
148  * This function must be called before any other SD function. Then you should call SD_create_environment().
149  *
150  * \param argc argument number
151  * \param argv argument list
152  * \see SD_create_environment(), SD_exit()
153  */
154 void SD_init(int *argc, char **argv)
155 {
156   xbt_assert(sd_global == nullptr, "SD_init() already called");
157
158   sd_global = new simgrid::sd::Global();
159
160   surf_init(argc, argv);
161
162   xbt_cfg_setdefault_string("host/model", "ptask_L07");
163
164   if (_sg_cfg_exit_asap) {
165     SD_exit();
166     exit(0);
167   }
168 }
169
170 /** \brief set a configuration variable
171  *
172  * Do --help on any simgrid binary to see the list of currently existing configuration variables, and
173  * see Section @ref options.
174  *
175  * Example: SD_config("host/model","default");
176  */
177 void SD_config(const char *key, const char *value){
178   xbt_assert(sd_global,"ERROR: Please call SD_init() before using SD_config()");
179   xbt_cfg_set_as_string(key, value);
180 }
181
182 /**
183  * \brief Creates the environment
184  *
185  * The environment (i.e. the \ref SD_host_api "hosts" and the \ref SD_link_api "links") is created with
186  * the data stored in the given XML platform file.
187  *
188  * \param platform_file name of an XML file describing the environment to create
189  * \see SD_host_api, SD_link_api
190  *
191  * The XML file follows this DTD:
192  *
193  *     \include simgrid.dtd
194  *
195  * Here is a small example of such a platform:
196  *
197  *     \include small_platform.xml
198  */
199 void SD_create_environment(const char *platform_file)
200 {
201   simgrid::s4u::Engine::instance()->loadPlatform(platform_file);
202
203   XBT_DEBUG("Host number: %zu, link number: %d", sg_host_count(), sg_link_count());
204 #if HAVE_JEDULE
205   jedule_sd_init();
206 #endif
207   XBT_VERB("Starting simulation...");
208   surf_presolve();            /* Takes traces into account */
209 }
210
211 /**
212  * \brief Launches the simulation.
213  *
214  * The function will execute the \ref SD_RUNNABLE runnable tasks.
215  * If \a how_long is positive, then the simulation will be stopped either when time reaches \a how_long or when a watch
216  * point is reached.
217  * A non-positive value for \a how_long means no time limit, in which case the simulation will be stopped either when a
218  * watch point is reached or when no more task can be executed.
219  * Then you can call SD_simulate() again.
220  *
221  * \param how_long maximum duration of the simulation (a negative value means no time limit)
222  * \return a dynar of \ref SD_task_t whose state has changed.
223  * \see SD_task_schedule(), SD_task_watch()
224  */
225 void SD_simulate(double how_long)
226 {
227   simgrid::sd::simulate(how_long);
228 }
229
230 void SD_simulate_with_update(double how_long, xbt_dynar_t changed_tasks_dynar)
231 {
232   std::set<SD_task_t> *changed_tasks = simgrid::sd::simulate(how_long);
233   for (const auto& task : *changed_tasks)
234     xbt_dynar_push(changed_tasks_dynar, &task);
235 }
236
237 /** @brief Returns the current clock, in seconds */
238 double SD_get_clock() {
239   return surf_get_clock();
240 }
241
242 /**
243  * \brief Destroys all SD internal data
244  * This function should be called when the simulation is over. Don't forget to destroy too.
245  * \see SD_init(), SD_task_destroy()
246  */
247 void SD_exit()
248 {
249   TRACE_end();
250 #if HAVE_JEDULE
251   jedule_sd_exit();
252 #endif
253   delete sd_global;
254 }