Logo AND Algorithmique Numérique Distribuée

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