Top Banner
Discrete event simulation
54

Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Mar 26, 2015

Download

Documents

Sophia McGrath
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Discrete event simulation

Page 2: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

About me

● Name : Tiberiu Petre(call me Tibi, Tib,etc.)● Job : Hardware Design and Verification Engineer● Experience : 5+ years● Tools used : HDL simulators● Languages : Verilog, VHDL, e, SystemVerilog,

C++, SystemC, Matlab

Page 3: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulation

● What is it?● Simulation is the imitation of the operation of a

real-world process or system over time.(Wikipedia)

● Why do we need to simulate stuff?● Sometimes we can't intuit or anticipate the

behavior of some types of systems.● We need simulation to analyze and understand

the behavior of complex systems.

Page 4: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simple example

● An AND gate ● No need to simulate this

● It's very simple● We can visualize its behavior without the need of a computer

Page 5: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Complex example

● A finite state machine

Page 6: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Another complex example

● Conway’s Game of Life

Page 7: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Discrete event simulation

● The operation of a system is represented as a chronological sequence of events

time

Page 8: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulation constraints

● We don't want any uncertainty● Everything has to be deterministic● When a scenario is simulated multiple times

with the same initial conditions the output of the simulation should be the same(reproducibility)

Page 9: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulation concerns

● Simulating parallelism● Simulating time● Simulating discrete events● Simulating randomness● Simulating communication

Page 10: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating parallelism

● Interleaved actions(race conditions)

Page 11: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Race conditions

● Interleaved actions(race conditions)

int x = 0;f(x) g(x)

Thread 1 Thread 2

X = ???

Sollution : Mutexes

Page 12: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Execution order

● Unpredictable execution order

int x = 0;f(x) g(x)

Thread 1 Thread 2

Who gets to x first?“f got x” “g got x”

Page 13: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Coroutines

● The OS kernel is replaced by the Simulation Kernel

● The programmer(not the compiler) decides when each thread gives control to the SKT1 T2 T1

SK SK

● How can we rely on the SK?● How does the SK remove the uncertainty?

def my_thread():do_something(“anything”)yielddo_something(“else”)

Page 14: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating time

● Real time

Thread.currentThread().sleep(1000);//sleep for 1000 ms

● Real time● Real time

Page 15: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating time

● Simulated time

wait(1000 ms);

<=>

global_time_counter+=1000 ms;

Page 16: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating time

● Simulated time

wait(1000 years);

<=>

global_time_counter+=1000 years;

● We make it look as if 1000 years have passed

Page 17: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Applications

● Financial models – they work with large periods of time(days, months, years)

● Digital system models – they work with small periods of time(ms, ns, ps, fs)

Page 18: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simultaneity

f(x) g(x)

Thread 1 Thread 2

t=15s

t=0s

Page 19: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simultaneity

● With native threads and real time: possible on multi-core/multi-processor architectures but very unlikely(impossible in practice)

● With coroutines and simulated time: we can make it look as if it happens :-)

def thread1():

wait(1s)

action1()

def thread2():

wait(1s)

action2()

Page 20: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Time over multiple threads

● Many threads – one global time counter● How do we update it?

def thread1():

wait(1s)

action1()

wait(3s)

def thread2():

wait(10s)

action2()

wait(1s)

def thread3():

wait(3s)

action3()

wait(5s)

● The SK updates the GTC not the threads● Threads must pass control to the SK when

they want to consume time

Page 21: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Time over multiple threads

def update():

min_time = minimum time waited by all threads

global_time_counter += min_time

for each thread in threads_that_wait_time:

if time_waited[thread] == min_time:schedule_resume(thread)

else:time_waited[thread] -= min_time

Page 22: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Time over multiple threads

def evaluate():

for each thread in threads:

if thread is scheduled for resumption:resume(thread)

Page 23: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Delta cycle

Evaluate

Update

Page 24: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulation algorithm

def simulate():

while not all threads finished:

evaluate()update()

Page 25: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Exampledef thread1():

wait(1s)

action1()

wait(3s)

def thread2():

wait(10s)

action2()

wait(1s)

def thread3():

wait(3s)

action3()

wait(5s)

GTC

T1

T2

T3

0s 1s 3s 4s 8s 10s 11s

w1s w3s w1s END

w10s w9s w7s w6s w2s w1s END

w3s w2s w5s 4ws END

a1

a3

a2

Page 26: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating events

● Emitting events● Waiting on events● Event callbacks● Events over multiple threads

Page 27: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating events

● What is an event?● It is a message indicating that something has

happened

time

Button pressed Door open Door closed

Page 28: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Using events

● Events can be emitted

x = 0

emit(x_has_been_reset)

● Events can be waited onwait(x_has_been_reset)

do_something_with(x)

