00001 // Implementation of the IAASim Simulator 00002 // George F. Riley, Georgia Tech, Summer 2009 00003 00004 #include "simulator.h" 00005 00006 int EventBase::nextUID = 0; 00007 EventSet_t Simulator::events; 00008 bool Simulator::halted = false; 00009 double Simulator::simTime; 00010 int Simulator::rank; 00011 int Simulator::nextComponentID; 00012 ComponentMap_t Simulator::components; 00013 00014 // Event0Stat is not a template, so we implement it here 00015 void Event0Stat::CallHandler() 00016 { 00017 handler(); 00018 } 00019 00020 00021 void Simulator::Run() 00022 { 00023 rank=0; 00024 while(!events.empty() && !halted) 00025 { 00026 EventSet_t::iterator i = events.begin(); 00027 EventBase* ev = *i; // Get the event 00028 // Set the simulation time 00029 simTime = ev->time; 00030 // Call the event handler 00031 cout.flush(); 00032 ev->CallHandler(); 00033 // Remove the event from the pending list 00034 events.erase(i); 00035 // And delete the event 00036 delete ev; 00037 } 00038 00039 } 00040 00041 void Simulator::Stop() 00042 { 00043 halted = true; 00044 } 00045 00046 void Simulator::StopAt(double stopTime) 00047 { // code later 00048 Schedule(stopTime, &Simulator::Stop); 00049 } 00050 00051 double Simulator::Now() 00052 { 00053 return simTime; 00054 } 00055 00056 bool Simulator::Cancel(EventId& evid) 00057 { 00058 EventSet_t::iterator it = events.find(&evid); 00059 if (it == events.end()) return false; // Not found 00060 events.erase(it); // Otherwise erase it 00061 return true; 00062 } 00063 00064 EventId Simulator::Peek() 00065 { // Return eventid for earliest event, but do not remove it 00066 // Event list must not be empty 00067 EventSet_t::iterator it = events.begin(); 00068 return EventId((*it)->time, (*it)->uid); 00069 } 00070 00071 EventBase* Simulator::GetEarliestEvent() 00072 { 00073 EventSet_t::iterator it = events.begin(); 00074 if (it == events.end()) return 0; 00075 return *it; 00076 } 00077 00078 int Simulator::MyRank() 00079 { 00080 return rank; 00081 00082 } 00083 00084 00085 void Simulator::registerComponent(Component* obj, int lp) 00086 { 00087 ComponentDescription* compDesc; 00088 if(lp==rank) 00089 { 00090 compDesc = new ComponentDescription(lp, obj); 00091 } 00092 else 00093 { 00094 compDesc = new ComponentDescription(lp, NULL); 00095 } 00096 components[nextComponentID]=compDesc; 00097 obj->setComponentId(nextComponentID); 00098 nextComponentID++; 00099 } 00100 00101 ComponentDescription* Simulator::getComponentDesc(int compId) 00102 { 00103 return components[compId]; 00104 }