dept. computer science, tianjin uni. 系统仿真 system simulation 2008

48
Dept. Computer Science, Tianjin Uni. 系系 System Simulation 2008

Upload: michael-wilkins

Post on 13-Jan-2016

305 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Dept. Computer Science, Tianjin Uni.

系统仿真 System Simulation

2008

Page 2: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

What’s simulation

A simulation is The imitation of the operation of a real-world process or system over time.

A technique, whereby a model of a system, is run in compressed time, to perform experimentation for analyzing system performance.

Whether done by hand or on a computer, simulation involves the generation of an artificial history of a system, and the observation of that artificial history to draw inferences concerning the operating characteristics of the real system.

Page 3: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Discrete system simulation

Page 4: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Simulation Category

General-purpose programming languages FORTRAN, C, C++ “build your own, use someone

else’s“Simulation programming languages ns-2, QualNet/GloMoSim, Opnet, RTSS GPSS/H, SIMAN V

Simulation technology Time-driven simulation

They work on a strictly unit-time basis automatically scanning thousands of fixed

time units to discover the next significant change

Event-driven simulation automatically skip to the next known event saving execute-time

Page 5: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Use Queuing models to Describe the behavior of queuing

systems Evaluate system performance

Model Queuing System

Queuing System

Queue Server

Customers

Page 6: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Time

Time

Arrival event

Delay

Begin service

Begin service

Arrival event

Delay

Activity

Activity

End service

End service

Customer n+1

Customer n

Interarrival

Page 7: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

example

Single-server queue simulation Mean Interarrival time 4.5 Mean Service Time 3.2 Standard Deviation of Service Times 0.6 Number of Customers served 1000 Server Utilization 0.68362

Maximum Line Length 9Average Response Time 6.739Proportion who Spend Four Minutes or More in System 0.663Simulation RunLength 4677.74Number of Departures 1000

Page 8: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

RTSS Simulator -- A model contains two parts

First, a set of processors The simulation software stores the

processor parameters, such as service rate etc., in a set of arrays – “the processor table”.

Each processor has his own queue to store waiting jobs.

Second, the characterization of the workload (i.e. jobs) The job parameters, such as job arriving

time, job length of a job etc., are stored in a set of arrays - ”the job table”.

The job parameters can be either deterministic or probabilistic.

Page 9: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

RTSS Simulator

Page 10: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

events

All events can be divided into three types Type 1 marks the arrival of a job in the system Type 2 corresponds to a job’s entering a server Type 3 corresponds to a job’s exiting from a

serverThe event service routines are the arrival routine, the incoming routine and the outgoing routine There is an event list to drive the simulation processEach event has 3 elements in the event list One element is an event identifier which is used

for identifying the event type. The second element is the event time The third element is a pointer to the job table

Page 11: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

control routine

The control routine is the heart of the simulator, its loop is executed once for each event handledAt the start of each loop, the control routine selects the earliest event in the event list calls the corresponding event service routine advances the simulation clock to the event

time, removes the event from the event listThe event service routine performs the required processing for the job determines its next event, calculates the event

time and inserts the next event in the event list

returns control to the control routine

Page 12: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

arrival routine

the arrival routine corresponds to event type 1, and does the following things:1. The next event (type 2) for the arriving job is inserted in the event list. 2. If the arriving job belongs to a job sequence and it is not the last job of the sequence, a new job of the sequence is generated and inserted in the job table. 3. The event (type 1) for the new job is inserted in the event list.Random-variant generators Routines to generate samples from

desired probability distributions

Page 13: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

incoming routine

The incoming routine corresponds to event type 2, and simulates a job entering a processor; it does the following things:1. If the processor is busy, the input job enters a processor queue.2. If the processor is not busy, it serves the input job. The next event (type 3) to occur for the input job is inserted in the event list.3. The states of the job and processor are updatedRandom-variant generators Routines to generate samples from

desired probability distributions

Page 14: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

outgoing routine

The outgoing routine corresponds to event type 3, and simulates a job exiting from a processor; it does the following things:1. The processor is released by the output job,2. Then, the queue of the released processor is examined; if there are jobs waiting, the one which has the highest priority is selected from the queue and scheduled to be served

Page 15: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

report routine

A group of variables describes the simulator state when the simulation clock is advanced by the occurrence of a series of events. The report routine records these variables, so that analysis and statistic programs give the final simulation result.

Page 16: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Simulation in C++

Main program Provides overall control of the

event-scheduling algorithmClock

A variable defining simulated time

Initialization subroutine A routine to define the system state

at time 0Min-time event subroutine A routine that identifies the

imminent event, FEL

Page 17: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Simulation in C++

Event subroutines For each event type, a subroutine to

update system state (and cumulative statistics) when that event occurs

Random-variant generators Routines to generate samples from

desired probability distributionsReport generator A routine that computers summary

statistics from cumulative statistics and prints a report at the end of the simulation

Page 18: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Start simulationObtain input parameters

Initialize subroutine:

