cpsc 531: simulation examples1 cpsc 531: some more examples – time-shared computer and...
TRANSCRIPT
CPSC 531: Simulation Examples 1
CPSC 531: Some more examples –Time-shared computer and multi-teller bank
Instructor: Anirban MahantiOffice: ICT 745Email: [email protected] Location: TRB 101Lectures: TR 15:30 – 16:45 hoursClass web page:
http://pages.cpsc.ucalgary.ca/~mahanti/teaching/F05/CPSC531/
Notes derived from “Simulation Modeling and Analysis” by A. Law and W. Kelton, Third edition, McGraw Hill, 2000.
CPSC 531: Simulation Examples 2
Objective Under simulation fundamentals via
examples Time-shared computer model Multi-teller bank with jockeying Job shop model
Get a grip of “simlib”
CPSC 531: Simulation Examples 4
Problem Specification Terminals “think” and then issue jobs Think times exponentially distributed - 25 s Job service times also exponentially distributed -
0.8 s Job processing rule: round-robin
Each visit to CPU is q = 0.1 sec. (a quantum)• If remaining processing requirement > q sec., it gets q sec,
then kicked out• If remaining processing requirement q sec., it gets what it
needs, returns to its terminal Other job processing possibilities: FIFO, SJF, LIFO etc.
Swap time: = 0.15 sec. Elapse whenever a job enters CPU before processing starts
CPSC 531: Simulation Examples 5
Problem Specification Response time of a job = (time job returns to
terminal) – (time it left its terminal) Initially, computer empty and idle, all n jobs
in the think state at their terminals Stopping rule: 1000 response times collected Output:
Average of the response times Time-average number of jobs in queue for the CPU CPU utilization
Question: how many terminals (n) can be loaded on and hold average response time to under 30 s?
CPSC 531: Simulation Examples 6
Turning the specifications to a prog.
Job ArrivalCPU
ProcessingEnd
Simulation
Terminals
CPSC 531: Simulation Examples 7
Simlib program: events, lists, etc Events
1 = Arrival of a job to the computer (at end of a think time)2 = A job leaves the CPU (done or kicked out)3 = End simulation (scheduled at time 1000th job gets done)(Also, have “utility” non-event function start_CPU_run to
remove first job from queue, put it into the CPU)
simlib lists, attributes:1 = queue, attributes = [time of arrival of job to computer,
remaining service time]2 = CPU, attributes = [time of arrival of job to computer,
remaining service time]• In CPU, if remaining service time > 0 then job has this much
CPU time needed after current CPU pass; if remaining service time 0, job will be done after this pass
25 = event list, attributes = [event time, event type]
CPSC 531: Simulation Examples 8
Simlib program: variables, random streams
sampst variable: 1 = response times
timest variables: none (use filest on the lists)
Random-number streams: 1 = think times, 2 = service times
CPSC 531: Simulation Examples 9
Overview of function main()/* ……… code fragment from main() ……….. */for (num_terms = min_terms; num_terms <= max_terms; num_terms += incr_terms) { init_simlib(); /* Initialize simlib */
maxatr = 4; /* NEVER SET maxatr TO BE SMALLER THAN 4. */num_responses = 0; /* Initialize the non-simlib statistical counter. */
for (term = 1; term <= num_terms; ++term) /* Schedule the first arrival to the CPU from each terminal. */
event_schedule(expon(mean_think, STREAM_THINK), EVENT_ARRIVAL);
do { timing(); /* Determine the next event. */
/* Invoke the appropriate event function. */ switch (next_event_type) {
case EVENT_ARRIVAL: arrive(); break;
case EVENT_END_CPU_RUN: end_CPU_run(); break;
case EVENT_END_SIMULATION: report(); break; } } while (next_event_type != EVENT_END_SIMULATION); }
CPSC 531: Simulation Examples 10
The arrive() functionFunction arrive()
Place job in queue
CPU idle?Yes
No
Return
Start CPU run
CPSC 531: Simulation Examples 11
The arrive() functionvoid arrive(void) /* Event function for arrival of job at CPU after think time. */{
/* Place the arriving job at the end of the CPU queue. Note that the following attributes are stored for each job record: 1. Time of arrival to the computer. 2. The (remaining) CPU service time required (here equal to the total service time since the job is just arriving). */
transfer[1] = sim_time; transfer[2] = expon(mean_service, STREAM_SERVICE); list_file(LAST, LIST_QUEUE);
/* If the CPU is idle, start a CPU run. */
if (list_size[LIST_CPU] == 0) start_CPU_run();}
CPSC 531: Simulation Examples 14
Things to do Look at the simlib code for the time shared
computer model Try problem 2.3 from the LK00 text book
Our next stop will be Multi-teller Bank with Jockeying
CPSC 531: Simulation Examples 15
Multi-teller Bank
New arrivals: If there’s an idle teller, choose leftmost (idle) teller If all tellers are busy, choose shortest queue (ties –
leftmost)
Initially empty and idle Termination: close doors at time 480 min.
If all tellers are idle at that time, stop immediately If any tellers are busy, operate until all customers leave
service
Interarrival times:Expon (mean = 1 min.) Service times:
Expon (mean = 4.5 min.)
CPSC 531: Simulation Examples 16
Jockeying Rules Suppose teller i (i fixed) finishes service — e.g., i =
3 above Then either teller i becomes idle, or queue i
becomes one shorter Maybe a customer at the end of some other queue
j ( i) moves to teller i (if now idle) or the end of the now-shorter queue i
For each teller/queue k, let nk = number of customers facing (in queue + in service) teller k just after teller i finishes service
Procedure: If nj > ni + 1 for some j i, then a jockey will occur If nj > ni + 1 for several values of j i, pick the closest j
(min |j – i|) If nj > ni + 1 for two equally closest (left and right) values
of j i, pick the left (smaller) value of j
CPSC 531: Simulation Examples 17
Performance Metrics
Estimate Time-average total number of customers in (all)
queues Average, max delay of customers in queue(s)
What’s the effect of the number of tellers?
CPSC 531: Simulation Examples 18
Simlib program: events, lists, etc Events
1 = Arrival of a customer to the bank2 = Departure of a customer from a teller (need to know
teller #)3 = Close doors at time 480 min. (may or may not be The
End)(Also, have “utility” non-event function jockey to see if
anyone wants to jockey, and, if so, carry it out)
simlib lists, attributes (n = number of tellers):1, …, n = queues, attributes = [time of arrival to queue]n + 1, …, 2n = tellers, no attributes = (dummy lists for
utilizations)25 = event list, attributes = [event time, event type,
teller number if event type = 2]
CPSC 531: Simulation Examples 19
Simlib program: variables, streams sampst variable: 1 = delay of customers in
queue(s) timest variables: none … use filest for
time-average number in queues since time-average of total = total of time averages (details in book)
Random-number streams:1 = interarrival times, 2 = service times
CPSC 531: Simulation Examples 20
The main() function// code fragment// … see text for the restevent_schedule(expon(mean_interarrival, STREAM_INTERARRIVAL), EVENT_ARRIVAL); /* Schedule the first
arrival. */
/* Schedule the bank closing. (Note need for consistency of units.) */ event_schedule(60 * length_doors_open, EVENT_CLOSE_DOORS);
/* Run the simulation while the event list is not empty. */ while (list_size[LIST_EVENT] != 0) { /* Determine the next event. */ timing(); /* Invoke the appropriate event function. */ switch (next_event_type) { case EVENT_ARRIVAL: arrive(); break; case EVENT_DEPARTURE: depart((int) transfer[3]); /* transfer[3] is teller number */
break; case EVENT_CLOSE_DOORS: event_cancel(EVENT_ARRIVAL); break; } }
CPSC 531: Simulation Examples 23
The depart() function …void depart(int teller) /* Departure event function. */{ /* Check to see whether the queue for teller "teller" is empty. */ if (list_size[teller] == 0)
/* The queue is empty, so make the teller idle. */ list_remove(FIRST, num_tellers + teller);
else { /* The queue is not empty, so start service on a customer. */ list_remove(FIRST, teller); sampst(sim_time - transfer[1], SAMPST_DELAYS); transfer[3] = teller; /* Define before event_schedule. */ event_schedule(sim_time + expon(mean_service, STREAM_SERVICE), EVENT_DEPARTURE); } /* Let a customer from the end of another queue jockey to the end of this queue, if possible. */ jockey(teller);}
CPSC 531: Simulation Examples 25
Things to do Look at the simlib code for the multi-teller
bank model Try problem 2.4 from the LK00 text book
Some food for thought How about replacing this multiple-teller
multiple-queue model with a single-queue multiple-teller model?
CPSC 531: Simulation Examples 26
Job Shop Model Five workstations
Number of identical machines at each workstation as shown
Network of multiserver queues Three types of jobs
Interarrival times (all job types combined) expon (mean = 0.25 hour)
Job type determined just after arrival
• Type 1, 2, 3 w.p. 0.3, 0.5, 0.2 Workstation routes for job types:
• Type 1: 3 1 2 5 (see fig.)• Type 2: 4 1 3• Type 3: 2 5 1 4 3
Mean service times (2-Erlang distrib.):
• Type 1: 0.50 0.60 0.85 0.50
• Type 2: 1.10 0.80 0.75• Type 3: 1.20 0.25 0.70
0.90 1.0
Initially empty and idle
Stop at time 365 8
hours
CPSC 531: Simulation Examples 27
Job Shop Model (cont’d.)
Estimate Average total delay in queues for each job type
separately Overall average total delay in queues over all job types,
weighted by their (known) probabilities of occurrence For each machine group separately:
• Average delay in queue there (all job types lumped together)• Time-average number of jobs in queue (all job types
together)
• Group utilization =
• Number of machines in the group
Question: If you could add one machine to the shop, to which group should it go?
Time-average number of machines that are busyNumber of machines in the group
CPSC 531: Simulation Examples 28
Simlib program: events, lists, etc Events
1 = Arrival of a job to the system2 = Departure of a job from a particular station3 = End of the simulation
simlib lists, attributes:1, …, 5 = queues, attributes = [time of arrival to station,
job type, task number]
25 = event list, attributes = [event time, event type,job type (if event type = 2),task number (if event type =
2)](Task number of a job is how far along it is on its route,
measured in stations, so starts at 1, then is incremented by 1 for each station)
CPSC 531: Simulation Examples 29
Simlib program: variables sampst variables:
1, …, 5, = delay in queue at machine group 1, …, 56, 7, 8 = total delay in queues for job type 1, 2, 3
timest variables:1, …, 5 = number of machines busy at machine
group 1, …, 5(We’ll keep our own, non-simlib variable num_machines_busy[j] to track the number of machines busy in group j)
Random-number streams: 1 = interarrival times, 2 = job-type coin flip, 3 = service times