TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every...

76
TKT-1527: Models of computation 2011-01-19 0-0

Transcript of TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every...

Page 1: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

TKT-1527: Models of computation

2011-01-19

0-0

Page 2: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

1 Overview

Practical models of computation can be derived from:

• State machines

• Petri nets

• Process networks

0-1

Page 3: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2 Finite state machines

2.1 “Man, wolf, goat, cabbage” -problem

Assume man, wolf, goat and a cabbage are on the same side of a river. Theman must move wolf, goat and cabbage to the other side of the river by usinga boat. However, the man can only move one item at a time over the river.Also, the wolf and goat may not be left alone because the wolf would eat thegoat. The goat and the cabbage may not be left alone because the goat wouldeat the cabbage. The goal is to find an action sequence such that these rulesare not violated.

This is a state search problem where the state can be represented as “A-B”,where “A” is the items on the original side of the river, and “B” is the itemson the destination side. Let “A” and “B” be strings that define the items.Define: M is the man, W is the wolf, G is the goat and C is the cabbage.The initial state is “MWGC-∅”, and the final (goal) state is “∅-MWGC”. Allpossible actions can be described with 4 symbols: “m”, “w”, “g” and “c”,whose explanations are in Table 1. Figure 1 shows two possible solutions

0-2

Page 4: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

Table 1: Actions (state transitions) for state space search problemSymbol Action

c man takes cabbage (over the river)g man takes the goatm man passes the riverw man takes the wolf

(sequences of actions) for the state search problem. The filled ellipse is theinitial state and the double ellipse is the final state.

0-3

Page 5: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

MWGC- WC-MGg

MWC-Gm

C-MWGw

W-MCG

c

MGC-Wg

MWG-Cg

G-MWC

c

w MG-WCm

-MWGCg

Figure 1: Solutions of the state space search problem

2.2 Definition: Finite state machine (FSM, DFA)

Finite state machine (FSM, DFA) is a 5-tuple M = (∑

, X, g, x0, F ), where

is a finite alphabet

X is a finite set of states

g is a state transition function, g : X ×∑

7→ X

x0 is the initial state, x0 ∈ X

F is the set of final states, F ⊆ X

0-4

Page 6: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

Finite state machines do not have memory or variables, only states. Ex-istence of multiple states is a model of memory and variables. The statemachines starts from the initial state x0 and reads input symbols belongingto the alphabet

. State machine makes state transitions according to thestate transition function g, current state and input. State machine goes to thestate g(x, a) when it is in state x and reads input symbol a. g(x, a) may notbe defined for all values of x and a, in which case the state machine does notaccept the input, no further input is read, and the input is called invalid.

The input can have arbitrary length. The sequence of all symbols in theinput is called a word. It is said that the state machine accepts the word (orinput) if the word takes state machine into a final state from set F . The set ofaccepted words defines a language L(M). That is, the state machine definesa language of words.

State machine does not accept a word if any read symbol lacks a transitionin g, or if the state after reading all the symbols is not in F (the set of finalstates).

The state machine defines a language

0-5

Page 7: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

L(M) = {r ∈∑∗ |M accepts r}.

2.2.1 Example: “Man, wolf, goat, cabbage” problem as a finitestate machine

“Man, wolf, goat, cabbage” state space be described with a finite state ma-chine, where transitions are only defined into states that valid from problemsetting point of view. For example, the wolf and the goat may not be aloneon the same of the river. Define finite state machine M = (

, X, g, x0, F ),where

= {m,w, g, c}

X = {(x, y)|x, y ⊆ {M,W,G,C} and y = {M,W,G,C}\x}

x0 = {({M,W,G,C}, ∅)}

F = {(∅, {M,W,G,C})}

Examples of possible state transitions:

0-6

Page 8: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

• Man takes the goat over the river: g(({M,W,G,C}, ∅), g) = ({W,C}, {M,G})

• Man returns back alone: g(({G,C}, {M,G}),m) = ({M,W,C}, {G})

Finite state machine M accepts words “gmwgcmg” and “gmcgwmg”.

0-7

Page 9: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.2.2 Example: binary number with even number of zeros and ones

Define an FSM M = (∑

, X, g, x0, F ), where∑

= {0, 1}

X = {x0, x1, x2, x3}

F = {x0, x3}

g(x0, 0) = x2, g(x0, 1) = x1

g(x1, 0) = x3, g(x1, 1) = x0

g(x2, 0) = x0, g(x2, 1) = x3

g(x3, 0) = x1, g(x3, 1) = x2

