Professional Documents
Culture Documents
1/ 31
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
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
3/ 31
Critical operations:
Insert / enqueue
Schedule an event
Delete / dequeue
Usually: process the next event Rarely: cancel an already-scheduled event
Examine operation
Search to read an attribute
Count operation
How many event notices in list?
4/ 31
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
5/ 31
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
Think
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
Type
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
Receive
6/ 31
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
7/ 31
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
8/ 31
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
9/ 31
Specication Model
Complete thinking time Complete a keystroke Complete character display Number of keystrokes remaining in command Number of characters remaining to display
10/ 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
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
14/ 31
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
15/ 31
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
16/ 31
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:
Most events are display character Display character has very short duration Most events scheduled for near future We should use most recent in front
17/ 31
# users n 5 10 50 100
18/ 31
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
19/ 31
next next next next next head . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.211 1 2.220 1 3.020 1 4.513 1 9.591 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
tail
20/ 31
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
22/ 31
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .. . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .. . . . . . . . . . . . .. .. . . . . . . . . . . . . . . . . . .. . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Full tree: All leaves at same level All non-leaves have 2 children
23/ 31
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
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
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
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
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)
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
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
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
30/ 31
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
31/ 31