Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Objectify SD_task_t
[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<Task*>* simulate(double how_long)
25 {
26   XBT_VERB("Run simulation for %f seconds", how_long);
27
28   auto engine = sd_global->engine_->get_impl();
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_global->runnable_tasks.begin()))->run();
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 kernel::resource::Action* action = model->extract_done_action();
51       while (action != nullptr && action->get_data() != nullptr) {
52         auto* task = static_cast<Task*>(action->get_data());
53         XBT_VERB("Task '%s' done", task->get_cname());
54         task->set_state(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->get_successors())
61           succ->released_by(task);
62         task->clear_successors();
63
64         for (auto const& output : task->get_outputs())
65           output->produced_by(task);
66         task->clear_outputs();
67         action = model->extract_done_action();
68       }
69
70       /* let's see which tasks have just failed */
71       action = model->extract_failed_action();
72       while (action != nullptr) {
73         auto* task = static_cast<Task*>(action->get_data());
74         XBT_VERB("Task '%s' failed", task->get_cname());
75         task->set_state(SD_FAILED);
76         sd_global->return_set.insert(task);
77         action = model->extract_failed_action();
78       }
79     }
80   }
81
82   if (not sd_global->watch_point_reached && how_long < 0 && not sd_global->initial_tasks.empty()) {
83     XBT_WARN("Simulation is finished but %zu tasks are still not done", sd_global->initial_tasks.size());
84     for (auto const& t : sd_global->initial_tasks)
85       XBT_WARN("%s is in %s state", t->get_cname(), __get_state_name(t->get_state()));
86   }
87
88   XBT_DEBUG("elapsed_time = %f, total_time = %f, watch_point_reached = %d", elapsed_time, total_time,
89             sd_global->watch_point_reached);
90   XBT_DEBUG("current time = %f", simgrid_get_clock());
91
92   return &sd_global->return_set;
93 }
94 } // namespace sd
95 } // namespace simgrid
96
97 /**
98  * @brief helper for pretty printing of task state
99  * @param state the state of a task
100  * @return the equivalent as a readable string
101  */
102 const char* __get_state_name(e_SD_task_state_t state)
103 {
104   static constexpr std::array<const char*, 7> state_names{
105       {"not scheduled", "schedulable", "scheduled", "runnable", "running", "done", "failed"}};
106   return state_names.at(static_cast<int>(log2(static_cast<double>(state))));
107 }
108
109 /**
110  * @brief Initializes SD internal data
111  *
112  * This function must be called before any other SD function. Then you should call SD_create_environment().
113  *
114  * @param argc argument number
115  * @param argv argument list
116  * @see SD_create_environment(), SD_exit()
117  */
118 void SD_init_nocheck(int* argc, char** argv)
119 {
120   xbt_assert(sd_global == nullptr, "SD_init() already called");
121
122   sd_global = std::make_unique<simgrid::sd::Global>(argc, argv);
123
124   simgrid::config::set_default<std::string>("host/model", "ptask_L07");
125   if (simgrid::config::get_value<bool>("debug/clean-atexit"))
126     atexit(SD_exit);
127 }
128
129 /** @brief set a configuration variable
130  *
131  * Do --help on any simgrid binary to see the list of currently existing configuration variables, and
132  * see Section @ref options.
133  *
134  * Example: SD_config("host/model","default")
135  */
136 void SD_config(const char* key, const char* value)
137 {
138   xbt_assert(sd_global, "ERROR: Please call SD_init() before using SD_config()");
139   simgrid::config::set_as_string(key, value);
140 }
141
142 /**
143  * @brief Creates the environment
144  *
145  * The environment (i.e. the @ref SD_host_api "hosts" and the @ref SD_link_api "links") is created with
146  * the data stored in the given XML platform file.
147  *
148  * @param platform_file name of an XML file describing the environment to create
149  * @see SD_host_api, SD_link_api
150  *
151  * The XML file follows this DTD:
152  *
153  *     @include simgrid.dtd
154  *
155  * Here is a small example of such a platform:
156  *
157  *     @include small_platform.xml
158  */
159 void SD_create_environment(const char* platform_file)
160 {
161   sd_global->engine_->load_platform(platform_file);
162
163   XBT_DEBUG("Host number: %zu, link number: %zu", sg_host_count(), sg_link_count());
164 #if SIMGRID_HAVE_JEDULE
165   jedule_sd_init();
166 #endif
167   XBT_VERB("Starting simulation...");
168   sd_global->engine_->get_impl()->presolve(); /* Takes traces into account */
169 }
170
171 /**
172  * @brief Launches the simulation.
173  *
174  * The function will execute the @ref SD_RUNNABLE runnable tasks.
175  * If @a how_long is positive, then the simulation will be stopped either when time reaches @a how_long or when a watch
176  * point is reached.
177  * A non-positive value for @a how_long means no time limit, in which case the simulation will be stopped either when a
178  * watch point is reached or when no more task can be executed.
179  * Then you can call SD_simulate() again.
180  *
181  * @param how_long maximum duration of the simulation (a negative value means no time limit)
182  * @return a dynar of @ref SD_task_t whose state has changed.
183  * @see SD_task_schedule(), SD_task_watch()
184  */
185 void SD_simulate(double how_long)
186 {
187   simgrid::sd::simulate(how_long);
188 }
189
190 void SD_simulate_with_update(double how_long, xbt_dynar_t changed_tasks_dynar)
191 {
192   const std::set<SD_task_t>* changed_tasks = simgrid::sd::simulate(how_long);
193   for (auto const& task : *changed_tasks)
194     xbt_dynar_push(changed_tasks_dynar, &task);
195 }
196
197 /** @brief Returns the current clock, in seconds */
198 double SD_get_clock()
199 {
200   return simgrid_get_clock();
201 }
202
203 /**
204  * @brief Destroys all SD internal data
205  * This function should be called when the simulation is over. Don't forget to destroy too.
206  * @see SD_init(), SD_task_destroy()
207  */
208 void SD_exit()
209 {
210 #if SIMGRID_HAVE_JEDULE
211   jedule_sd_exit();
212 #endif
213   sd_global->engine_->shutdown();
214 }