M accepts all binary numbers that have even number of zeros and ones. Instate x0 the FSM has an even number of zeros and ones. In state x1 it hasan odd number of ones and even number of zeros. In state x2 it has an oddnumber of zeros and even number of ones. In state x3 it has an odd number of

0-8

Page 10: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

x0

x1

1

x2

01

x3

0

0

1

01

Figure 2: Finite state machine that accepts all binary numbers with evennumber of bits

zeros and ones. The FSM is shown in Figure 2. The FSM can accept arbitrarylength inputs even if the number of states is finite.

0-9

Page 11: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.3 Non-finite state machines

An FSM has limitations on the language it can accept. For example, a lan-guage of all palindromes (words that read the same forwards and backwards)is not feasible with finite state machines, but is feasible with non-finite statemachines. The non-finite state machines have a similar definition to FSM:

2.3.1 Definition: state machine

State machine is a 5-tuple M = (∑

, X, g, x0, F ), where

is a numberable alphabet (countable set)

X is a numberable set of states

g is the state transition function, g : X ×∑

7→ X

x0 is initial state, x0 ∈ X

F is the set of final states, F ⊆ X

0-10

Page 12: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

Of course general (non-finite) state machines can not be implemented inpractice, but unbounded memory (states) can be practical to modeling sys-tems.

0-11

Page 13: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

x0

a

x1a

b

Figure 3: An example of a non-deterministic finite state machine

2.4 Non-deterministic finite state machines

Previous state machines were deterministic, which means they did one specificstate transition for a given input. Non-deterministic state machines can makea choice between several alternative transitions. A given input may causemore than one transition. These (non-deterministic) transitions are explicitlydefined by the state transition function. Non-deterministic state machines donot exist as implementations, but they are practical tools for models. Figure 3has an example of a non-deterministic state machine, where symbol “a” maycause a transition to state x0 or x1.

0-12

Page 14: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.4.1 Definition: Non-deterministic finite state machine (NFA)

A non-deterministic finite state machine (NFA) is a 5-tuple (∑

, X, g, x0, F ),where

is a finite alphabet

X is a finite set of states

g is a state transition function, g : X ×∑

7→ P (X)

x0 is the initial state, x0 ∈ X

F is the set of final states, F ⊆ X

The difference between deterministic and non-deterministic definitions isthe state transition function g that in NFA maps a given state and inputinto a set of possible states. P (X) is the power set of X, which is the set ofall subsets of X. A non-deterministic state machine accepts a given word ifthere is a possible combination/path of state transitions that leads to a finalstate. It can be thought as if the state machine tries all possible transitions

0-13

Page 15: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

in parallel, and in the end of input it checks if any given path would resideon a final state. The selection of state transition alternatives is not “random”because all possible alternatives are explored.

Figure 3 NFA is defined with 5-tuple (∑

, X, g, x0, F ), where

= {a, b}

X = {x0, x1}

F = {x0}

g(x0, a) = {x0, x1}, g(x0, b) = ∅

g(x1, a) = ∅, g(x1, b) = {x0}

0-14

Page 16: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.4.2 On determinism and non-determinism in state machines

For every non-deterministic state machine M there exists a deterministic statemachine M ′ that accepts the exactly same language, that is, L(M) = L(M ′).

Non-deterministic state machines does not make new languages possible ascompared to deterministic ones. However, a non-deterministic state machinecan be significantly smaller (in the number of states) than a deterministicversion. A non-deterministic state machine with n states can be convertedinto a deterministic one with O(n2) states. Thus, non-determinism can be auseful modeling tool.

0-15

Page 17: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

d0

d1

a

d2

1

d3

2

1 2

n 0

n 1

a

n 2

a

1 2

Figure 4: A deterministic and a non-deterministic version of a finite statemachine that accepts inputs “a11...” and “a22...”. The non-deterministicversion is smaller.

0-16

Page 18: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.4.3 Definition: a non-deterministic finite state machine with ǫ-moves

Meegan’s Second Law: If something happens, it must be possible.— Charles (Chip) Meegan, NASA Scientist

The definition of non-deterministic state machines can be further extendedwithout losing “compatibility” with deterministic variants. A non-deterministic

finite state machine with ǫ-moves (ǫ-NFA) is like an NFA, but it can make atransition without reading input. The state transition function g is definedas a mapping g : X × (

∪{ǫ}) 7→ P (X) which allows a transition with anǫ-symbol. The state machine does not read an input symbol on an ǫ-move.

