00001 // Copyright 2009-2015 Sandia Corporation. Under the terms 00002 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. 00003 // Government retains certain rights in this software. 00004 // 00005 // Copyright (c) 2009-2015, Sandia Corporation 00006 // All rights reserved. 00007 // 00008 // This file is part of the SST software package. For license 00009 // information, see the LICENSE file in the top level directory of the 00010 // distribution. 00011 00012 00013 #ifndef SST_CORE_SUBCOMPONENT_H 00014 #define SST_CORE_SUBCOMPONENT_H 00015 00016 #include <sst/core/component.h> 00017 00018 namespace SST { 00019 00020 00021 /** 00022 SubComponent is a class loadable through the factory which allows 00023 dynamic functionality to be added to a Component. The 00024 SubComponent API is nearly identical to the Component API and all 00025 the calls are proxied to the parent Compoent. 00026 */ 00027 class SubComponent : public Module { 00028 00029 public: 00030 SubComponent(Component* parent); 00031 virtual ~SubComponent(); 00032 00033 /** Used during the init phase. The method will be called each phase of initialization. 00034 Initialization ends when no components have sent any data. */ 00035 virtual void init(unsigned int phase) {} 00036 /** Called after all components have been constructed and inialization has 00037 completed, but before simulation time has begun. */ 00038 virtual void setup( ) { } 00039 /** Called after simulation completes, but before objects are 00040 destroyed. A good place to print out statistics. */ 00041 virtual void finish( ) { } 00042 00043 protected: 00044 Component* const parent; 00045 // Component* parent; 00046 00047 /** Configure a Link 00048 * @param name - Port Name on which the link to configure is attached. 00049 * @param time_base - Time Base of the link 00050 * @param handler - Optional Handler to be called when an Event is received 00051 * @return A pointer to the configured link, or NULL if an error occured. 00052 */ 00053 Link* configureLink( std::string name, TimeConverter* time_base, Event::HandlerBase* handler = NULL); 00054 /** Configure a Link 00055 * @param name - Port Name on which the link to configure is attached. 00056 * @param time_base - Time Base of the link 00057 * @param handler - Optional Handler to be called when an Event is received 00058 * @return A pointer to the configured link, or NULL if an error occured. 00059 */ 00060 Link* configureLink( std::string name, std::string time_base, Event::HandlerBase* handler = NULL); 00061 /** Configure a Link 00062 * @param name - Port Name on which the link to configure is attached. 00063 * @param handler - Optional Handler to be called when an Event is received 00064 * @return A pointer to the configured link, or NULL if an error occured. 00065 */ 00066 Link* configureLink( std::string name, Event::HandlerBase* handler = NULL); 00067 00068 /** Configure a SelfLink (Loopback link) 00069 * @param name - Name of the self-link port 00070 * @param time_base - Time Base of the link 00071 * @param handler - Optional Handler to be called when an Event is received 00072 * @return A pointer to the configured link, or NULL if an error occured. 00073 */ 00074 Link* configureSelfLink( std::string name, TimeConverter* time_base, Event::HandlerBase* handler = NULL); 00075 /** Configure a SelfLink (Loopback link) 00076 * @param name - Name of the self-link port 00077 * @param time_base - Time Base of the link 00078 * @param handler - Optional Handler to be called when an Event is received 00079 * @return A pointer to the configured link, or NULL if an error occured. 00080 */ 00081 Link* configureSelfLink( std::string name, std::string time_base, Event::HandlerBase* handler = NULL); 00082 /** Configure a SelfLink (Loopback link) 00083 * @param name - Name of the self-link port 00084 * @param handler - Optional Handler to be called when an Event is received 00085 * @return A pointer to the configured link, or NULL if an error occured. 00086 */ 00087 Link* configureSelfLink( std::string name, Event::HandlerBase* handler = NULL); 00088 00089 bool doesSubComponentInfoStatisticExist(std::string statisticName); 00090 00091 template <typename T> 00092 Statistic<T>* registerStatistic(std::string statName, std::string statSubId = "") 00093 { 00094 // Verify here that name of the stat is one of the registered 00095 // names of the component's ElementInfoStatistic. 00096 if (false == doesSubComponentInfoStatisticExist(statName)) { 00097 printf("Error: Statistic %s name %s is not found in ElementInfoStatistic, exiting...\n", 00098 StatisticBase::buildStatisticFullName(parent->getName().c_str(), statName, statSubId). 00099 c_str(), 00100 statName.c_str()); 00101 exit(1); 00102 } 00103 return parent->registerStatisticCore<T>(statName, statSubId); 00104 } 00105 00106 /** Registers a clock for this component. 00107 @param freq Frequency for the clock in SI units 00108 @param handler Pointer to Clock::HandlerBase which is to be invoked 00109 at the specified interval 00110 NOTE: Unlike Components, SubComponents do not have a default timebase 00111 */ 00112 TimeConverter* registerClock( std::string freq, Clock::HandlerBase* handler); 00113 TimeConverter* registerClock( const UnitAlgebra& freq, Clock::HandlerBase* handler); 00114 00115 /** Removes a clock handler from the component */ 00116 void unregisterClock(TimeConverter *tc, Clock::HandlerBase* handler); 00117 00118 /** Reactivates an existing Clock and Handler 00119 * @return time of next time clock handler will fire 00120 */ 00121 Cycle_t reregisterClock(TimeConverter *freq, Clock::HandlerBase* handler); 00122 /** Returns the next Cycle that the TimeConverter would fire */ 00123 Cycle_t getNextClockCycle(TimeConverter *freq); 00124 00125 // For now, no OneShot support in SubComponent 00126 #if 0 00127 /** Registers a OneShot event for this component. 00128 Note: OneShot cannot be canceled, and will always callback after 00129 the timedelay. 00130 @param timeDelay Time delay for the OneShot in SI units 00131 @param handler Pointer to OneShot::HandlerBase which is to be invoked 00132 at the specified interval 00133 */ 00134 TimeConverter* registerOneShot( std::string timeDelay, OneShot::HandlerBase* handler); 00135 TimeConverter* registerOneShot( const UnitAlgebra& timeDelay, OneShot::HandlerBase* handler); 00136 #endif 00137 00138 TimeConverter* getTimeConverter( const std::string& base ); 00139 TimeConverter* getTimeConverter( const UnitAlgebra& base ); 00140 00141 /** return the time since the simulation began in units specified by 00142 the parameter. 00143 @param tc TimeConverter specificing the units */ 00144 SimTime_t getCurrentSimTime(TimeConverter *tc) const; 00145 00146 /** return the time since the simulation began in timebase specified 00147 @param base Timebase frequency in SI Units */ 00148 SimTime_t getCurrentSimTime(std::string base); 00149 00150 /** Utility function to return the time since the simulation began in nanoseconds */ 00151 SimTime_t getCurrentSimTimeNano() const; 00152 /** Utility function to return the time since the simulation began in microseconds */ 00153 SimTime_t getCurrentSimTimeMicro() const; 00154 /** Utility function to return the time since the simulation began in milliseconds */ 00155 SimTime_t getCurrentSimTimeMilli() const; 00156 00157 Module* loadModule(std::string type, Params& params); 00158 Module* loadModuleWithComponent(std::string type, Params& params); 00159 SubComponent* loadSubComponent(std::string type, Params& params); 00160 00161 00162 /** Find a lookup table */ 00163 SharedRegion* getLocalSharedRegion(const std::string &key, size_t size); 00164 SharedRegion* getGlobalSharedRegion(const std::string &key, size_t size, SharedRegionMerger *merger = NULL); 00165 00166 00167 private: 00168 /** Component's type, set by the factory when the object is created. 00169 It is identical to the configuration string used to create the 00170 component. I.e. the XML "<component id="aFoo"><foo>..." would 00171 set component::type to "foo" */ 00172 friend class Component; 00173 std::string type; 00174 SubComponent(); 00175 // friend class boost::serialization::access; 00176 // template<class Archive> 00177 // void 00178 // serialize(Archive & ar, const unsigned int version ) 00179 // { 00180 // } 00181 }; 00182 } //namespace SST 00183 00184 // BOOST_CLASS_EXPORT_KEY(SST::SubComponent) 00185 00186 #endif // SST_CORE_SUBCOMPONENT_H