Logo AND Algorithmique Numérique Distribuée

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