Call the time-advancesubroutine

A

B

Page 19: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Call appropriateevent subroutine

Simulationover?

Reportgenerator

A

B

Yes

No

Page 20: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Initialize subroutine:1. Set CLOCK = 02. Set cumulative statistics to 03. Generate initial events andplace on FEL4. Define initial system state

Page 21: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Time-advance subroutine:1. Find imminent event, say i2. Advance CLOCK to imminentevent time

Page 22: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Event subroutine i :1. Execute event i : updatesystem state, entity2. Collect cumulative statistics3. Generate future events andplace on FEL

Page 23: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Report generator:1. Compute summarystatistics2. print report

Page 24: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

example

Single-server queue simulation

Page 25: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

main() programStart simulation

Call Initialization()Initialize the model

Remove Imminent eventfrom FutureEventList

Advance time to event time

A

B

Page 26: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Call event routinebased on event type

Simulation over?

Call ReportGeneration()Generate final report

A

B

Yes

No

Page 27: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Definitions tab. 4.6, p. 108

Variables System state

QueueLengthNumberInService

Entity attributes and setsCustomers

Page 28: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Variables(cont’d)

Future event listFutureEventList

Activity durationService time

Page 29: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Variables(cont’d)

Input ParametersMeanInterarrivalTimeMeanServiceTimeSIGMA, Standard deviation of service

timeTotalCustomers, stopping criteria

Page 30: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Variables(cont’d)

Simulation variablesClock

Statistical AccumulatorsLastEventTimeTotalBusyMaxQueueLength

Page 31: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Statistical Accumulators

SumResponseTimeNumberOfDeparturesLongService, Number of customers

who spend 4 or more minutes

Page 32: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Variables(cont’d)

Summary statistics RHO = BusyTime/Clock AVGR : average response time PC4 : Proportion of customers who

spend 4 or more minutes at the checkout counter

Page 33: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Functions

exponential ( mu )normal ( xmu, SIGMA )

Page 34: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Subroutines

InitializationProcessArrivalProcessDepartureReportGeneration

Page 35: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Class Event{Friend bool operator < (const Event& e1,

const Event& e2);Friend bool operator == (const Event& e1,

const Event& e2);public: Event(){};enum EvtType { arrival, departure };

Page 36: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Event ( EvtType type, double etime) : _type(type), _etime(etime){};

EvtType get_type() { return _type;} double get_time() { return _etime;}protected: EvtType _type; double _etime;};

Page 37: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

……priority_queue<Event> FutureEventListQueue<Event> Customers

Page 38: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Main( int argc, char* argv[ ] ){MeanInterArrivalTime = 4.5……Long seed = atoi ( argv[1] );SetSeed(seed);Initialization();

Page 39: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

While ( NumberOfDeparture < TotalCustomers ){

Event evt = FutureEventList.top();FutureEventList.pop();Clock = evt.get_time();If ( evt.get_type() == Event::arrival )

ProcessArrival ( evt );Else ProcessDeparture ( evt );}ReportGeneration();}

Page 40: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Void Initialization (){ Clock = 0;QueueLength = 0;……Event evt( Event::arrival, exponential

( MeanInterArrivalTime ) );FutureEventList.push ( evt );}

Page 41: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Void ProcessArrival( Event evt ){Customer.push ( evt );QueueLength ++;If ( NumberInService == 0 )

ScheduleDeparture ();Else TotalBusy += ( Clock – LastEventTime );If ( MaxQueueLength < QueueLength )

MaxQueueLength = QueueLength;

Page 42: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Event next_arrival ( Event::arrival, Clock + exponential ( MeanInterArrivalTime )

); FutureEventList.push ( next_arrival ); LastEventTime = Clock;}

Page 43: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Void scheduleDeparture () {double ServiceTime;while( ( ServiceTime = normal ( MeanServiceTime, SIGMA ))

< 0 );Event depart ( Event::departure, Clock + ServiceTime );FutureEventList.push ( depart );NumberInservice = 1;QueueLength --;

}

Page 44: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Void ProcessDeparture ( Evevt evt) {Event finished = Customers.front ();Customers.pop ();if ( QueueLength ) ScheduleDeparture ();else NumberInService = 0;

Page 45: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

double response = Clock – finished.get_time ();

SumResponseTime + = response; …… NumberOfDepartures ++; LastEventTime = Clock;}

Page 46: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Void ReportGeneration (){ double RHO = TotalBusy/Clock; double AVGR = SumResponseTime/

TotalCustomers; double PC4 = ((double)LongService)/

TotalCustomers;……}

Page 47: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Mean Interarrival time 4.5Mean Service Time 3.2Standard Deviation of Service Times

0.6Number of Customers served

1000Server Utilization 0.68362Maximum Line Length 9Average Response Time 6.739

Page 48: Dept. Computer Science, Tianjin Uni. 系统仿真 System Simulation 2008

Proportion who Spend Four Minutes or More in System

0.663Simulation RunLength 4677.74Number of Departures 1000