Logo AND Algorithmique Numérique Distribuée

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