ǫ-moves do not make the set of possible languages larger compared to NFAs(or DFAs), but it makes modeling easier due to fewer states and transitionsthat need to be mapped.

For every ǫ-NFA there is an NFA that accepts the same language. Asa consequence, for every ǫ-NFA there is also a DFA that accepts the samelanguage.

0-17

Page 19: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.4.4 Example: a union of languages using ǫ-moves

Combining two state machines to accept a union of their respective languagesis easy to do with ǫ-moves. See Figure 5. Assume the number of states inthese machines n1 and n2. The number of states in the combined machine isn1 + n2 + 2. As a deterministic machines this machine would in a worst casehave n1n2 states.

0-18

Page 20: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

M1

M2

a ... c

E

eps

x .. . z

epsS

eps

eps

Figure 5: An example of a non-deterministic finite state machine with ǫ-moves

0-19

Page 21: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.5 A minimal state machine

A state machine is minimal when it accepts a given language but there existsno state machine that accepts the same language with fewer states. A finitestate machine can be algorithmically minimized in polynomial time with re-spect to the number of states. The minimized machine accepts the exactlysame language. Figure 6 shows an example of minimization. In practice, theminimization can be done in O(n logn) time with respect to the number ofstates n. One example of a minimization algorithm can be found from [1].

0-20

Page 22: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

original s tate machine

a minimized s ta te machine

a

b

xc

y

x y

a/b

x

cy

Figure 6: Minimizing a finite state machine

0-21

Page 23: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.6 Finite state machines with output

Previous state machines did not have output. They only accepted or rejectedthe input. A finite state machine with output is similar to a FSM. It hasoutput, but it does not have final states.

2.6.1 Definition: Mealy machine

A mealy machine is a 6-tuple M = (∑

,∆, X, g, f, x0), where

X, x0 ja g the same as in FSM

is a finite input alphabet

∆ is a finite output alphabet

f is an output function, f : X ×∑

7→ ∆

A Mealy machine is similar to a finite state machine, but it outputs onesymbol for each state transition. It does not have final states, so it does not

0-22

Page 24: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

define a language. Output function f is defined for each pair (x, a), where x

is a state and a is a symbol.A Moore machine is a special case of Mealy machine where the output

function only depends on the state but not the input symbol. In a Mooremachine: f : X 7→ ∆

2.6.2 Example: A synchronous digital circuit as a Mealy machine

A synchronous digital circuit (RTL) can be modeled as a Mealy machine.

• Circuit’s state holding elements (registers and memory) define the set ofpossible states X. n bits of state holding elements implies |X| = 2n.

•∑

(alphabet) consists of all possible inputs to the circuit. For m inputbits |

| = 2m.

• g is the circuit logic that defines the next state for the circuit

• x0 is the state of state holding elements after a reset.

0-23

Page 25: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

• ∆ is the output alphabet that defines the set of all possible outputs forthe circuit. p bits of output implies |∆| = 2p.

• f is the output function that models the logic for outputs of the circuit(not feedback)

From RTL point of view, Mealy machine output can react to new inputwithin one clock cycle. Moore machine output can react to new input withintwo clock cycles. In Moore machines first the internal state is changed, afterwhich the output changes.

2.6.3 An example communication system

Figure 7(a) implements a simple communication system using a Moore ma-chine. Figure 7(b) implements the same system using a Mealy machine. Thesystem receives message “a” for which it responds to with a message “t”. Themessage is resent unless it is acknowledged. The system returns to the initialstate when the message is acknowledged.

The Moore machine is larger than Mealy machine because it needs a sep-arate state to output the “transmitted” message.

0-24

Page 26: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

I/idle

M/preparing

a

T/waiting

t t imeou t1

S/ t ransmi t ted

acknowledge

t imeou t2

(a) A Moore machine

I

M

a/preparing

T

t/wait ing

acknowledgement / t ransmi t t ed

t imeout1 / re t ransmi t

(b) A Mealy machine

Figure 7: A simple communication system

0-25

Page 27: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.7 Mealy machine verification

There are three main classes of methods for Mealy machine verification:

• Equivalence checking: test that two mealy machines are similar in be-havior (not in implementation)

• Model checking: verify that a given model behaves as expected

• Theorem proving: verify that certain properties are held by using math-ematical theorem proving tools

These often require some manual effort, testing and design time planning.

2.7.1 Equivalence checking

Boolean equivalence checking (BEC) is used to compare models that have thesame state holding elements. Circuit inputs and registers are “flattened” intounified inputs. Circuit outputs and feedback to registers are “flattened” intounified outputs. The outputs of two flattened models are verified to implementthe same logic.

