Professional Documents
Culture Documents
Emulation
Lecture #36-40
Simulation Modeling ▪ An emulator program is a simulation program
that runs on some existing system to make that
system appear to be something else.
Dr Syed Zaffar Qasim
Assistant Professor (CIS) ▪ Sacrifices performance for flexibility.
CSM: CS-438 1
Lecture 37
Static (Monte Carlo) Simulation Example Static (Monte Carlo) Simulation Example
▪ Consider the problem of numerically determining the ▪ Thus, the ratio of the area of the quarter-circle to the
value of . area of the square, which we denote R, is R=/4.
▪ We begin with a geometric description as shown in ▪ The numerical value of then can be found from
Figure 1. =4R.
Fig 1
CSM: CS-438 2
Lecture 38
Types of Simulations (cont’d) Discrete-Event Simulations
Discrete-Event Simulations 1. An event scheduler
▪ A simulation using a discrete-state model of the
▪ It maintains a linked list of all pending events in
system that changes as a function of time.
their global time order.
▪ May use discrete- or continuous-time values.
▪ Processes the next event by removing it from the list
▪ Components of a discrete-event simulator:-
and dispatching it to the appropriate event-
1. An event scheduler processing routine.
2. Simulation Clock and a Time-advancing Mechanism ▪ May be called several times during one event to
3. Event processing routines schedule other new events.
4. Initialization Routines ▪ The scheduler also inserts new events into the
5. Event-generation appropriate point in the list on the basis of their
execution time.
6. Recording and summarization of data
CSM: CS-438 3
Discrete-Event Simulations Discrete-Event Simulations
2. Simulation Clock and a Time-advancing 3. Event processing routines
Mechanism (cont’d)
o After all these events have been processed, the ▪ Each event in the system is simulated by its own
scheduler again increments the global time event-processing routine.
variable. ▪ For example, routines to handle the two events of job
b)The alternative event-driven approach allows the arrivals and job departure.
global time to jump to the value of the next event at ▪ These routines may update the global state and they
the head of the pending-event list. may generate additional events that must be inserted
o Here the value of the clock will change non- into the pending-event list.
uniformly. ▪ For example, a memory-access event in a simulation
o The event-driven approach is probably the most of a processor may result in two possible outcomes.
common in simulations of computer systems.
Discrete-Event Simulations
Discrete-Event Simulations
5. Event-generation
3. Event processing routines (cont’d)
▪ Three possible techniques
a)Cache-hit: the event may simply return the stored a) execution driven,
value. b) trace driven, and
c) distribution driven.
b)Cache-miss: the memory-access event routine may
generate a new event a) Execution driven
o that returns the corresponding data value tmiss o The simulator actually executes a benchmark
time units in the future, program.
o where tmiss is the time required to service a cache o It models the necessary details of the system being
miss. tested.
4. Initialization Routines o To simulate a program that does floating-point-
arithmetic operations or any input/output operations,
▪ These set the initial state of the system state variables ❖the simulator must provide mechanisms for actually
and initialize various random-number generation performing the necessary arithmetic and
streams. input/output operations.
CSM: CS-438 4
Discrete-Event Simulations Discrete-Event Simulations
5. Event-generation
o One of the main advantages of execution-driven b) Trace driven
simulation is their accuracy i.e. o A trace is a time-ordered record of events on a real
❖it provides a level of control of timing system.
between events, and the timing of each ❖Sequence of pages
individual event, ❖2, 6, 4, 11, …
❖that is not possible with the other types of o Trace-driven simulations are generally used in
simulation. analyzing or tuning resource management algorithms
o Their primary disadvantage is that they can be ❖Paging algorithms, CPU scheduling algorithms,
time consuming to develop, verify and execute deadlock prevention algorithms etc.
the simulator. o In this technique, trace of resource demand patterns
of key programs are obtained on a system.
❖This trace can then be used as input to the simulation
which models different algorithms
CSM: CS-438 5
Discrete-Event Simulations Discrete-Event Simulations
5. Event-generation 6. Recording and summarization of data
c) Distribution driven (cont’d) ▪ The simulator maintains appropriate event counts
o The simulator then produces an output that would and time measurements.
occur ▪ These values will be used at the end of simulation
❖if the real system were driven by an application program to calculate appropriate statistics.
❖that produced the same sequence of inputs. ▪ For example, if a memory system is being
o The simulation should be run many times modeled, the simulator would probably count
❖with different input sequences and outputs produced o the total number of memory references and
❖then can be averaged to produce a statistics of the o the number of those references that result in
performance of the system. cache misses.
o This type of probabilistic (or stochastic) simulation ▪ These values can be used to estimate the cache-
is often used when actual application programs are miss ratio.
not available to drive the simulator.
CSM: CS-438 6
The simulation algorithm
Generation of Random Numbers
4. while ((more events in the list ) AND
(time < maximum simulation time limit))
{ ▪ Implementing a distribution-driven discrete-event
a. Advance the global time simulation and Monte-Carlo simulation requires a
b. Remove the next event from the pending-event list supply of random numbers from probability
c. Process the event distributions.
{
i. Perform event-specific operations ▪ A random number generator is an algorithm that
ii. Update global variables produces sequences of random numbers that
iii. Update simulation statistics
iv. Generate new events triggered by this event
follow a specified probability distribution.
and insert into the pending list
}
}
5. Print the simulation results
CSM: CS-438 7
The Inverse Transformation Method The Inverse Transformation Method
▪ Given that R is a random value obtained from a
▪ Suppose it is desired to obtain a random sample x from uniform (0, 1) distribution, and assuming that F-1 is the
the (continuous or discrete) PDF f(x). inverse of F.
▪ The inverse method first determines a closed-form ▪ The steps of the method are as follows:
expression of the Cumulative Density Function, CDF, o Step 1. Generate a (0, 1) uniform random number, R.
F(x) = P{y ≤ x}, o Step 2. Set F(x) = R and solve for x as x = F-1(R).
where 0 ≤ F(x) ≤ 1, for all defined values of y.
Fig 2: Sampling from a probability distribution by the Fig 2: Sampling from a probability distribution by the
inverse method inverse method
CSM: CS-438 8
Uniform Distribution
▪ The PDF for a value x = unif(a,b) is
• R= unif(0,1)
• x = F-1(R) = a + (b - a) R
• Suppose a = 10, b=20, R= 0.3456
• What is x?
CSM: CS-438 9