Logo AND Algorithmique Numérique Distribuée

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