SST  9.0.0
StructuralSimulationToolkit
factory.h
1 // Copyright 2009-2019 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2019, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef _SST_CORE_FACTORY_H
13 #define _SST_CORE_FACTORY_H
14 
15 #include <sst/core/sst_types.h>
16 
17 #include <stdio.h>
18 #include <mutex>
19 
20 #include <sst/core/params.h>
21 #include <sst/core/eli/elementinfo.h>
22 
23 /* Forward declare for Friendship */
24 extern int main(int argc, char **argv);
25 
26 namespace SST {
27 namespace Statistics {
28 class StatisticOutput;
29 class StatisticBase;
30 }
31 
32 class Module;
33 class Component;
34 class BaseComponent;
35 class SubComponent;
36 class ElemLoader;
37 class SSTElementPythonModule;
38 
39 /**
40  * Class for instantiating Components, Links and the like out
41  * of element libraries.
42  */
43 class Factory {
44 public:
45 
46  static Factory* getFactory() { return instance; }
47 
48  /** Get a list of allowed ports for a given component type.
49  * @param type - Name of component in lib.name format
50  * @return True if this is a valid portname
51  */
52  bool isPortNameValid(const std::string &type, const std::string port_name);
53 
54  /** Get a list of allowed param keys for a given component type.
55  * @param type - Name of component in lib.name format
56  * @return True if this is a valid portname
57  */
58  const Params::KeySet_t& getParamNames(const std::string &type);
59 
60 
61  /** Attempt to create a new Component instantiation
62  * @param id - The unique ID of the component instantiation
63  * @param componentname - The fully qualified elementlibname.componentname type of component
64  * @param params - The params to pass to the component's constructor
65  * @return Newly created component
66  */
67  Component* CreateComponent(ComponentId_t id, std::string &componentname,
68  Params& params);
69 
70  /** Ensure that an element library containing the required event is loaded
71  * @param eventname - The fully qualified elementlibname.eventname type
72  */
73  void RequireEvent(std::string eventname);
74 
75  /** Instantiate a new Module
76  * @param type - Fully qualified elementlibname.modulename type
77  * @param params - Parameters to pass to the Module's constructor
78  */
79  Module* CreateModule(std::string type, Params& params);
80 
81  /** Instantiate a new Module
82  * @param type - Fully qualified elementlibname.modulename type
83  * @param comp - Component instance to pass to the Module's constructor
84  * @param params - Parameters to pass to the Module's constructor
85  */
86  Module* CreateModuleWithComponent(std::string type, Component* comp, Params& params);
87 
88  /** Instantiate a new Module
89  * @param type - Fully qualified elementlibname.modulename type
90  * @param comp - Component instance to pass to the SubComponent's constructor
91  * @param params - Parameters to pass to the SubComponent's constructor
92  */
93  SubComponent* CreateSubComponent(std::string type, Component* comp, Params& params);
94 
95 
96  bool doesSubComponentExist(std::string type);
97 
98  /** Return partitioner function
99  * @param name - Fully qualified elementlibname.partitioner type name
100  */
101  Partition::SSTPartitioner* CreatePartitioner(std::string name, RankInfo total_ranks, RankInfo my_rank, int verbosity);
102 
103 
104  /**
105  Check to see if a given element type is loadable with a particular API
106  @param name - Name of element to check in lib.name format
107  @return True if loadable as the API specified as the template parameter
108  */
109  template <class Base>
110  bool isSubComponentLoadableUsingAPI(std::string type) {
111  std::string elemlib, elem;
112  std::tie(elemlib, elem) = parseLoadName(type);
113 
114  requireLibrary(elemlib);
115  std::lock_guard<std::recursive_mutex> lock(factoryMutex);
116 
117  auto* lib = ELI::InfoDatabase::getLibrary<Base>(elemlib);
118  if (lib){
119  auto map = lib->getMap();
120  auto* info = lib->getInfo(elem);
121  if (info){
122  auto* builderLib = Base::getBuilderLibrary(elemlib);
123  if (builderLib){
124  auto* fact = builderLib->getBuilder(elem);
125  if (fact){
126  return true;
127  }
128  }
129  }
130  }
131  return false;
132  }
133 
134  /**
135  * General function for a given base class
136  * @param type
137  * @param params
138  * @param args Constructor arguments
139  */
140  template <class Base, class... CtorArgs>
141  Base* Create(const std::string& type, SST::Params& params, CtorArgs&&... args){
142  std::string elemlib, elem;
143  std::tie(elemlib, elem) = parseLoadName(type);
144 
145  requireLibrary(elemlib);
146  std::lock_guard<std::recursive_mutex> lock(factoryMutex);
147 
148  auto* lib = ELI::InfoDatabase::getLibrary<Base>(elemlib);
149  if (lib){
150  auto map = lib->getMap();
151  auto* info = lib->getInfo(elem);
152  if (info){
153  auto* builderLib = Base::getBuilderLibrary(elemlib);
154  if (builderLib){
155  auto* fact = builderLib->getBuilder(elem);
156  if (fact){
157  params.pushAllowedKeys(info->getParamNames());
158  Base* ret = fact->create(std::forward<CtorArgs>(args)...);
159  params.popAllowedKeys();
160  return ret;
161  }
162  }
163  }
164  }
165  notFound(Base::ELI_baseName(), type);
166  return nullptr;
167  }
168 
169  /** Instantiate a new Statistic
170  * @param comp - Owning component
171  * @param type - Fully qualified elementlibname.statisticname type
172  * @param statName - Name of the statistic
173  * @param statSubId - Name of the sub statistic
174  * @param params - Parameters to pass to the Statistics's constructor
175  * @param fieldType - Type of data stored in statistic
176  */
177  template <class T, class... Args>
179  BaseComponent* comp, const std::string& statName,
180  const std::string& stat, Params& params,
181  Args... args){
182  std::string elemlib, elem;
183  std::tie(elemlib, elem) = parseLoadName(type);
184  // ensure library is already loaded...
185  requireLibrary(elemlib);
186 
187  auto* lib = ELI::BuilderDatabase::getLibrary<Statistics::Statistic<T>, Args...>(elemlib);
188  if (lib){
189  auto* fact = lib->getFactory(elem);
190  if (fact){
191  return fact->create(comp, statName, stat, params, std::forward<Args>(args)...);
192  }
193  }
194  // If we make it to here, component not found
195  out.fatal(CALL_INFO, -1,"can't find requested statistic %s.\n ", type.c_str());
196  return NULL;
197  }
198 
199 
200  /** Return Python Module creation function
201  * @param name - Fully qualified elementlibname.pythonModName type name
202  */
203  SSTElementPythonModule* getPythonModule(std::string name);
204  /** Checks to see if library exists and can be loaded */
205  bool hasLibrary(std::string elemlib);
206  void requireLibrary(std::string &elemlib);
207 
208  void getLoadedLibraryNames(std::set<std::string>& lib_names);
209  void loadUnloadedLibraries(const std::set<std::string>& lib_names);
210 
211  /** Determine if a SubComponentSlot is defined in a components ElementInfoStatistic
212  * @param type - The name of the component/subcomponent
213  * @param slotName - The name of the SubComponentSlot
214  * @return True if the SubComponentSlot is defined in the component's ELI
215  */
216  bool DoesSubComponentSlotExist(const std::string& type, const std::string& slotName);
217 
218  /** Determine if a statistic is defined in a components ElementInfoStatistic
219  * @param type - The name of the component
220  * @param statisticName - The name of the statistic
221  * @return True if the statistic is defined in the component's ElementInfoStatistic
222  */
223  bool DoesComponentInfoStatisticNameExist(const std::string& type, const std::string& statisticName);
224 
225  /** Determine if a statistic is defined in a subcomponents ElementInfoStatistic
226  * @param type - The name of the subcomponent
227  * @param statisticName - The name of the statistic
228  * @return True if the statistic is defined in the component's ElementInfoStatistic
229  */
230  // bool DoesSubComponentInfoStatisticNameExist(const std::string& type, const std::string& statisticName);
231 
232  /** Get the enable level of a statistic defined in the component's ElementInfoStatistic
233  * @param componentname - The name of the component
234  * @param statisticName - The name of the statistic
235  * @return The Enable Level of the statistic from the ElementInfoStatistic
236  */
237  uint8_t GetComponentInfoStatisticEnableLevel(const std::string& type, const std::string& statisticName);
238 
239  /** Get the units of a statistic defined in the component's ElementInfoStatistic
240  * @param componentname - The name of the component
241  * @param statisticName - The name of the statistic
242  * @return The units string of the statistic from the ElementInfoStatistic
243  */
244  std::string GetComponentInfoStatisticUnits(const std::string& type, const std::string& statisticName);
245 
246 private:
247  friend int ::main(int argc, char **argv);
248 
249  void notFound(const std::string& baseName, const std::string& type);
250 
251 
252  Factory(std::string searchPaths);
253  ~Factory();
254 
255  Factory(); // Don't Implement
256  Factory(Factory const&); // Don't Implement
257  void operator=(Factory const&); // Don't Implement
258 
259  static Factory *instance;
260 
261  // find library information for name
262  bool findLibrary(std::string name, bool showErrors=true);
263  // handle low-level loading of name
264  bool loadLibrary(std::string name, bool showErrors=true);
265 
266  std::set<std::string> loaded_libraries;
267 
268  std::string searchPaths;
269 
270  ElemLoader *loader;
271  std::string loadingComponentType;
272 
273  std::pair<std::string, std::string> parseLoadName(const std::string& wholename);
274 
275  std::recursive_mutex factoryMutex;
276 
277 
278 protected:
279  Output &out;
280 };
281 
282 
283 } // namespace SST
284 
285 #endif // SST_CORE_FACTORY_H
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:54
Component * CreateComponent(ComponentId_t id, std::string &componentname, Params &params)
Attempt to create a new Component instantiation.
Definition: factory.cc:201
void RequireEvent(std::string eventname)
Ensure that an element library containing the required event is loaded.
Definition: factory.cc:546
bool isPortNameValid(const std::string &type, const std::string port_name)
Get a list of allowed ports for a given component type.
Definition: factory.cc:101
void pushAllowedKeys(const KeySet_t &keys)
Definition: params.cc:179
Main component object for the simulation.
Definition: component.h:32
bool hasLibrary(std::string elemlib)
Checks to see if library exists and can be loaded.
Definition: factory.cc:616
bool DoesComponentInfoStatisticNameExist(const std::string &type, const std::string &statisticName)
Determine if a statistic is defined in a components ElementInfoStatistic.
Definition: factory.cc:282
bool isSubComponentLoadableUsingAPI(std::string type)
Check to see if a given element type is loadable with a particular API.
Definition: factory.h:110
Forms the template defined base class for statistics gathering within SST.
Definition: elementinfo.h:43
std::string GetComponentInfoStatisticUnits(const std::string &type, const std::string &statisticName)
Get the units of a statistic defined in the component&#39;s ElementInfoStatistic.
Definition: factory.cc:381
Statistics::Statistic< T > * CreateStatistic(std::string type, BaseComponent *comp, const std::string &statName, const std::string &stat, Params &params, Args...args)
Instantiate a new Statistic.
Definition: factory.h:178
Module is a tag class used with the loadModule function.
Definition: module.h:22
Module * CreateModuleWithComponent(std::string type, Component *comp, Params &params)
Instantiate a new Module.
Definition: factory.cc:454
Class to load Element Libraries.
Definition: elemLoader.h:25
uint8_t GetComponentInfoStatisticEnableLevel(const std::string &type, const std::string &statisticName)
Determine if a statistic is defined in a subcomponents ElementInfoStatistic.
Definition: factory.cc:332
void fatal(uint32_t line, const char *file, const char *func, int exit_code, const char *format,...) const
Output the fatal message with formatting as specified by the format parameter.
Definition: output.cc:155
Partition::SSTPartitioner * CreatePartitioner(std::string name, RankInfo total_ranks, RankInfo my_rank, int verbosity)
Return partitioner function.
Definition: factory.cc:557
Definition: rankInfo.h:21
Main component object for the simulation.
Definition: baseComponent.h:52
const Params::KeySet_t & getParamNames(const std::string &type)
Get a list of allowed param keys for a given component type.
Definition: factory.cc:158
Base * Create(const std::string &type, SST::Params &params, CtorArgs &&...args)
General function for a given base class.
Definition: factory.h:141
Base class for Partitioning graphs.
Definition: sstpart.h:32
Parameter store.
Definition: params.h:45
void popAllowedKeys()
Removes the most recent set of keys considered allowed.
Definition: params.cc:185
SSTElementPythonModule * getPythonModule(std::string name)
Return Python Module creation function.
Definition: factory.cc:589
Class for instantiating Components, Links and the like out of element libraries.
Definition: factory.h:43
Base class for python modules in element libraries.
Definition: element_python.h:126
std::set< key_type, KeyCompare > KeySet_t
Definition: params.h:172
SubComponent * CreateSubComponent(std::string type, Component *comp, Params &params)
Instantiate a new Module.
Definition: factory.cc:489
SubComponent is a class loadable through the factory which allows dynamic functionality to be added t...
Definition: subcomponent.h:30
bool DoesSubComponentSlotExist(const std::string &type, const std::string &slotName)
Determine if a SubComponentSlot is defined in a components ElementInfoStatistic.
Definition: factory.cc:241
Module * CreateModule(std::string type, Params &params)
Instantiate a new Module.
Definition: factory.cc:416