SST  6.0.0
StructuralSimulationToolkit
subcomponent.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 
13 #ifndef SST_CORE_SUBCOMPONENT_H
14 #define SST_CORE_SUBCOMPONENT_H
15 
16 #include <sst/core/component.h>
17 
18 namespace SST {
19 
20 
21 /**
22  SubComponent is a class loadable through the factory which allows
23  dynamic functionality to be added to a Component. The
24  SubComponent API is nearly identical to the Component API and all
25  the calls are proxied to the parent Compoent.
26 */
27 class SubComponent : public Module {
28 
29 public:
30  SubComponent(Component* parent);
31  virtual ~SubComponent();
32 
33  /** Used during the init phase. The method will be called each phase of initialization.
34  Initialization ends when no components have sent any data. */
35  virtual void init(unsigned int phase) {}
36  /** Called after all components have been constructed and inialization has
37  completed, but before simulation time has begun. */
38  virtual void setup( ) { }
39  /** Called after simulation completes, but before objects are
40  destroyed. A good place to print out statistics. */
41  virtual void finish( ) { }
42 
43 protected:
44  Component* const parent;
45  // Component* parent;
46 
47  /** Determine if a port name is connected to any links */
48  bool isPortConnected(const std::string &name) const;
49 
50  /** Configure a Link
51  * @param name - Port Name on which the link to configure is attached.
52  * @param time_base - Time Base of the link
53  * @param handler - Optional Handler to be called when an Event is received
54  * @return A pointer to the configured link, or NULL if an error occured.
55  */
56  Link* configureLink( std::string name, TimeConverter* time_base, Event::HandlerBase* handler = NULL);
57  /** Configure a Link
58  * @param name - Port Name on which the link to configure is attached.
59  * @param time_base - Time Base of the link
60  * @param handler - Optional Handler to be called when an Event is received
61  * @return A pointer to the configured link, or NULL if an error occured.
62  */
63  Link* configureLink( std::string name, std::string time_base, Event::HandlerBase* handler = NULL);
64  /** Configure a Link
65  * @param name - Port Name on which the link to configure is attached.
66  * @param handler - Optional Handler to be called when an Event is received
67  * @return A pointer to the configured link, or NULL if an error occured.
68  */
69  Link* configureLink( std::string name, Event::HandlerBase* handler = NULL);
70 
71  /** Configure a SelfLink (Loopback link)
72  * @param name - Name of the self-link port
73  * @param time_base - Time Base of the link
74  * @param handler - Optional Handler to be called when an Event is received
75  * @return A pointer to the configured link, or NULL if an error occured.
76  */
77  Link* configureSelfLink( std::string name, TimeConverter* time_base, Event::HandlerBase* handler = NULL);
78  /** Configure a SelfLink (Loopback link)
79  * @param name - Name of the self-link port
80  * @param time_base - Time Base of the link
81  * @param handler - Optional Handler to be called when an Event is received
82  * @return A pointer to the configured link, or NULL if an error occured.
83  */
84  Link* configureSelfLink( std::string name, std::string time_base, Event::HandlerBase* handler = NULL);
85  /** Configure a SelfLink (Loopback link)
86  * @param name - Name of the self-link port
87  * @param handler - Optional Handler to be called when an Event is received
88  * @return A pointer to the configured link, or NULL if an error occured.
89  */
90  Link* configureSelfLink( std::string name, Event::HandlerBase* handler = NULL);
91 
92  bool doesSubComponentInfoStatisticExist(std::string statisticName);
93 
94  template <typename T>
95  Statistic<T>* registerStatistic(std::string statName, std::string statSubId = "")
96  {
97  // Verify here that name of the stat is one of the registered
98  // names of the component's ElementInfoStatistic.
99  if (false == doesSubComponentInfoStatisticExist(statName)) {
100  printf("Error: Statistic %s name %s is not found in ElementInfoStatistic, exiting...\n",
101  StatisticBase::buildStatisticFullName(parent->getName().c_str(), statName, statSubId).
102  c_str(),
103  statName.c_str());
104  exit(1);
105  }
106  return parent->registerStatisticCore<T>(statName, statSubId);
107  }
108 
109  /** Registers a clock for this component.
110  @param freq Frequency for the clock in SI units
111  @param handler Pointer to Clock::HandlerBase which is to be invoked
112  at the specified interval
113  NOTE: Unlike Components, SubComponents do not have a default timebase
114  */
115  TimeConverter* registerClock( std::string freq, Clock::HandlerBase* handler);
116  TimeConverter* registerClock( const UnitAlgebra& freq, Clock::HandlerBase* handler);
117 
118  /** Removes a clock handler from the component */
119  void unregisterClock(TimeConverter *tc, Clock::HandlerBase* handler);
120 
121  /** Reactivates an existing Clock and Handler
122  * @return time of next time clock handler will fire
123  */
124  Cycle_t reregisterClock(TimeConverter *freq, Clock::HandlerBase* handler);
125  /** Returns the next Cycle that the TimeConverter would fire */
126  Cycle_t getNextClockCycle(TimeConverter *freq);
127 
128  // For now, no OneShot support in SubComponent
129 #if 0
130  /** Registers a OneShot event for this component.
131  Note: OneShot cannot be canceled, and will always callback after
132  the timedelay.
133  @param timeDelay Time delay for the OneShot in SI units
134  @param handler Pointer to OneShot::HandlerBase which is to be invoked
135  at the specified interval
136  */
137  TimeConverter* registerOneShot( std::string timeDelay, OneShot::HandlerBase* handler);
138  TimeConverter* registerOneShot( const UnitAlgebra& timeDelay, OneShot::HandlerBase* handler);
139 #endif
140 
141  TimeConverter* getTimeConverter( const std::string& base );
142  TimeConverter* getTimeConverter( const UnitAlgebra& base );
143 
144  /** return the time since the simulation began in units specified by
145  the parameter.
146  @param tc TimeConverter specificing the units */
147  SimTime_t getCurrentSimTime(TimeConverter *tc) const;
148 
149  /** return the time since the simulation began in timebase specified
150  @param base Timebase frequency in SI Units */
151  SimTime_t getCurrentSimTime(std::string base);
152 
153  /** Utility function to return the time since the simulation began in nanoseconds */
154  SimTime_t getCurrentSimTimeNano() const;
155  /** Utility function to return the time since the simulation began in microseconds */
156  SimTime_t getCurrentSimTimeMicro() const;
157  /** Utility function to return the time since the simulation began in milliseconds */
158  SimTime_t getCurrentSimTimeMilli() const;
159 
160  Module* loadModule(std::string type, Params& params);
161  Module* loadModuleWithComponent(std::string type, Params& params);
162  SubComponent* loadSubComponent(std::string type, Params& params);
163 
164 
165  /** Find a lookup table */
166  SharedRegion* getLocalSharedRegion(const std::string &key, size_t size);
167  SharedRegion* getGlobalSharedRegion(const std::string &key, size_t size, SharedRegionMerger *merger = NULL);
168 
169 
170 private:
171  /** Component's type, set by the factory when the object is created.
172  It is identical to the configuration string used to create the
173  component. I.e. the XML "<component id="aFoo"><foo>..." would
174  set component::type to "foo" */
175  friend class Component;
176  std::string type;
177  SubComponent();
178  };
179 } //namespace SST
180 
181 // BOOST_CLASS_EXPORT_KEY(SST::SubComponent)
182 
183 #endif // SST_CORE_SUBCOMPONENT_H
virtual void finish()
Called after simulation completes, but before objects are destroyed.
Definition: subcomponent.h:41
SimTime_t getCurrentSimTimeMilli() const
Utility function to return the time since the simulation began in milliseconds.
Definition: subcomponent.cc:158
virtual void init(unsigned int phase)
Used during the init phase.
Definition: subcomponent.h:35
A class to convert between a component's view of time and the core's view of time.
Definition: timeConverter.h:25
Main component object for the simulation.
Definition: component.h:56
Cycle_t reregisterClock(TimeConverter *freq, Clock::HandlerBase *handler)
Reactivates an existing Clock and Handler.
Definition: subcomponent.cc:108
SimTime_t getCurrentSimTimeMicro() const
Utility function to return the time since the simulation began in microseconds.
Definition: subcomponent.cc:154
Definition: action.cc:17
SimTime_t getCurrentSimTime(TimeConverter *tc) const
return the time since the simulation began in units specified by the parameter.
Definition: subcomponent.cc:141
Forms the template defined base class for statistics gathering within SST.
Definition: statbase.h:263
bool isPortConnected(const std::string &name) const
Determine if a port name is connected to any links.
Definition: subcomponent.cc:48
Cycle_t getNextClockCycle(TimeConverter *freq)
Returns the next Cycle that the TimeConverter would fire.
Definition: subcomponent.cc:112
TimeConverter * registerClock(std::string freq, Clock::HandlerBase *handler)
Registers a clock for this component.
Definition: subcomponent.cc:96
Module is a tag class used with the loadModule function.
Definition: module.h:20
Link * configureLink(std::string name, TimeConverter *time_base, Event::HandlerBase *handler=NULL)
Configure a Link.
Definition: subcomponent.cc:54
SharedRegion * getLocalSharedRegion(const std::string &key, size_t size)
Find a lookup table.
Definition: subcomponent.cc:182
Statistic< T > * registerStatisticCore(std::string statName, std::string statSubId="")
Registers a statistic.
Definition: component.h:234
SimTime_t getCurrentSimTimeNano() const
Utility function to return the time since the simulation began in nanoseconds.
Definition: subcomponent.cc:150
void unregisterClock(TimeConverter *tc, Clock::HandlerBase *handler)
Removes a clock handler from the component.
Definition: subcomponent.cc:104
const std::string & getName() const
Returns component Name.
Definition: component.h:80
Functor classes for Event handling.
Definition: event.h:86
Link * configureSelfLink(std::string name, TimeConverter *time_base, Event::HandlerBase *handler=NULL)
Configure a SelfLink (Loopback link)
Definition: subcomponent.cc:72
virtual void setup()
Called after all components have been constructed and inialization has completed, but before simulati...
Definition: subcomponent.h:38
SubComponent is a class loadable through the factory which allows dynamic functionality to be added t...
Definition: subcomponent.h:27