0-26

Page 28: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

The problem is much harder when the state holding elements are different.This problem is called sequential equivalence checking.

There are several methods to compare circuit logic of two systems:

• Trivial (small) logic systems can be verified by comparing logic tables oftwo models. The size of the logic table grows exponentially with respectto inputs.

• Canonical forms will try to reduce logic into comparable symbolic form.For example, logic is transformed into boolean products and sums: e.g.f = ab+ c or f = (a+ c)(b+ c)

• Binary decision diagram (BDD) is used to construct a recursive defini-tion tree for each output bit. If the trees are similar the logic is thesame. This often requires careful design and verification plan to makeverification feasible. Also, this often requires mathematical proofs andproblem “slicing” into tractable subproblems.

• Automatic test pattern generation (ATPG) and Satisfiability testing (SAT)are used to automatically construct inputs that will try to show a conflict

0-27

Page 29: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

between the two models.

0-28

Page 30: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.7.2 Model checking

Model checking is used to verify that certain requirements and properties arevalid for all (valid) input.

• The model has static and dynamic properties

• Static properties hold for all times. For example, both read and write

signals may not be simultaneously “ones” in output

• Dynamic properties depend on the state or time. For example, if theregister bit was a “one” in the previous clock cycle, now it must be a“zero”.

• Static and dynamic properties can be modeled with assertions and tem-

poral logic. These can be embedded into HDL models.

Usually model checking works by trying to “lead” the system into a statewhere temporal logic or assertions fail. This can be done with random walkor monte carlo simulation of states, for example.

0-29

Page 31: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.7.3 Theorem proving

• Prove properties of the system by using mathematical logic

• Based on axioms and claims given by the system designer

0-30

Page 32: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

2.8 Finite state machine extensions

Finite state machines can be extended in many ways. The problem withextensions is increased complexity in theory and models. Finite state machinesare often used for their simplicity, and thus, additional features are not broughtinto the model.

2.8.1 Example: Finite state machine with variables

An FSM may not accept input of the form anbn for all values of n becausethat would require an infinite number of states. However, the model can beextended with memory or variables that does not increase the number of statesbut affects state transitions. Figure 8 is shows that kind of machine. Variablev counts how many “a” symbols have been received. An equal number of “b”symbols is required so that the whole input is accepted. Reading “a” increasesthe variable, “b” decreases it. v must be zero in the final state.

0-31

Page 33: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

x0 x1a /v := 1

a /v := v + 1

x2b/v := v - 1

b/v := v - 1

x3v = = 0 / e p s

Figure 8: State machine that accepts input of the form anbn˜∀n > 0

3 Petri nets

3.1 Introduction to Petri nets

• Petri net is a model of concurrent processes

• Models concurrent actions without time

• Consists of places, transitions, tokens and arcs

• Places are presented with circles, tokens with with small dots insidecircles

• Arcs connect places and transitions together

0-32

Page 34: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

t1p1 p2

p3 p4t2

t1p1 p2

p3 p4t2

Figure 9: Two simple Petri nets. In the upper net, t1 is not enabled. In thelower net, t2 is enabled, and consequently fired; two tokens are consumed fromp3 and one token is produced into p4.

• Transitions are marked with lines perpendicular to arcs

Figure 9 shows two simple Petri nets. p1 to p4 are places and t1 and t2 aretransitions. There are two arcs from place p1 to transition t1, but only onetoken at place p1.

• Places having arcs to transition t are called input places of transition t

• Places having arcs coming from t are called output places of t

0-33

Page 35: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

• Transition t is enabled when there are sufficiently many tokens in allinput places of t

• If there are n arcs from place p to t, t can not be enabled before p hasat least n tokens

– In figure 9, transition t1 is not enabled, because two arcs connectplace p1 to t1, but there is only one token at p1.

– Transition t2 is enabled because two arcs connect place p3 to t2 andthere are two tokens at p3.

• Assume:

– Places p and q are connected with transition t

– There are n arcs from p to t and m arcs from t to q

– Transition t is enabled and fired

– Then, as a consequence, n tokes are consumed from place p and m

tokens are produced to place q

0-34

Page 36: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

– Example: In figure 9, transition t2 consumes two tokens from p3and produces one token to place p4

• Any place (or transition) can have output arcs to any number of transi-tions (or place)

– Which transition is fired is nondeterministic

– Any number of transitions can happen simultaneously providedthat there are enough tokens

• Similarly, any place (or transition) can have input arcs from any numberof transitions (or place)

