You are on page 1of 9

Lecture 36

BE (Computer Systems) Types of Simulations


Spring 2022 Semester 1. Emulation
2. Static (or Monte-Carlo) simulation
Computer Systems Modeling 3. Discrete-event simulation
(CS-438) 4. Continuous-event simulation

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.

Emulation Static (Monte Carlo) Simulation


▪ Examples
o A terminal-emulator program is often executed on a ▪ No time parameter.
PC to make that computer appear to be a specific ▪ The simulation is run until further refinement of the
type of terminal to a remote system. state of the simulated system is no longer useful or
o JVM is an example of a processor emulator. possible.
❖Executes on a wide variety of different computer ▪ Static simulations are often used to evaluate
systems. o some physical phenomenon (probabilistically), or
❖It simulates the execution of a VP whose o to numerically estimate the solution of some
instruction set is that defined in the JVM mathematical expression, such as a complex
specification. integral.
❖JVM allows programs written in the Java to be
executed on any processor for which a JVM has
been written.

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

▪ The problem of computing the value of  has been


▪ Since the area of a circle with a radius of 1 is (1)2 = , transformed into the equivalent geometric problem of
the area of the quarter-circle within the first quadrant determining the ratio of the two areas, R.
is /4. ▪ A Monte Carlo simulation can be used to find R by
▪ The area contained within the unit square in this modeling an equivalent physical system.
quadrant is simply 1.

Static (Monte Carlo) Simulation Example


Static (Monte Carlo) Simulation Example
• Imagine throwing a large number of darts randomly at
figure 1 such that every dart hits within the unit ▪ If the distance from the origin of the point defined by (x,y)
square. = (u1,u2) is smaller than the radius of the circle, that is,
𝑢12 + 𝑢22 < 1, then the simulated dart has hit within the
quarter-circle.

• Then we count the number of times a dart hit within


the quarter-circle, ncirc, and the total number of darts
thrown, ntotal. ▪ By repeating this process a large number of times, we can
• Then the desired ratio of the two areas is theoretically compute the value of  to any level of
R = ncirc/ntotal. precision desired.
▪ We can simulate this dart-throwing experiment by ▪ The key to this type of Monte Carlo simulation is
generating two random numbers, u1 and u2, for each dart identifying an appropriate physical model for the system
thrown, such that u1 and u2 are both uniformly distributed being studied.
between 0 and 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

Discrete-Event Simulations Discrete-Event Simulations


2. Simulation Clock and a Time-advancing
1. An event scheduler (cont’d) Mechanism
▪ A global time variable records current simulation time.
▪ Events can be manipulated in various ways as
follows:- ▪ It can be updated by the scheduler using one of two
approaches:-
o Schedule event X at time T.
a) In the fixed-increment approach, the scheduler
o Cancel a previously scheduled event X. increments the global clock by some fixed amount.
o Hold event X indefinitely (until it is o It then checks the pending events.
scheduled by another event). o If the scheduled time for any of the pending events
matches the current time, all of these events are
dispatched for execution.

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

Discrete-Event Simulations Discrete-Event Simulations


5. Event-generation 5. Event-generation
b) Trace driven c) Distribution driven
o Advantage: the simulator itself does not have to o A distribution driven simulation is similar to a
provide all of the functionality needed by the trace-driven simulation, except that input
program, as is required in an execution-driven events are generated by the simulator itself to
simulation. follow some predefined probabilistic function.
o For example, sending messages over a
o Disadvantage: the trace represents only one
communication network could be modeled by
possible execution path which need not be
representative of the `typical' workload. ❖ using an exponential distribution
❖ to determine the amount of time that elapses
between each message.

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.

Lecture 39 The simulation algorithm


Types of Simulations (cont’d)
Continuous-Event Simulations ▪ Using the basic components, the overall
processing done by a discrete-event
▪ The state of the system varies continuously with simulator can be summarized in the
time. following steps:-
▪ Used in chemical simulations where the state of
the system is described by the concentration of a 1. Initialize global state variables
chemical substance. 2. Initialize the global time to 0
3. Add the first event to the pending-event list

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

Lecture 40 Methods of Generation


Methods of Generation
▪ The inverse method particularly suited for
▪ Three methods for generating successive analytically tractable PDFs, such as the
random samples (x = x1, x2, …) from a exponential and the uniform.
probability distribution f(x):-
▪ The remaining two methods deal with more
1. Inverse Transformation method. complex cases, such as the normal and the
2. Convolution method. Poisson.
3. Acceptance-rejection method. ▪ All three methods rooted in the use of
independent and identically distributed uniform
(0, 1) random numbers.

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

The Inverse Transformation Method The Inverse Transformation Method


▪ In terms of simulation, the result means that
▪ For certain continuous distributions, the inverse arrivals are spaced x time units apart.
transformation method can be implemented on a
computer by first solving the equation F(x) = R ▪ For example,
analytically for x. o for  = 4 jobs per hour and
▪ The CDF for the exponential distribution is o R = .9,
o the time period until the next arrival occurs is
F(x) = 1 – e-x, for x ≥ 0,
computed as
▪ Setting F(x) = R thereby yields
1 – e-x = R,
▪ The values of R used to obtain successive samples
e-x = 1 – R, must be selected randomly from a uniform (0, 1)
distribution.

CSM: CS-438 8
Uniform Distribution
▪ The PDF for a value x = unif(a,b) is

▪ The corresponding CDF 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

You might also like