Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
say bye to simgrid::surf namespace
[simgrid.git] / src / surf / surf_c_bindings.cpp
1 /* Copyright (c) 2013-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 <simgrid/s4u/Engine.hpp>
7
8 #include "src/instr/instr_private.hpp"
9 #include "src/kernel/EngineImpl.hpp"
10 #include "src/kernel/resource/DiskImpl.hpp"
11 #include "src/kernel/resource/VirtualMachineImpl.hpp"
12 #include "src/kernel/resource/profile/FutureEvtSet.hpp"
13 #include "surf/surf.hpp"
14
15 #include <algorithm>
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_kernel);
18
19 /*********
20  * TOOLS *
21  *********/
22
23 extern double NOW;
24
25 void surf_presolve()
26 {
27   XBT_DEBUG("Consume all trace events occurring before the starting time.");
28   double next_event_date;
29   while ((next_event_date = simgrid::kernel::profile::future_evt_set.next_date()) != -1.0) {
30     if (next_event_date > NOW)
31       break;
32
33     double value                                  = -1.0;
34     simgrid::kernel::resource::Resource* resource = nullptr;
35     while (auto* event = simgrid::kernel::profile::future_evt_set.pop_leq(next_event_date, &value, &resource)) {
36       if (value >= 0)
37         resource->apply_event(event, value);
38     }
39   }
40
41   XBT_DEBUG("Set every models in the right state by updating them to 0.");
42   for (auto const& model : simgrid::kernel::EngineImpl::get_instance()->get_all_models())
43     model->update_actions_state(NOW, 0.0);
44 }
45
46 double surf_solve(double max_date)
47 {
48   double time_delta                             = -1.0; /* duration */
49   double value                                  = -1.0;
50   simgrid::kernel::resource::Resource* resource = nullptr;
51
52   if (max_date != -1.0) {
53     xbt_assert(max_date >= NOW, "You asked to simulate up to %f, but that's in the past already", max_date);
54
55     time_delta = max_date - NOW;
56   }
57
58   XBT_DEBUG("Looking for next event in all models");
59   auto engine = simgrid::kernel::EngineImpl::get_instance();
60   for (auto model : engine->get_all_models()) {
61     if (not model->next_occurring_event_is_idempotent()) {
62       continue;
63     }
64     double next_event = model->next_occurring_event(NOW);
65     if ((time_delta < 0.0 || next_event < time_delta) && next_event >= 0.0) {
66       time_delta = next_event;
67     }
68   }
69
70   XBT_DEBUG("Min for resources (remember that NS3 don't update that value): %f", time_delta);
71
72   XBT_DEBUG("Looking for next trace event");
73
74   while (true) { // Handle next occurring events until none remains
75     double next_event_date = simgrid::kernel::profile::future_evt_set.next_date();
76     XBT_DEBUG("Next TRACE event: %f", next_event_date);
77
78     for (auto model : engine->get_all_models()) {
79       /* Skip all idempotent models, they were already treated above
80        * NS3 is the one to handled here */
81       if (model->next_occurring_event_is_idempotent())
82         continue;
83
84       if (next_event_date != -1.0) {
85         time_delta = std::min(next_event_date - NOW, time_delta);
86       } else {
87         time_delta = std::max(next_event_date - NOW, time_delta); // Get the positive component
88       }
89
90       XBT_DEBUG("Run the NS3 network at most %fs", time_delta);
91       // run until min or next flow
92       double model_next_action_end = model->next_occurring_event(time_delta);
93
94       XBT_DEBUG("Min for network : %f", model_next_action_end);
95       if (model_next_action_end >= 0.0)
96         time_delta = model_next_action_end;
97     }
98
99     if (next_event_date < 0.0 || (next_event_date > NOW + time_delta)) {
100       // next event may have already occurred or will after the next resource change, then bail out
101       XBT_DEBUG("no next usable TRACE event. Stop searching for it");
102       break;
103     }
104
105     XBT_DEBUG("Updating models (min = %g, NOW = %g, next_event_date = %g)", time_delta, NOW, next_event_date);
106
107     while (auto* event = simgrid::kernel::profile::future_evt_set.pop_leq(next_event_date, &value, &resource)) {
108       if (resource->is_used() || (watched_hosts().find(resource->get_cname()) != watched_hosts().end())) {
109         time_delta = next_event_date - NOW;
110         XBT_DEBUG("This event invalidates the next_occurring_event() computation of models. Next event set to %f",
111                   time_delta);
112       }
113       // FIXME: I'm too lame to update NOW live, so I change it and restore it so that the real update with surf_min
114       // will work
115       double round_start = NOW;
116       NOW                = next_event_date;
117       /* update state of the corresponding resource to the new value. Does not touch lmm.
118          It will be modified if needed when updating actions */
119       XBT_DEBUG("Calling update_resource_state for resource %s", resource->get_cname());
120       resource->apply_event(event, value);
121       NOW = round_start;
122     }
123   }
124
125   /* FIXME: Moved this test to here to avoid stopping simulation if there are actions running on cpus and all cpus are
126    * with availability = 0. This may cause an infinite loop if one cpu has a trace with periodicity = 0 and the other a
127    * trace with periodicity > 0.
128    * The options are: all traces with same periodicity(0 or >0) or we need to change the way how the events are managed
129    */
130   if (time_delta < 0) {
131     XBT_DEBUG("No next event at all. Bail out now.");
132     return -1.0;
133   }
134
135   XBT_DEBUG("Duration set to %f", time_delta);
136
137   // Bump the time: jump into the future
138   NOW = NOW + time_delta;
139
140   // Inform the models of the date change
141   for (auto const& model : simgrid::kernel::EngineImpl::get_instance()->get_all_models())
142     model->update_actions_state(NOW, time_delta);
143
144   simgrid::s4u::Engine::on_time_advance(time_delta);
145
146   return time_delta;
147 }