• Petri net is a dynamic system that communicates tokens between placesthrough transitions

– Real data is not transferred with tokens in basic Petri nets, butthere are extensions to Petri nets that model different types of datatransfered from one place to another

0-35

Page 37: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

3.2 Definition of Petri nets

A Petri net is a 5-tuple N = (P, T,A,w, ~x0), where

P is a finite set of places

T is finite set of transitions

A is a set or arcs, A ⊆ (P × T ) ∪ (T × P )

w is a weight function, w : A 7→ IN

~x0 is an initial marking vector, ~x0 ∈ IN |P |

Markings are tokens located at places. Marking vector ~x0 contains theini-tial number of tokens for each place. Marking vector can be considered the

state for Petri nets.Let x(p) denote number of tokens at place p. Let I(t) denote incoming

places of transition t, and O(t) the output places of transition t. Then, tran-sition t is enabled in state ~x if

x(p) ≥ w(p, t) ∀p ∈ I(t)

0-36

Page 38: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

Let N = (P, T,A,w, ~x0) be a Petri net with P = {p0, p1, . . . , pn−1} and~x = [x(p0), . . . , x(pn−1)] be a marking for the n places.

Transition function G : (INn × T ) 7→ INn defines the next state when thePetri net is at state ~x and takes transition t:

G(~x, t) =

