Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill a parameter that is always true
[simgrid.git] / src / kernel / EngineImpl.hpp
1 /* Copyright (c) 2016-2022. 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 #ifndef SIMGRID_KERNEL_ENGINEIMPL_HPP
7 #define SIMGRID_KERNEL_ENGINEIMPL_HPP
8
9 #include <simgrid/kernel/resource/Model.hpp>
10 #include <simgrid/s4u/Engine.hpp>
11 #include <simgrid/s4u/NetZone.hpp>
12 #include <simgrid/simix.hpp>
13 #include <xbt/dynar.h>
14 #include <xbt/functional.hpp>
15
16 #include "src/kernel/activity/ExecImpl.hpp"
17 #include "src/kernel/activity/IoImpl.hpp"
18 #include "src/kernel/activity/MailboxImpl.hpp"
19 #include "src/kernel/activity/SleepImpl.hpp"
20 #include "src/kernel/activity/Synchro.hpp"
21 #include "src/kernel/actor/ActorImpl.hpp"
22 #include "src/kernel/resource/SplitDuplexLinkImpl.hpp"
23
24 #include <boost/intrusive/list.hpp>
25 #include <map>
26 #include <mutex>
27 #include <set>
28 #include <string>
29 #include <unordered_map>
30 #include <vector>
31
32 namespace simgrid::kernel {
33
34 class EngineImpl {
35   std::unordered_map<std::string, routing::NetPoint*> netpoints_;
36   std::unordered_map<std::string, activity::MailboxImpl*> mailboxes_;
37
38   std::unordered_map<std::string, actor::ActorCodeFactory> registered_functions; // Maps function names to actor code
39   actor::ActorCodeFactory default_function; // Function to use as a fallback when the provided name matches nothing
40   std::vector<resource::Model*> models_;
41   std::unordered_map<std::string, std::shared_ptr<resource::Model>> models_prio_;
42   routing::NetZoneImpl* netzone_root_ = nullptr;
43   std::set<actor::ActorImpl*> daemons_;
44   std::vector<actor::ActorImpl*> actors_to_run_;
45   std::vector<actor::ActorImpl*> actors_that_ran_;
46   std::map<aid_t, actor::ActorImpl*> actor_list_;
47   boost::intrusive::list<actor::ActorImpl,
48                          boost::intrusive::member_hook<actor::ActorImpl, boost::intrusive::list_member_hook<>,
49                                                        &actor::ActorImpl::kernel_destroy_list_hook>>
50       actors_to_destroy_;
51
52   static double now_;
53   static EngineImpl* instance_;
54   actor::ActorImpl* maestro_ = nullptr;
55   context::ContextFactory* context_factory_ = nullptr;
56
57   std::unique_ptr<void, std::function<int(void*)>> platf_handle_; //!< handle for platform library
58   friend s4u::Engine;
59
60 public:
61   EngineImpl() = default;
62
63   /* Currently, only one instance is allowed to exist. This is why you can't copy or move it */
64 #ifndef DOXYGEN
65   EngineImpl(const EngineImpl&) = delete;
66   EngineImpl& operator=(const EngineImpl&) = delete;
67   virtual ~EngineImpl();
68   static void shutdown();
69 #endif
70
71   void initialize(int* argc, char** argv);
72   void load_platform(const std::string& platf);
73   void load_deployment(const std::string& file) const;
74   void seal_platform() const;
75   void register_function(const std::string& name, const actor::ActorCodeFactory& code);
76   void register_default(const actor::ActorCodeFactory& code);
77
78   bool is_maestro(const actor::ActorImpl* actor) const { return actor == maestro_; }
79   void set_maestro(actor::ActorImpl* actor) { maestro_ = actor; }
80   actor::ActorImpl* get_maestro() const { return maestro_; }
81
82   context::ContextFactory* get_context_factory() const { return context_factory_; }
83   void set_context_factory(context::ContextFactory* factory) { context_factory_ = factory; }
84   bool has_context_factory() const { return context_factory_ != nullptr; }
85
86   void context_mod_init() const;
87   /**
88    * @brief Add a model to engine list
89    *
90    * @param model Pointer to model
91    * @param list  List of dependencies for this model
92    */
93   void add_model(std::shared_ptr<simgrid::kernel::resource::Model> model,
94                  const std::vector<resource::Model*>& dep_models = {});
95
96   /** @brief Get list of all models managed by this engine */
97   const std::vector<resource::Model*>& get_all_models() const { return models_; }
98
99   static bool has_instance() { return s4u::Engine::has_instance(); }
100   static EngineImpl* get_instance() { return s4u::Engine::get_instance()->pimpl; }
101   static EngineImpl* get_instance(int* argc, char** argv) { return s4u::Engine::get_instance(argc, argv)->pimpl; }
102
103   actor::ActorCodeFactory get_function(const std::string& name)
104   {
105     auto res = registered_functions.find(name);
106     if (res == registered_functions.end())
107       return default_function;
108     else
109       return res->second;
110   }
111
112   routing::NetZoneImpl* get_netzone_root() const { return netzone_root_; }
113
114   void add_daemon(actor::ActorImpl* d) { daemons_.insert(d); }
115   void remove_daemon(actor::ActorImpl* d);
116   void add_actor_to_run_list(actor::ActorImpl* actor);
117   void add_actor_to_run_list_no_check(actor::ActorImpl* actor);
118   void add_actor_to_destroy_list(actor::ActorImpl& actor) { actors_to_destroy_.push_back(actor); }
119
120   bool has_actors_to_run() const { return not actors_to_run_.empty(); }
121   const actor::ActorImpl* get_first_actor_to_run() const { return actors_to_run_.front(); }
122   const actor::ActorImpl* get_actor_to_run_at(unsigned long int i) const { return actors_to_run_[i]; }
123   unsigned long int get_actor_to_run_count() const { return actors_to_run_.size(); }
124   size_t get_actor_count() const { return actor_list_.size(); }
125   actor::ActorImpl* get_actor_by_pid(aid_t pid);
126   void add_actor(aid_t pid, actor::ActorImpl* actor) { actor_list_[pid] = actor; }
127   void remove_actor(aid_t pid) { actor_list_.erase(pid); }
128
129   const std::map<aid_t, actor::ActorImpl*>& get_actor_list() const { return actor_list_; }
130   const std::vector<actor::ActorImpl*>& get_actors_to_run() const { return actors_to_run_; }
131   const std::vector<actor::ActorImpl*>& get_actors_that_ran() const { return actors_that_ran_; }
132
133   void handle_ended_actions() const;
134   /**
135    * Garbage collection
136    *
137    * Should be called some time to time to free the memory allocated for actors that have finished (or killed).
138    */
139   void empty_trash();
140   void display_all_actor_status() const;
141   void run_all_actors();
142
143   /*  @brief Finish simulation initialization
144    *  This function must be called before the first call to solve()
145    */
146   void presolve() const;
147   /** @brief Performs a part of the simulation
148    *  @param max_date Maximum date to update the simulation to, or -1
149    *  @return the elapsed time, or -1.0 if no event could be executed
150    *
151    *  This function execute all possible events, update the action states  and returns the time elapsed.
152    *  When you call execute or communicate on a model, the corresponding actions are not executed immediately but only
153    *  when you call solve().
154    *  Note that the returned elapsed time can be zero.
155    */
156   double solve(double max_date) const;
157
158   /** @brief Run the main simulation loop until the specified date (or infinitly if max_date is negative). */
159   void run(double max_date);
160
161   /** @brief Return the current time in milliseconds. */
162   static double get_clock();
163 };
164
165 } // namespace simgrid::kernel
166
167 #endif