Page 29: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Event callbacks

● Events can trigger actions(callbacks)

on(x_has_been_reset):

do_something()

Page 30: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Events over multiple threads

● Events are emitted and waited over multiple threads

● How do we simulate them?

def thread1():

action1()

emit(ev1)

def thread2():

wait(ev1)

action2()

Page 31: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

How emit works

● Events can have 2 states : emitted, idle● Emit does not suspend thread execution● Emit does not consume time

def emit(event):

sk.event_states[event] = emitted

Page 32: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

How emit works

● Emits are resolved during the evaluation phase

def evaluate():for each thread in threads:

if thread is scheduled for resumption:resume(thread) # this is where emits might be called

Page 33: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

How wait works

● Waits are resolved during the update phase● Waits suspend thread executions● Waits might consume time

def update():

# time management code ...

for each thread in threads_that_wait_events:

if events_waited[thread].state == emitted:

schedule_resume(thread)

Page 34: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simple example

● Evaluate: action1() is executed

● Evaluate: ev1 is emitted

● Evaluate: thread2 is suspended waiting for e1

● Update: thread1 is marked as finished

● Update: thread2 is scheduled for resumption

● Evaluate: thread2 is resumed, action2() is executed

● Update: thread2 is marked as finished

● Evaluate: all threads are finished, simulation ends

def thread1():

action1()

emit(ev1)

def thread2():

wait(ev1)

action2()

T1

T2

a1->ev1

a2

END

wev1 END

Page 35: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Temporal expressions

event my_ev is {e1,10s,2*e2,1s,e3}

e1

e2

e3

my_ev

10s

1s

Page 36: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Temporal assertions

assert e1 => {[5s..10s],2*e2,1s,e3}

e1

e2

e3

7s

1s

Assertion passedAssertion evaluation starts

Page 37: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating randomness

● We must use pseudo-randomization● We must set an initial seed which will

guarantee us that the sequence of generated random numbers will be the same every time we rerun the simulation with the same seed

set_seed(16182)

for i from 1 to 10:

x=rand()

print x

Page 38: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Pseudo randomness

● Linear feedback shift registers

Page 39: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Pseudo randomness

● Linear feedback shift registers

Page 40: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Pseudo randomness

● Polynomials for maximal LFSRs

Page 41: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Constrained randomization

● Requires a constraint solver

struct my_struct_t {

x:uint;

y:uint;

keep x+y==10;

keep x !=7;

};

extend sys {

s : my_struct_t;

run() is also {

gen s;

print s;

};

};

Page 42: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating random execution order

● We can (pseudo-)randomize the way in which threads are resumed

def evaluate():

randomize thread execution order

for each thread in threads_in_random_order:

if thread is scheduled for resumption:

resume(thread)

● We can also (pseudo-)randomize the way in which event callbacks are executed

Page 43: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating Communication

● Producers and consumers● Initiators and targets● Broadcasting

Page 44: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating Communication

● Producers and consumers

Producer Consumer

Page 45: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating Communication

● Initiators and targets

Initiator Target

Page 46: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating Communication

● Producer-initiator and consumer-target

ProducerInitiator

ConsumerTarget

Page 47: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating Communication

● Producer-target and consumer-initiator

ProducerTarget

ConsumerInitiator

Page 48: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Simulating Communication

● Broadcast

Producer

Consumer1

Consumer2

Consumer3

Page 49: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Boosting simulation performance

● Parallelize independent threads ● Parallelize execution of independent event

callbacks● Temporal decoupling

Page 50: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Visualizing simulation output

● Waveforms● Plots● Logs● Write your own scripts for processing

simulation output

Page 51: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Exercisedef T1():

while true:

wait(1s)

emit(e1)

a1()

def T2():

for i from 1 to 2:

wait(e1)

a2()

kill(T1)

a3()0s 1s 2s 3s 4s

w1s w1s w1s w1s END

we1 we1 we1 END

->e1a1

->e1a1

->e1a1

a2

GTC

T1

T2

a2kill(T1)a3

Page 52: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Applications

● HDL Simulators● Virtual Platforms for early software

development● Architectural exploration● Performance evaluation● Turn-based strategy games● Back-in-time debugging● Cause analysis● Business model simulation

Page 53: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Further reading

● A Curious Course on Coroutines and Concurrency, David Beazley - http://dabeaz.com/coroutines/

● SystemC - http://www.accellera.org/home/

● Transaction Level Modeling - http://en.wikipedia.org/wiki/Transaction-level_modeling

● Coroutines in C - http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

Page 54: Discrete event simulation. About me Name : Tiberiu Petre(call me Tibi, Tib,etc.) Job : Hardware Design and Verification Engineer Experience : 5+ years.

Questions and/or comments

THANKYOU

!!!