SST  7.0.0
StructuralSimulationToolkit
factory.h
1 // Copyright 2009-2017 Sandia Corporation. Under the terms
2 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2017, Sandia Corporation
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/elemLoader.h>
22 #include <sst/core/element.h>
23 #include <sst/core/model/element_python.h>
24 #include <sst/core/statapi/statfieldinfo.h>
25 
26 /* Forward declare for Friendship */
27 extern int main(int argc, char **argv);
28 
29 namespace SST {
30 namespace Statistics {
31 class StatisticOutput;
32 class StatisticBase;
33 }
34 
35 class Module;
36 class Component;
37 class BaseComponent;
38 class SubComponent;
39 
40 /**
41  * Class for instantiating Components, Links and the like out
42  * of element libraries.
43  */
44 class Factory {
45 public:
46 
47  static Factory* getFactory() { return instance; }
48 
49  /** Get a list of allowed ports for a given component type.
50  * @param type - Name of component in lib.name format
51  * @return True if this is a valid portname
52  */
53  bool isPortNameValid(const std::string &type, const std::string port_name);
54 
55 
56  /** Attempt to create a new Component instantiation
57  * @param id - The unique ID of the component instantiation
58  * @param componentname - The fully qualified elementlibname.componentname type of component
59  * @param params - The params to pass to the component's constructor
60  * @return Newly created component
61  */
62  Component* CreateComponent(ComponentId_t id, std::string &componentname,
63  Params& params);
64 
65  /** Ensure that an element library containing the required event is loaded
66  * @param eventname - The fully qualified elementlibname.eventname type
67  */
68  void RequireEvent(std::string eventname);
69 
70  /** Instatiate a new Module
71  * @param type - Fully qualified elementlibname.modulename type
72  * @param params - Parameters to pass to the Module's constructor
73  */
74  Module* CreateModule(std::string type, Params& params);
75 
76  /** Instatiate a new Module
77  * @param type - Fully qualified elementlibname.modulename type
78  * @param comp - Component instance to pass to the Module's constructor
79  * @param params - Parameters to pass to the Module's constructor
80  */
81  Module* CreateModuleWithComponent(std::string type, Component* comp, Params& params);
82 
83  /** Instantiate a new Module from within the SST core
84  * @param type - Name of the module to load (just modulename, not element.modulename)
85  * @param params - Parameters to pass to the module at constructor time
86  */
87  Module* CreateCoreModule(std::string type, Params& params);
88 
89  /** Instantiate a new Module from within the SST core
90  * @param type - Name of the module to load (just modulename, not element.modulename)
91  * @param params - Parameters to pass to the module at constructor time
92  */
93  Module* CreateCoreModuleWithComponent(std::string type, Component* comp, Params& params);
94 
95  /** Instatiate a new Module
96  * @param type - Fully qualified elementlibname.modulename type
97  * @param comp - Component instance to pass to the SubComponent's constructor
98  * @param params - Parameters to pass to the SubComponent's constructor
99  */
100  SubComponent* CreateSubComponent(std::string type, Component* comp, Params& params);
101 
102  /** Return partitioner function
103  * @param name - Fully qualified elementlibname.partitioner type name
104  */
105  Partition::SSTPartitioner* CreatePartitioner(std::string name, RankInfo total_ranks, RankInfo my_rank, int verbosity);
106 
107  /** Return generator function
108  * @param name - Fully qualified elementlibname.generator type name
109  */
110  generateFunction GetGenerator(std::string name);
111 
112 
113  /** Instatiate a new Statistic
114  * @param comp - Owning component
115  * @param type - Fully qualified elementlibname.statisticname type
116  * @param statName - Name of the statistic
117  * @param statSubId - Name of the sub statistic
118  * @param params - Parameters to pass to the Statistics's constructor
119  * @param fieldType - Type of data stored in statistic
120  */
121  Statistics::StatisticBase* CreateStatistic(BaseComponent* comp, const std::string &type,
122  const std::string &statName, const std::string &statSubId,
124 
125 
126  /** Return Python Module creation function
127  * @param name - Fully qualified elementlibname.pythonModName type name
128  */
129  // genPythonModuleFunction getPythonModule(std::string name);
130  SSTElementPythonModule* getPythonModule(std::string name);
131  /** Checks to see if library exists and can be loaded */
132  bool hasLibrary(std::string elemlib);
133  void requireLibrary(std::string &elemlib);
134 
135  void getLoadedLibraryNames(std::set<std::string>& lib_names);
136  void loadUnloadedLibraries(const std::set<std::string>& lib_names);
137 
138  /** Attempt to create a new Statistic Output instantiation
139  * @param statOutputType - The name of the Statistic Output to create (Module Name)
140  * @param statOutputParams - The params to pass to the statistic output's constructor
141  * @return Newly created Statistic Output
142  */
143  Statistics::StatisticOutput* CreateStatisticOutput(const std::string& statOutputType, const Params& statOutputParams);
144 
145  /** Determine if a SubComponentSlot is defined in a components ElementInfoStatistic
146  * @param type - The name of the component/subcomponent
147  * @param slotName - The name of the SubComponentSlot
148  * @return True if the SubComponentSlot is defined in the component's ELI
149  */
150  bool DoesSubComponentSlotExist(const std::string& type, const std::string& slotName);
151 
152  /** Determine if a statistic is defined in a components ElementInfoStatistic
153  * @param type - The name of the component
154  * @param statisticName - The name of the statistic
155  * @return True if the statistic is defined in the component's ElementInfoStatistic
156  */
157  bool DoesComponentInfoStatisticNameExist(const std::string& type, const std::string& statisticName);
158 
159  /** Determine if a statistic is defined in a subcomponents ElementInfoStatistic
160  * @param type - The name of the subcomponent
161  * @param statisticName - The name of the statistic
162  * @return True if the statistic is defined in the component's ElementInfoStatistic
163  */
164  bool DoesSubComponentInfoStatisticNameExist(const std::string& type, const std::string& statisticName);
165 
166  /** Get the enable level of a statistic defined in the component's ElementInfoStatistic
167  * @param componentname - The name of the component
168  * @param statisticName - The name of the statistic
169  * @return The Enable Level of the statistic from the ElementInfoStatistic
170  */
171  uint8_t GetComponentInfoStatisticEnableLevel(const std::string& type, const std::string& statisticName);
172 
173  /** Get the units of a statistic defined in the component's ElementInfoStatistic
174  * @param componentname - The name of the component
175  * @param statisticName - The name of the statistic
176  * @return The units string of the statistic from the ElementInfoStatistic
177  */
178  std::string GetComponentInfoStatisticUnits(const std::string& type, const std::string& statisticName);
179 
180 private:
181  Module* LoadCoreModule_StatisticOutputs(std::string& type, Params& params);
182 
183  friend int ::main(int argc, char **argv);
184 
185  struct ComponentInfo {
186  const ElementInfoComponent* component;
187  Params::KeySet_t params;
188  std::vector<std::string> ports;
189  std::vector<std::string> statNames;
190  std::vector<std::string> statUnits;
191  std::vector<uint8_t> statEnableLevels;
192  std::vector<std::string> subcomponentslots;
193 
194  ComponentInfo() {}
195 
196  ComponentInfo(const ElementInfoComponent *component, Params::KeySet_t params) :
197  component(component), params(params)
198  {
199  const ElementInfoPort *p = component->ports;
200  while ( NULL != p && NULL != p->name ) {
201  ports.push_back(p->name);
202  p++;
203  }
204 
205  const ElementInfoStatistic *s = component->stats;
206  while ( NULL != s && NULL != s->name ) {
207  statNames.push_back(s->name);
208  statUnits.push_back(s->units);
209  statEnableLevels.push_back(s->enableLevel);
210  s++;
211  }
212 
213  const ElementInfoSubComponentSlot *ss = component->subComponents;
214  while ( NULL != ss && NULL != ss->name ) {
215  subcomponentslots.push_back(ss->name);
216  ss++;
217  }
218  }
219 
220  ComponentInfo(const ComponentInfo& old) : component(old.component), params(old.params), ports(old.ports),
221  statNames(old.statNames), statUnits(old.statUnits), statEnableLevels(old.statEnableLevels),
222  subcomponentslots(old.subcomponentslots)
223  { }
224 
225  ComponentInfo& operator=(const ComponentInfo& old)
226  {
227  component = old.component;
228  params = old.params;
229  ports = old.ports;
230  statNames = old.statNames;
231  statUnits = old.statUnits;
232  statEnableLevels = old.statEnableLevels;
233  subcomponentslots = old.subcomponentslots;
234  return *this;
235  }
236  };
237 
238  struct ModuleInfo {
239  const ElementInfoModule* module;
240  Params::KeySet_t params;
241 
242  ModuleInfo() {}
243 
244  ModuleInfo(const ElementInfoModule *module,
245  Params::KeySet_t params) : module(module), params(params)
246  { }
247 
248  ModuleInfo(const ModuleInfo& old) : module(old.module), params(old.params)
249  { }
250 
251  ModuleInfo& operator=(const ModuleInfo& old)
252  {
253  module = old.module;
254  params = old.params;
255  return *this;
256  }
257  };
258 
259  struct SubComponentInfo {
260  const ElementInfoSubComponent* subcomponent;
261  Params::KeySet_t params;
262  std::vector<std::string> statNames;
263  std::vector<std::string> statUnits;
264  std::vector<uint8_t> statEnableLevels;
265  std::vector<std::string> ports;
266  std::vector<std::string> subcomponentslots;
267 
268  SubComponentInfo() {}
269 
270  SubComponentInfo(const ElementInfoSubComponent *subcomponent,
271  Params::KeySet_t params) : subcomponent(subcomponent), params(params)
272  {
273  const ElementInfoStatistic *s = subcomponent->stats;
274  while ( NULL != s && NULL != s->name ) {
275  statNames.push_back(s->name);
276  statUnits.push_back(s->units);
277  statEnableLevels.push_back(s->enableLevel);
278  s++;
279  }
280 
281  const ElementInfoPort *p = subcomponent->ports;
282  while ( NULL != p && NULL != p->name ) {
283  ports.push_back(p->name);
284  p++;
285  }
286 
287  const ElementInfoSubComponentSlot *ss = subcomponent->subComponents;
288  while ( NULL != ss && NULL != ss->name ) {
289  subcomponentslots.push_back(ss->name);
290  ss++;
291  }
292  }
293 
294  SubComponentInfo(const SubComponentInfo& old) : subcomponent(old.subcomponent), params(old.params), statNames(old.statNames), statUnits(old.statUnits), statEnableLevels(old.statEnableLevels), subcomponentslots(old.subcomponentslots)
295  { }
296 
297  SubComponentInfo& operator=(const SubComponentInfo& old)
298  {
299  subcomponent = old.subcomponent;
300  params = old.params;
301  statNames = old.statNames;
302  statUnits = old.statUnits;
303  statEnableLevels = old.statEnableLevels;
304  subcomponentslots = subcomponentslots;
305  return *this;
306  }
307  };
308 
309  typedef std::map<std::string, const ElementLibraryInfo*> eli_map_t;
310  typedef std::map<std::string, ComponentInfo> eic_map_t;
311  typedef std::map<std::string, const ElementInfoEvent*> eie_map_t;
312  typedef std::map<std::string, ModuleInfo> eim_map_t;
313  typedef std::map<std::string, SubComponentInfo> eis_map_t;
314  typedef std::map<std::string, const ElementInfoPartitioner*> eip_map_t;
315  typedef std::map<std::string, const ElementInfoGenerator*> eig_map_t;
316 
317  Factory(std::string searchPaths);
318  ~Factory();
319 
320  Factory(); // Don't Implement
321  Factory(Factory const&); // Don't Implement
322  void operator=(Factory const&); // Don't Implement
323 
324  static Factory *instance;
325 
326  Params::KeySet_t create_params_set(const ElementInfoParam *params);
327 
328  // find library information for name
329  const ElementLibraryInfo* findLibrary(std::string name, bool showErrors=true);
330  // handle low-level loading of name
331  const ElementLibraryInfo* loadLibrary(std::string name, bool showErrors=true);
332 
333  eli_map_t loaded_libraries;
334  eic_map_t found_components;
335  eie_map_t found_events;
336  eim_map_t found_modules;
337  eis_map_t found_subcomponents;
338  eip_map_t found_partitioners;
339  eig_map_t found_generators;
340  std::string searchPaths;
341  ElemLoader *loader;
342  std::string loadingComponentType;
343 
344  std::pair<std::string, std::string> parseLoadName(const std::string& wholename);
345 
346  std::recursive_mutex factoryMutex;
347 
348 
349 protected:
350  Output &out;
351 };
352 
353 } // namespace SST
354 
355 #endif // SST_CORE_FACTORY_H
const ElementInfoStatistic * stats
Definition: element.h:98
Output object provides consistant method for outputing data to stdout, stderr and/or sst debug file...
Definition: output.h:54
Statistics::StatisticBase * CreateStatistic(BaseComponent *comp, const std::string &type, const std::string &statName, const std::string &statSubId, Params &params, Statistics::StatisticFieldInfo::fieldType_t fieldType)
Instatiate a new Statistic.
Definition: factory.cc:263
Module * CreateCoreModuleWithComponent(std::string type, Component *comp, Params &params)
Instantiate a new Module from within the SST core.
Definition: factory.cc:698
Component * CreateComponent(ComponentId_t id, std::string &componentname, Params &params)
Attempt to create a new Component instantiation.
Definition: factory.cc:164
generateFunction GetGenerator(std::string name)
Return generator function.
Definition: factory.cc:849
void RequireEvent(std::string eventname)
Ensure that an element library containing the required event is loaded.
Definition: factory.cc:798
Describes a Component and its associated information.
Definition: element.h:49
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:109
Definition: elibase.h:62
Forms the base class for statistics output generation within the SST core.
Definition: statoutput.h:47
Describes all the parts of the Element Library.
Definition: element.h:130
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:921
Statistics::StatisticOutput * CreateStatisticOutput(const std::string &statOutputType, const Params &statOutputParams)
Attempt to create a new Statistic Output instantiation.
Definition: factory.cc:221
Forms the base class for statistics gathering within SST.
Definition: statbase.h:61
bool DoesComponentInfoStatisticNameExist(const std::string &type, const std::string &statisticName)
Determine if a statistic is defined in a components ElementInfoStatistic.
Definition: factory.cc:363
Definition: action.cc:17
std::string GetComponentInfoStatisticUnits(const std::string &type, const std::string &statisticName)
Get the units of a statistic defined in the component's ElementInfoStatistic.
Definition: factory.cc:527
const ElementInfoStatistic * stats
Definition: element.h:57
Describes Statistics used by a Component.
Definition: elibase.h:31
Module is a tag class used with the loadModule function.
Definition: module.h:20
Module * CreateCoreModule(std::string type, Params &params)
Instantiate a new Module from within the SST core.
Definition: factory.cc:679
Module * CreateModuleWithComponent(std::string type, Component *comp, Params &params)
Instatiate a new Module.
Definition: factory.cc:705
Class to load Element Libraries.
Definition: elemLoader.h:24
uint8_t GetComponentInfoStatisticEnableLevel(const std::string &type, const std::string &statisticName)
Get the enable level of a statistic defined in the component's ElementInfoStatistic.
Definition: factory.cc:466
Partition::SSTPartitioner * CreatePartitioner(std::string name, RankInfo total_ranks, RankInfo my_rank, int verbosity)
Return partitioner function.
Definition: factory.cc:817
Definition: rankInfo.h:21
const char * name
Definition: elibase.h:32
Main component object for the simulation.
Definition: baseComponent.h:104
Describes Parameters to a Component.
Definition: elibase.h:40
Base class for Partitioning graphs.
Definition: sstpart.h:31
Describes a Module.
Definition: element.h:82
const ElementInfoPort * ports
Definition: element.h:55
Parameter store.
Definition: params.h:45
const ElementInfoPort * ports
Definition: element.h:100
SSTElementPythonModule * getPythonModule(std::string name)
Return Python Module creation function.
Definition: factory.cc:874
const char * name
Definition: elibase.h:49
const uint8_t enableLevel
Definition: elibase.h:35
Class for instantiating Components, Links and the like out of element libraries.
Definition: factory.h:44
Base class for python modules in element libraries.
Definition: element_python.h:26
std::set< key_type, KeyCompare > KeySet_t
Definition: params.h:94
bool DoesSubComponentInfoStatisticNameExist(const std::string &type, const std::string &statisticName)
Determine if a statistic is defined in a subcomponents ElementInfoStatistic.
Definition: factory.cc:415
SubComponent * CreateSubComponent(std::string type, Component *comp, Params &params)
Instatiate a new Module.
Definition: factory.cc:753
Describes Ports that the Component can use.
Definition: elibase.h:48
Definition: element.h:92
SubComponent is a class loadable through the factory which allows dynamic functionality to be added t...
Definition: subcomponent.h:29
const char * units
Definition: elibase.h:34
fieldType_t
Supported Field Types.
Definition: statfieldinfo.h:36
bool DoesSubComponentSlotExist(const std::string &type, const std::string &slotName)
Determine if a SubComponentSlot is defined in a components ElementInfoStatistic.
Definition: factory.cc:300
Module * CreateModule(std::string type, Params &params)
Instatiate a new Module.
Definition: factory.cc:586