You are on page 1of 31

Section 5.

3: Event List Management


Discrete-Event Simulation: A First Course
c 2006 Pearson Ed., Inc. 0-13-142917-5

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

1/ 31

Section 5.3: Event List Management

Large NES may have thousands of events in the event list Such a NES will spend most of CPU time on managing event list Ecient event management will reduce overall CPU time Structures also applicable to SJF queue discipline

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

2/ 31

Introduction

Event list: data structure of events, plus any extra associated info Elements in the list: event notices List size classications:
Fixed maximum. No need for dynamic memory allocation. Variable or unknown maximum.

Application:
A specic model. We can exploit model characteristics. General-purpose (e.g., simulation language). Need robust DS

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

3/ 31

Event list operations

Critical operations:
Insert / enqueue
Schedule an event

Delete / dequeue
Usually: process the next event Rarely: cancel an already-scheduled event

Other operations (not considered here):


Change operation
Search to change an attribute (e.g., scheduled time)

Examine operation
Search to read an attribute

Count operation
How many event notices in list?

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

4/ 31

Event list criteria

Speed
Balance between sophisticated DS and overhead Consider average-case and worst-case performance

Robustness
Performs well for many scheduling scenarios General purpose: performs well for many simulation models
Use diverse, representative benchmark models

Adaptability
Adapt to changes in event distributions Adapt to changes in event list size Parameter-free

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

5/ 31

Conceptual Model: a computer timesharing system

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..

Think

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..

Type

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..

Receive

Computer user endlessly cycles:


1 2

Thinking: requires Uniform(0, 10) seconds Typing:


Equilikely(5, 15) keystrokes for the command Uniform(0.15, 0.35) seconds per keystroke

Receive output of command:


Equilikely(50, 300) characters of output 1/120 seconds to display each character (constant)

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

6/ 31

Conceptual Model: simplications

Always exactly n users at n terminals All users think at same rate All users type at same rate No interaction between users No load on system:
Command execution time is always instantaneous Receive rate remains xed

Are the distributions appropriate?

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

7/ 31

Rough analysis: simulated time

Average think time is Average type time is Average receive time

0+10 = 5 seconds 2 5+15 0.15+0.35 = 2.5 seconds 2 2 1 50+300 is 2 120 1.4583 seconds

Average cycle time is 5 + 2.5 + 1.4583 = 8.9583 seconds Steady-state probability of thinking, typing, receiving: 5 0.56 8.9583 2.5 0.28 8.9583 1.4583 0.16 8.9583

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

8/ 31

Rough analysis: scheduled events

Average think events per cycle: 1 Average keystroke events per cycle:
5+15 2

= 10
50+300 2

Average display character events per cycle: Expected fractions of event types: 1 0.005 186 10 0.054 186

= 175

Average number of events per cycle: 1 + 10 + 175 = 186 175 0.941 186

Most scheduled events are display character

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

9/ 31

Specication Model

Events for each user (eld type):


1 2 3

Complete thinking time Complete a keystroke Complete character display Number of keystrokes remaining in command Number of characters remaining to display

Events (2) and (3) require extra information (eld info)

Simplistic event-list structure:


Array of size n with elds time, type, info O(1) for insert operation O(n) for delete operation

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

10/ 31

Simple event list


Users initially thinking
time type info 0 1 2 3 4 9.803 1 3.507 1 1.305 1 2.155 1 8.243 1

At time 1.305, user 2 nishes thinking:


Generate Equilikely(5, 15) keystrokes: 7 Generate Uniform(0.15, 0.35) time for rst keystroke: 0.301 Schedule keystroke completion at time 1.305 + 0.301 = 1.606
time type info 0 1 9.803 1 3.507 1 1.606 2 7 2 3 4 2.155 1 8.243 1

Subsequent keystrokes for user 2 decrement info eld


Discrete-Event Simulation: A First Course Section 5.3: Event List Management 11/ 31

Algorithm 5.3.1, main t = 0.0; for (i = 0; i < n; i ++) { event[i ].time = GetThinkTime(); event[i ].type = 1; } while (t < ) { j = MinIndex(event); /* scan the event list */ t = event[j ].time; /* update clock */ if (event[j ].type == 1) DoneThinking(j ); else if (event[j ].type == 2) DoneKeystroke(j ); else if (event[j ].type == 3) DoneDisplay(j ); }
Discrete-Event Simulation: A First Course Section 5.3: Event List Management 12/ 31

Algorithm 5.3.1, details


DoneThinking Method
DoneThinking(int j ) { /*Schedule first keystroke*/ event[j ].time += GetKeystrokeTime(); event[j ].type = 2; event[j ].info = GetNumKeystrokes(); }

DoneKeystroke Method
DoneKeystroke(int j ) { event[j ].info; /*Decrement remaining keystrokes*/ if (event[j ].info > 0) event[j ].time += GetKeystrokeTime(); else { /* Schedule first display */ event[j ].time += 1.0 / 120.0; event[j ].type = 3; event[j ].info = GetNumCharacters(); } }
Discrete-Event Simulation: A First Course Section 5.3: Event List Management 13/ 31

Program ttr

Implements Algorithm 5.3.1 Counts total number of events scheduled Counts total number of event notices searched Terminates at time = 100 seconds Average number of cycles per user:
100 8.9583

11.16

Average number of events per user: 11.16 186 = 2076.3

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

14/ 31

Performance of simple event list