{

~x′ if x(p) ≥ w(p, t) ∀p ∈ I(t)~x otherwise

with ~x′ = [x′(p0), . . . , x′(pn−1)], where

x′(pi) = x(pi)− w(pi, t) + w(t, pi) for 0 ≤ i < n

That is, if a transition is not enabled, markings can not change. If atransition is enabled, the markings in input places pi ∈ I(t) are reduced byw(pi, t) and markings in output places pj ∈ O(t) are increased by w(t, pj).

It is said that state ~y is immediately reachable from state ~x if there existsa transition t such that G(~x, t) = ~y. The reachability set R(~x) is the set ofstates that can eventually be reached by repeatedly firing transitions.

0-37

Page 39: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

3.3 Petri net state transitions with matrix algebra

One can model Petri net state transitions with matrix algebra. Assume Petrinet N has m transitions {tj} and n places {pi}. The incidence matrix A isan m× n matrix whose (j, i) entry is

aj,i = w(tj , pi)− w(pi, tj)

For transition tj define an unit vector

~u = [0, . . . , 1, 0, . . . , 0]

with 1 at position j. The state transition for transition j can then be formu-lated as

~x′ = ~x+ ~uA

Several transitions can take place with a single matrix operation by sum-ming transition unit vectors together. If there are N transitions ~ui (that canhappen simultaneously), state ~x can be updated in one pass:

~x′ = ~x+ (∑N

i=1~ui)A

0-38

Page 40: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

input 1

Petri Net modelinput 2

o u t p u t 1

o u t p u t 2

o u t p u t 3

Figure 10: The I/O of a Petri net modeled as places.

3.4 A Petri net as a component in hierarchical model

Petri nets can be composed in a hierarchical manner. One can denote a fullPetri net with a single shape, and draw transitions outside the net. Figure 10shows a Petri net model connecting with places at higher hierarchy levels.

0-39

Page 41: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

3.5 Sequential and parallel composition with Petri nets

3.5.1 Customer service example

Figure 11 shows a server model where customers arrive at input p1 (a), andthe service is enabled by an initial marking at p3.

The service takes place by firing t1, after which new arriving customerscan not be enabled before there is a marking at p3 again. The service is beingprocessed at subfigure (b).

The customer is finally deported in subfigure (c). The customer goes to p4and the initial marking is returned to state p3 so that new customers can beserviced.

3.5.2 Composition in sequence and parallel

Figure 12 shows sequential and parallel compositions of Petri nets. Each circlecan represent a full Petri net.

0-40

Page 42: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

p1p3

p2

p4

(a)

t1

t2

p1p3

p2

p4

t1

t2

p1p3

p2

p4

t1

t2

(b) (c)

Figure 11: Customer service Petri net model

0-41

Page 43: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

(a)

(b)

Figure 12: (a) A sequential and (b) a parallel composition of two Petri nets.

0-42

Page 44: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

Two customer services in sequence Figure 13 shows two servers con-nected sequentially together. Note, this is not strictly pipelining, becausethere can be arbitrary number of customers waiting at p4. Places are notsimilar to registers, which can hold only a single value, but they are similar tobuffers that can hold arbitrary many values.

Two customer services in parallel Figure 14 shows two customer servicesconnected in parallel. Two customers can be served simultaenously.

Petri nets are easy to connect in sequence, parallel, or a combination ofsequential and parallel processing.

Fork and join operations Fork and join can be used to enable and limitparallelism in composite models of Petri nets. Figure 15 shows forking andjoining operations. Each place an represent a full Petri net. These are oftenused in parallel computation.

0-43

Page 45: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

p1p3

p2

p4

t1

t2

t3

t4

p5

p6

p7

Figure 13: Two servers are connected sequentially together.

0-44

Page 46: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

p2

t1

t2

t3

t4

p1

p3

p6

p4

p5

Figure 14: Two servers connected in parallel.

0-45

Page 47: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

Fork

Join

Figure 15: Fork and join operations.

0-46

Page 48: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

3.5.3 Example: a mutual exclusion problem

Consider processes A and B modifying a shared data integer z. Both processeswill increment the variable by 1. Integer x is private to process A, and y isprivate to process B.

Process A: Process B:

x := z; y := z;

x := x + 1; y := y + 1;

z := x; z := y;

Assume that processes A and B become active simultaneously. The orderin which the shared data integer z is accessed is usually undefined in parallelcomputation, unless mutual exclusion and/or synchronization is used.

0-47

Page 49: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

The following order of events could take place:

A: x := z;

B: y := z;

A: x := x + 1;

A: z := x;

B: y := y + 1;

B: z := y;

If z was initially 0, we would expect normally that z = 2 in the end,because two processes incremented the variable by 1. However, in the orderof events we presented, the result would be z = 1.

This is a classic problem with shared integers, also known as a race con-

dition. A mutual exclusion (e.g. a semaphore, mutex or spinlock) or a syn-cronization primitive (e.g. atomic operations) is needed to fix the problem.

Figure 16 shows a Petri net solution to the mutual exclusion problem.Place p4 controls mutual exclusion. There is only one token in place p4 andthus concurrent processes A and B can not access z simultaneously.

0-48

Page 50: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

Criticalsection

Criticalsection

p1

p3

p6

t1

t3

p7

p2

t2

t4

p4 p5

Process A Process B

Figure 16: Mutual exclusion solution with Petri nets for two processes.

0-49

Page 51: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

3.5.4 Example: producer and consumer relationship

Figure 17 shows a producer / consumer relationship. Place p1 produces ma-terial into buffer B, and place p2 consumes material from the buffer.

It is important to note that:

• The producer can indefinitely produce new material into the buffer

• The rate of production is undefined since Petri nets do not model time.From the model perspective, no product might ever be produced.

• The buffer B is infinite, which contradicts reality

0-50

Page 52: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

B

p1

Produce

Producer

Put in buffer

p2

Consumer

Consume

Removefrombuffer

Figure 17: Producer / consumer relation ship as a Petri net.

0-51

Page 53: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

3.6 Properties of Petri nets

Boundedness A place p in Petri net N = (P, T,A,w, ~x0) is k-bounded ork-safe if there can be at most k tokens at place p. Petri net N is k-boundedif all its places are k-bounded.

Conservation A Petri net is conservative if the total number of tokens doesnot change over time.

Reachability Reachability problem is a question whether or not a state ~x

can be reached from initial state ~x0. Unfortunately there does not exist anefficient algorithm to test reachability, except in some special cases.

0-52

Page 54: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

Liveness A transition is deadlocked in state ~x if it can not be enabled inany state reachable from ~x.

Consider two processes both acquiring two resources in different order withrespect to liveness. Assume that only one process may hold a single resourceat a time. If a process tries to allocate a resource that is already allocated forsome other process, it must wait until the resource is unallocated.

Process A: Process B:

allocate(resource1); allocate(resource2);

allocate(resource2); allocate(resource1);

use(resource1); use(resource1);

use(resource2); use(resource2);

free(resource2); free(resource1);

free(resource1); free(resource2);

The system deadlocks if process A first allocates resource1 and then processB allocates resource2, because neither can release its resource before gettingthe other resource. The solution is to always allocate the same resources in thesame order. Both processes should first allocate resource1 and then resource2.

0-53

Page 55: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

Think, what would the Petri nets look like for broken and corrected sys-tems?

0-54

Page 56: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4 Process networks and dataflow models

• Process networks is a concurrent model of computation (MoC) that is asuperset of data flow models

– As a directed graph, each arc represents a FIFO queue for com-munication and each node represents an independent, concurrentprocess

• Dataflow networks is a special case of process networks where nodes areactors that respond to firing rules

– Dataflow applies series of transformations to a data stream

0-55

Page 57: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4.1 Process networks

• Process networks is a model of computation that was originally devel-oped for modeling distributed systems

• However, it has proven its convenience for modeling signal processingsystems

• Process networks are also called Kahn process networks after Gilles Kahnwho first introduced them in 1974

– http://en.wikipedia.org/wiki/Kahn_Process_Network

• Departing from the original process networks by Kahn, a number of morespecific models have been derived

0-56

Page 58: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4.2 Applying process networks

• Natural model for describing signal processing systems where infinitestreams of data are incrementally transformed by processes executing insequence or parallel

• Nevertheless, this model of computation does not require multitaskingor parallelism and usually neither infinite queues

• Process networks have found many applications in modeling embeddedsystems as it is typical for embedded systems to be designed to operateinfinitely with limited resources

0-57

Page 59: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4.3 Execution semantics

• Process networks are directed graphs

– Nodes represent processes

– Arcs are infinite FIFO queues that connect the processes

• Writing to a channel is non-blocking, i.e. it always succeeds immediately(a consequence of infinite FIFOs)

– No real world system satisfies this requirement because of limitedmemory

• Reading from a channel is blocking

– If a process tries to read from an empty input it is suspended untilit has enough input data and the execution context is switched toanother process or level

• A process may not ”test” channel for presence of data

0-58

Page 60: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

– A process can not simultaneously wait two channels for data. Itmust block on one or the other channel.

• At any given point a process is either “enabled” or “blocked” waitingfor data on one of its channels

• Processes produce tokens (data elements) that are sent along a commu-nication channel, and consumed by the destination process

• Communication channels are the only way processes may exchange in-formation

• Kahn process network systems are deterministic

– History of tokens produced/consumed does not depend on execu-tion order

– Same history of input tokens for the network always produces thesame overall result. The network is timing independent.

• Stream is a finite or infinite sequence X of data elements or tokens xi:X = {x1, x2, . . .}

0-59

Page 61: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

• Process is in this sense a functional mapping from input to outputstreams

• Two properties of process networks:

– Termination: Does the network execute forever?

– Boundedness: Can it be executed with bounded buffers?

• These properties are undecidable in finite time for the general case

– Under some restrictions, we can study and classify process networksbefore execution

0-60

Page 62: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4.4 What makes KPN a suitable MoC?

• Each process is a sequential program that consumes tokens from itsinputs queues and produces tokens to the output queues

– It is not necessary that a process consumes any tokens as it can bea source process that only outputs data. Also, it need not produceanything as it is a sink process that only reads data.

– The sequential program may issue reads and writes to any port inany order or quantity, but determinism property must be preserved

• Each queue has exactly one source and one destination

– Each queue produces data to exactly one process

– Each queue gets data from exactly one process

• A process is blocked when it tries to read from a channel with insufficientdata. It resumes when there is enough data again.

0-61

Page 63: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

• Although writing is generally non-blocking, blocking write can be usedto be able to use bounded queues (but deadlocks must be avoided withsufficient buffering)

• Process network has no global (shared) data

– Concurrency can be implemented safely

• Scheduling is transparent to the user

• Process networks support hierarchy and scalability

0-62

Page 64: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4.5 Dataflow networks

• Dataflow networks are very closely related to process networks

• Arcs represent FIFO queues

• Nodes represent actors (not processes)

• Actors use firing rules instead of responding to the simple blocking-readsemantics of process networks

– Specify how many tokens must be available on every input for theactor to fire

• When an actor fires, it consumes a finite number of tokens and producesalso a finite number of output tokens

• Process can be formed by repeated firings of a dataflow actor

0-63

Page 65: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4.6 Firing rules

• An actor may have more than one firing rule

• Evaluation of the firing rules is sequential

– Rules are sequentially evaluated until at least one of them is satis-fied

– Thus, an actor can only fire if one or more of its firing rules aresatisfied

• In general, synchronous dataflow actors have a single firing rule:

– A number of tokens that must be available at each of the inputs

– For example, an adder with two inputs has a single firing rule thateach input must at least have one token

• Breaking down processes into smaller units such as dataflow actors fir-ings, makes efficient implementations possible

0-64

Page 66: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

• Restricting the type of dataflow actors to those that have a predictableconsumption and production pattern

– Possible to perform static, off-line analysis to bound memory

• Processes are freely interleaved by a scheduler that determines the se-quence of actor firings

– The biggest advantage is that the cost of process suspension and re-sumption is avoided in software implementations (pre-emptive mul-titasking and process switching in operating systems)

• In many signal processing applications the firing sequence can be deter-mined statically at compile time

– The class of dataflow process networks where this is possible arecalled synchronous dataflow networks

• Dataflow graphs have data-driven semantics

0-65

Page 67: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

– Availability of operands enables the operator and hence sequencingconstraints follow only from data availability

• Principal strength of dataflow networks is that they do not over-specifyan algorithm by imposing unnecessary sequencing constraints betweenoperators

0-66

Page 68: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4.7 Synchronous dataflow

• Synchronous dataflow (SDF) is a special case of dataflow networks

• The number of tokens consumed and produced by an actor is knownbefore the execution begins

• The same behavior repeats in a particular actor every time it is fired

• To make feedback connections possible, arcs can have initial tokens

– Every initial token represents an offset between the token producedand the token consumed at the other end

– It is a unit delay and is represented by a diamond in the middle ofthe arc

• Schedule can be performed statically

• As the execution of the graph is going to be repeated the compiler shouldjust construct one complete cycle of the periodic schedule

0-67

Page 69: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

db ca

ge f

D

h

2D

1 1 1 1 1 1

11

1 1 1 2

2

2 1

1

Figure 18: Two synchronous dataflow graphs (SDFs)

0-68

Page 70: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

– Complete cycle is defined as the sequence of actor firings that re-turns the graph to its original state

• From the static information of the network we can construct a topologymatrix that contains relations between produced/consumed tokens inevery arc

– The element ij is defined as the number of tokens produced on theith arc by the jth actor

– Although it is only a partial info because there is no informationon the number of initial tokens on each arc we can use the matrixto build the static schedule

0-69

Page 71: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4.8 Boolean dataflow

• SDF is rarely enough for representing large parts of a program

• A more general model is needed to represent data-dependent iteration,conditionals and recursion

– We can generalize synchronous dataflow to allow conditional, data-dependent execution and still use the balance equations

• Boolean dataflow (BDF) is an extension of synchronous dataflow thatallows conditional token consumption and production

• By adding two simple control actors like switch and select we can buildconditional constructs like if-then-else and do-while loops

• Switch actor has two inputs (control and data) and two outputs (T andF). It gets a control token and then copies a token from data input tothe appropriate output (T or F), determined by the boolean value of thecontrol token

0-70

Page 72: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

• Select actor has three inputs (control, T and F) and one output. It getsa control token and then copies a token from the appropriate input (T orF) to the output, determined by the boolean value of the control token.

• These actors are not SDF because the number of produced/consumedtokens is not fixed and depends on an input boolean control

0-71

Page 73: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

p1 p1

SWITCH

SELECT

SWITCH

SELECTT

T

T

T

F

F

F

F

p5

p4p3

p2

p2 p3

p4

Figure 19: Two boolean dataflow (BDF) models representing branching onthe left and a loop construct on the right

0-72

Page 74: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4.9 On dataflow scheduling

• Dataflow scheduling can be classified as data-driven (eager execution),demand-driven (lazy execution), or a combination of the two

– In eager execution a process is activated as soon as it has enoughdata as required by any of its firing rules

– In lazy execution a process is activated only if the consumer processdoes not have enough data tokens

• When using bounded scheduling (limited number of buffers) three rulesmust be applied:

– A process is suspended when trying to read from an empty input

– A process is suspended when trying to write onto a full queue

– On artificial deadlock, increase the capacity of the smallest fullqueue until its producer can fire

0-73

Page 75: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4.10 Summary

• Process networks and dataflow networks are graphically depicted as di-rected graphs

• In process networks, nodes represent processes, and arcs are infiniteFIFO queues that connect the processes

– Non-blocking write, blocking read

• In dataflow networks, nodes represent actors, and arcs are FIFO queues

– Actors use firing rules to fire execution

• Synchronous dataflow is a special case of dataflow networks

– Number of tokens produced and consumed must be fixed

– Consequently, this allows to perform static schedule

0-74

Page 76: TKT-1527: Modelsofcomputation of zeros and even number of ones. In state x3 it has an ... for every ǫ-NFA there is also a DFA that accepts the ... The minimized machine accepts the

4.11 Attribution

• Some parts of process and dataflow networks were copied from XavierAmatriain’s PhD thesis located at http://www.create.ucsb.edu/~xavier/Thesis/

References

[1] Axel Jantsch, Modeling embedded systems and SoC’s: concurrency and

time in models of computation, Elsevier, 2004.

0-75