User guide

88 CHAPTER 3. THE LIBRARY
3.7.3 A Discrete Event Simulator
This is a benchmark test for a di sc r e te event simulator using coroutines. It simulates
a network of n nodes which each receive, q ue ue , process and transmit messages to
other nodes. The nodes are uniformly spaced on a straight line and the network de-
lay is assumed to be proportional to the linear distance between the source and the
destination. When a message arri ves at a node it is queued if the node was busy,
otherwise it is processed immediately. After processing the message for random time,
it is sent to another randomly chosen node. After dispatching the message, the node
dequeues its next message and processes it if there is one, otherwise the node becomes
suspended. Initially every node is processing a message and every queue is empty.
There are n corouti ne s to simulate the progress of each message and the disc re t e event
priority queue is implemented using the heapsort heap structure. The simulation stops
at a specified simulated time . The result is the number of messages that have been
processed. A machi ne independe nt random number generator is used so the resulting
value should be independent of implementation language and machine being used.
The program is given below. When it is run using the default settings, it executes
435,363,350 Cintcode instruc ti ons and has 2,510,520 coroutine changes.
SECTION "cosim"
GET "libhdr"
GLOBAL {
priq:ug // The vector holding the priority queue
priqupb // The upper bound
priqn // Number of items in the priority queue
wkqv // The vector of work queues
count // count of messages processed
nodes // The number of nodes
ptmax // The maximum processing time
stopco // The stop coroutine
cov // Vector of message coroutines
ranv // A vector used by the random number generator
rani; ranj // subscripts of ranv
simtime // Simulated time
stoptime // Time to stop the simulation
tracing
// Functions
rnd; initrnd; closernd; prq; insertevent; upheap
downheap; getevent; waitfor; prwaitq; qitem; dqitem
stopcofn; messcofn
}