Run ttr with seeds 123456789, 987654321, 555555555 Compute averages over three runs
# users n 5 10 50 100 Theoretical Avg.# events 10 381 20 763 193 814 207 628 Average # events 9 902 20 678 191 669 201 949 Average # searches / event 5 10 50 100

Number of events is consistently lower than theoretical average


Due to initial bias (initially thinking)

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

15/ 31

Improved event list management

Use an ordered event list (i.e., times are sorted) Use linked list instead of array (faster insertion) Insertion is O(n) in worst case, O(1) in best case Deletion is O(1) Structure for searching head to tail (i.e., most recent in front)
1.606 2.155 3.507 8.243 9.803 2 1 1 1 1 7 next next next next next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

head . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

tail

Most recent in back: reverse pointers, search tail to head

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

16/ 31

Ordered list performance

Using most recent in back: # users Average # n events 5 9 902 10 20 678 50 191 669 100 201 949 But from earlier analysis:

Average # searches / event 3.83 8.11 40.55 81.19

Most events are display character Display character has very short duration Most events scheduled for near future We should use most recent in front

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

17/ 31

Ordered list performance, most recent in front

# users n 5 10 50 100

Average # events 9 902 20 678 191 669 201 949

Average # searches / event 1.72 2.73 10.45 19.81

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

18/ 31

Advanced event list management

Further discussion is for general case:


1 2 3

Number of events in list varies Maximum size of event list is unknown Structure of simulation model is unknown Multiple linked lists Binary search trees Heaps Hybrid schemes

We will discuss four structures:


1 2 3 4

Other structures exist

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

19/ 31

Multiple Linked Lists


Use k ordered lists Insert into shortest list: worst case O(n/k ) Delete is O(k ): check front of each list Example for k = 2, n = 10 for ttr:
1.305 1 2.155 1 3.507 1 8.243 1 9.803 1 tail

next next next next next head . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.211 1 2.220 1 3.020 1 4.513 1 9.591 1

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

next next next next next head . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

tail

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

20/ 31

Issues for Multiple Linked Lists

Should k be xed, or allowed to vary? If xed, what is a good k ? If variable, decide


When to increase or decrease k (as a function of n) How to increase k : split lists or start new? How to decrease k : merge lists?

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

21/ 31

Binary trees

Recall: Each node has at most 2 child nodes, and at most one parent node The node with no parent: root A node with no children: leaf Node level: 1 if root, 1+parents level otherwise Tree height: maximum level

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

22/ 31

More on binary trees

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . .. . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .. . . . . . . . . . . . .. .. . . . . . . . . . . . . . . . . . .. . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Full tree: All leaves at same level All non-leaves have 2 children

Complete tree: Full down to level h 1 Level h is lled left to right

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

23/ 31

Binary search trees


4.513 1
. . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.220 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9.591 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.155 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3.507 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8.243 1

9.803 1

. . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . .

1.305 1

2.211 1

3.020 1

Node property:
Discrete-Event Simulation: A First Course

Left child value Node value Right child value


Section 5.3: Event List Management 24/ 31

Binary search trees for event lists

Leftmost node is most imminent Worst-case insert: O(h) Worst-case delete: O(h) Unbalanced trees: easier to implement
Height of tree h can be as large as n

Balanced trees: need to rotate nodes


Height can be limited to h = O(log n) AVL, red-black, Splay trees are examples

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

25/ 31

Heaps
1.305 1
. . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.220 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.155 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3.020 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4.513 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9.803 1

2.211 1

. . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . .

8.243 1

3.507 1

9.591 1

Node property:
Discrete-Event Simulation: A First Course

Node value Left child value, right child value


Section 5.3: Event List Management 26/ 31

Heaps as event lists

Always a complete binary tree Root node is most imminent Insert, delete: maintain heap property by swapping nodes with parent
Easier to implement than balanced binary search trees

Worst-case insert: O(log n) Worst-case delete: O(log n) Searching for arbitrary event: O(n)

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

27/ 31

Hybrid schemes

If n is small, a simple structure may work best If n is large, a tree structure should work well One hybrid scheme: change data structures. E.g.:
When n increases above 15, change to heap When n decreases below 6, change to ordered list

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

28/ 31

Henriksens algorithm

Uses binary search tree and ordered list simultaneously Ordered list:
Doubly linked Ordered by event time Contains dummy events with time and

Binary tree:
Nodes for a subset of event times Node format:
Pointer to next lower time tree node Pointer to left child tree node Pointer to right child tree node Event time Pointer to the event notice

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

29/ 31

Example of Henriksens DS
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3.507

0 0 1.305

0 0 +

prev prev prev tail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...................................................... .......... ......... + 1.305 3.507 four four 1 1 other other event event head .. next .. next .. notices .. next .. notices .. next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .................. .................. dummy dummy real real real real
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

prev

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

30/ 31

Operations using Henriksens algorithm

Deletion: from front of list, tree is not xed, O(1) Insertion (time t ):
1 2 3 4

Find smallest time larger than t in tree Search backwards in list at most l positions (usually l = 4) Position found: insert Position not found: pull operation to rebalance tree:
Go to next lower time node in tree Change that tree node to point to current list entry Continue search at step (2) If next lower time node not present, add a new level to tree

Tends to have short average insertion time Implemented in simulation languages: GPSS, SLX, SLAM

Discrete-Event Simulation: A First Course

Section 5.3: Event List Management

31/ 31

You might also like