00001 // Copyright 2009-2010 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-2010, 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_COMPONENT_H 00014 #define SST_COMPONENT_H 00015 00016 #include <map> 00017 00018 #include <sst/core/sst_types.h> 00019 #include <sst/core/linkMap.h> 00020 #include <sst/core/clock.h> 00021 #include <sst/core/timeConverter.h> 00022 00023 namespace SST { 00024 00025 #define _COMP_DBG( fmt, args...) __DBG( DBG_COMP, Component, fmt, ## args ) 00026 00027 /** 00028 * Main component object for the simulation. 00029 * All models inherit from this. 00030 */ 00031 class Component { 00032 public: 00033 typedef std::map<std::string,std::string> Params_t; 00034 00035 /** Constructor. Generally only called by the factory class. 00036 @param id Unique component ID 00037 */ 00038 Component( ComponentId_t id ); 00039 virtual ~Component() {} 00040 00041 /** Returns unique component ID */ 00042 inline ComponentId_t getId() const { return id; } 00043 00044 /** Component's type, set by the factory when the object is created. 00045 It is identical to the configuration string used to create the 00046 component. I.e. the XML "<component id="aFoo"><foo>..." would 00047 set component::type to "foo" */ 00048 std::string type; 00049 00050 /** Called after all components have been constructed, but before 00051 simulation time has begun. */ 00052 virtual int Setup( ) { return 0; } 00053 /** Called after simulation completes, but before objects are 00054 destroyed. A good place to print out statistics. */ 00055 virtual int Finish( ) { return 0; } 00056 00057 virtual bool Status( ) { return 0; } 00058 00059 Link* configureLink( std::string name, TimeConverter* time_base, Event::HandlerBase* handler = NULL); 00060 Link* configureLink( std::string name, std::string time_base, Event::HandlerBase* handler = NULL); 00061 Link* configureLink( std::string name, Event::HandlerBase* handler = NULL); 00062 00063 Link* configureSelfLink( std::string name, TimeConverter* time_base, Event::HandlerBase* handler = NULL); 00064 Link* configureSelfLink( std::string name, std::string time_base, Event::HandlerBase* handler = NULL); 00065 Link* configureSelfLink( std::string name, Event::HandlerBase* handler = NULL); 00066 00067 /** Registers a clock for this component. 00068 @param freq Frequency for the clock in SI units 00069 @param handler Pointer to Clock::HandlerBase which is to be invoked 00070 at the specified interval 00071 @param regAll Should this clock perioud be used as the default 00072 time base for all of the links connected to this component 00073 */ 00074 TimeConverter* registerClock( std::string freq, Clock::HandlerBase* handler, 00075 bool regAll = true); 00076 void unregisterClock(TimeConverter *tc, Clock::HandlerBase* handler); 00077 00078 /** Registers a default time base for the component and optionally 00079 sets the the component's links to that timebase. Useful for 00080 components which do not have a clock, but would like a default 00081 timebase. 00082 @params base Frequency for the clock in SI units 00083 @params regAll Should this clock perioud be used as the default 00084 time base for all of the links connected to this component 00085 */ 00086 TimeConverter* registerTimeBase( std::string base, bool regAll = true); 00087 00088 /** return the time since the simulation began in units specified by 00089 the parameter. 00090 @param tc TimeConverter specificing the units */ 00091 SimTime_t getCurrentSimTime(TimeConverter *tc); 00092 /** return the time since the simulation began in the default timebase */ 00093 SimTime_t getCurrentSimTime() { 00094 return getCurrentSimTime(defaultTimeBase); 00095 } 00096 /** return the time since the simulation began in timebase specified 00097 @param base Timebase frequency in SI Units */ 00098 SimTime_t getCurrentSimTime(std::string base); 00099 00100 /** Utility function to return the time since the simulation began in nanoseconds */ 00101 SimTime_t getCurrentSimTimeNano(); 00102 /** Utility function to return the time since the simulation began in microseconds */ 00103 SimTime_t getCurrentSimTimeMicro(); 00104 /** Utility function to return the time since the simulation began in milliseconds */ 00105 SimTime_t getCurrentSimTimeMilli(); 00106 00107 /** Register that the simulation should not end until this component 00108 says it is OK to. Calling this function (generally done in 00109 Component::Setup()) increments a global counter. Calls to 00110 Component::unregisterExit() decrement the counter. The 00111 simulation cannot end unless this counter reaches zero, or the 00112 simulation time limit is reached. This counter is synchonized 00113 periodically with the other nodes. 00114 00115 @sa Component::unregisterExit() 00116 */ 00117 bool registerExit(); 00118 00119 /** Indicate permission for the simulation to end. This function is 00120 the mirror of Component::registerExit(). It decrements the 00121 global counter, which, upon reaching zero, indicates that the 00122 simulation can terminate. @sa Component::registerExit() */ 00123 bool unregisterExit(); 00124 00125 protected: 00126 Component(); // For serialization only 00127 00128 /** Manually set the default detaulTimeBase */ 00129 void setDefaultTimeBase(TimeConverter *tc) { 00130 defaultTimeBase = tc; 00131 } 00132 00133 /** Timebase used if no other timebase is specified for calls like 00134 Component::getCurrentSimTime(). Often set by Component::registerClock() 00135 function */ 00136 TimeConverter* defaultTimeBase; 00137 00138 Link* selfLink( std::string name, Event::HandlerBase* handler = NULL ); 00139 00140 private: 00141 00142 void addSelfLink(std::string name); 00143 00144 /** Unique ID */ 00145 ComponentId_t id; 00146 LinkMap* myLinks; 00147 00148 friend class boost::serialization::access; 00149 template<class Archive> 00150 void serialize(Archive& ar, const unsigned int version); 00151 }; 00152 00153 } 00154 00155 BOOST_CLASS_EXPORT_KEY(SST::Component) 00156 00157 #endif