Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1857b859365a843fbb3fa0d1a3a06808c769dcd8
[simgrid.git] / src / kernel / EngineImpl.hpp
1 /* Copyright (c) 2016-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 #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/functional.hpp>
14
15 #include <map>
16 #include <set>
17 #include <string>
18 #include <unordered_map>
19
20 namespace simgrid {
21 namespace kernel {
22
23 class EngineImpl {
24   std::map<std::string, s4u::Host*, std::less<>> hosts_;
25   std::map<std::string, resource::LinkImpl*, std::less<>> links_;
26   std::unordered_map<std::string, routing::NetPoint*> netpoints_;
27   std::unordered_map<std::string, actor::ActorCodeFactory> registered_functions; // Maps function names to actor code
28   actor::ActorCodeFactory default_function; // Function to use as a fallback when the provided name matches nothing
29   std::vector<resource::Model*> models_;
30   std::unordered_map<std::string, std::shared_ptr<resource::Model>> models_prio_;
31   routing::NetZoneImpl* netzone_root_ = nullptr;
32   std::set<kernel::actor::ActorImpl*> daemons_;
33
34   std::vector<xbt::Task<void()>> tasks;
35   std::vector<xbt::Task<void()>> tasksTemp;
36
37   friend s4u::Engine;
38
39 public:
40   EngineImpl() = default;
41
42   EngineImpl(const EngineImpl&) = delete;
43   EngineImpl& operator=(const EngineImpl&) = delete;
44   virtual ~EngineImpl();
45
46   void load_deployment(const std::string& file) const;
47   void register_function(const std::string& name, const actor::ActorCodeFactory& code);
48   void register_default(const actor::ActorCodeFactory& code);
49
50   /**
51    * @brief Add a model to engine list
52    *
53    * @param model Pointer to model
54    * @param list  List of dependencies for this model
55    */
56   void add_model(std::shared_ptr<simgrid::kernel::resource::Model> model,
57                  const std::vector<resource::Model*>& dep_models = {});
58
59   /** @brief Get list of all models managed by this engine */
60   const std::vector<resource::Model*>& get_all_models() const { return models_; }
61
62   static EngineImpl* get_instance() { return simgrid::s4u::Engine::get_instance()->pimpl; }
63   actor::ActorCodeFactory get_function(const std::string& name)
64   {
65     auto res = registered_functions.find(name);
66     if (res == registered_functions.end())
67       return default_function;
68     else
69       return res->second;
70   }
71   void add_daemon(actor::ActorImpl* d) { daemons_.insert(d); }
72   void rm_daemon(actor::ActorImpl* d);
73
74   bool execute_tasks();
75   void add_task(xbt::Task<void()>&& t) { tasks.push_back(std::move(t)); }
76   void wake_all_waiting_actors() const;
77   void display_all_actor_status() const;
78
79   /** @brief Run the main simulation loop. */
80   void run();
81 };
82
83 } // namespace kernel
84 } // namespace simgrid
85
86 #endif