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