Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move globals to EngineImpl
[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
14 #include <map>
15 #include <string>
16 #include <unordered_map>
17
18 namespace simgrid {
19 namespace kernel {
20
21 class EngineImpl {
22   std::map<std::string, s4u::Host*, std::less<>> hosts_;
23   std::map<std::string, resource::LinkImpl*, std::less<>> links_;
24   std::unordered_map<std::string, routing::NetPoint*> netpoints_;
25   std::unordered_map<std::string, actor::ActorCodeFactory> registered_functions; // Maps function names to actor code
26   actor::ActorCodeFactory default_function; // Function to use as a fallback when the provided name matches nothing
27   std::vector<std::unique_ptr<simgrid::kernel::resource::Model>> models_;
28   std::unordered_map<simgrid::kernel::resource::Model::Type, std::vector<simgrid::kernel::resource::Model*>>
29       models_by_type_;
30
31   friend s4u::Engine;
32
33 public:
34   EngineImpl() = default;
35
36   EngineImpl(const EngineImpl&) = delete;
37   EngineImpl& operator=(const EngineImpl&) = delete;
38   virtual ~EngineImpl();
39
40   void load_deployment(const std::string& file) const;
41   void register_function(const std::string& name, const actor::ActorCodeFactory& code);
42   void register_default(const actor::ActorCodeFactory& code);
43
44   /**
45    * @brief Add a model to engine list
46    *
47    * @param type Model type (network, disk, etc)
48    * @param model Pointer to model
49    * @param is_default Is this the default model for this type of resource in this exp
50    */
51   void add_model(simgrid::kernel::resource::Model::Type type, std::unique_ptr<simgrid::kernel::resource::Model> model,
52                  bool is_default = false);
53   /**
54    * @brief Add a model (specific for ptask)
55    *
56    * Ptask is special. The CPU and NETWORK models need to be in the managed
57    * resources by surf_solve (model_by_type) but cannot be in the list of
58    * all models (old all_existing_models global variable)
59    *
60    * This methods does this job while we cannot handle ptask as the remaining models
61    */
62   void add_model_ptask(simgrid::kernel::resource::Model::Type type, simgrid::kernel::resource::Model* model,
63                        bool is_default);
64   /** @brief Get current default model for a resource type */
65   simgrid::kernel::resource::Model* get_default_model(simgrid::kernel::resource::Model::Type type);
66
67   /** @brief Get list of models created for a resource type */
68   const std::vector<simgrid::kernel::resource::Model*>& get_model_list(simgrid::kernel::resource::Model::Type type)
69   {
70     return models_by_type_[type];
71   }
72   /** @brief Get list of all models managed by this engine */
73   const std::vector<std::unique_ptr<simgrid::kernel::resource::Model>>& get_all_models() { return models_; }
74
75   routing::NetZoneImpl* netzone_root_ = nullptr;
76   static EngineImpl* get_instance() { return simgrid::s4u::Engine::get_instance()->pimpl; }
77   actor::ActorCodeFactory get_function(const std::string& name)
78   {
79     auto res = registered_functions.find(name);
80     if (res == registered_functions.end())
81       return default_function;
82     else
83       return res->second;
84   }
85 };
86
87 } // namespace kernel
88 } // namespace simgrid
89
90 #endif