Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More doc for SMPI
[simgrid.git] / src / kernel / resource / profile / FutureEvtSet.cpp
1 /* Copyright (c) 2004-2023. 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 "src/kernel/resource/profile/FutureEvtSet.hpp"
7 #include "src/kernel/resource/Resource.hpp"
8 #include "src/kernel/resource/profile/Event.hpp"
9 #include "src/kernel/resource/profile/Profile.hpp"
10 #include <simgrid/s4u/Engine.hpp>
11
12 namespace simgrid::kernel::profile {
13
14 simgrid::kernel::profile::FutureEvtSet future_evt_set; // FIXME: singleton antipattern
15
16 FutureEvtSet::FutureEvtSet() = default;
17 FutureEvtSet::~FutureEvtSet()
18 {
19   while (not heap_.empty()) {
20     delete heap_.top().second;
21     heap_.pop();
22   }
23 }
24
25 /** @brief Schedules an event to a future date */
26 void FutureEvtSet::add_event(double date, Event* evt)
27 {
28   if (heap_.empty())
29     s4u::Engine::on_platform_created_cb([this]() {
30       /* Handle the events of time = 0 right after the platform creation */
31       double next_event_date;
32       while ((next_event_date = this->next_date()) != -1.0) {
33         if (next_event_date > 0)
34           break;
35
36         double value                 = -1.0;
37         resource::Resource* resource = nullptr;
38         while (auto* event = this->pop_leq(next_event_date, &value, &resource)) {
39           if (value >= 0)
40             resource->apply_event(event, value);
41         }
42       }
43     });
44
45   heap_.emplace(date, evt);
46 }
47
48 /** @brief returns the date of the next occurring event (or -1 if empty) */
49 double FutureEvtSet::next_date() const
50 {
51   return heap_.empty() ? -1.0 : heap_.top().first;
52 }
53
54 /** @brief Retrieves the next occurring event, or nullptr if none happens before date */
55 Event* FutureEvtSet::pop_leq(double date, double* value, resource::Resource** resource)
56 {
57   if (next_date() > date || heap_.empty())
58     return nullptr;
59
60   Event* event       = heap_.top().second;
61   Profile* profile   = event->profile;
62   DatedValue dateVal = profile->next(event);
63
64   *resource = event->resource;
65   *value    = dateVal.value_;
66
67   heap_.pop();
68
69   return event;
70 }
71 } // namespace simgrid::kernel::profile