You are on page 1of 18

Simulation of Multithreaded

Algorithms Using Petri-Object


Models

Stetsenko Inna V.
Dyfuchyna Oleksandra
Igor Sikorsky Kyiv Polytechnic Institute
Related works
• Peterson, J.: Petri Nets Theory and the
Modelling of Systems (1981)
• Kavi, K., Moshtaghi, A., Chen, D.: Modeling
Multithreaded Applications Using Petri
Nets(2002)
• Katayama, T., Kitano, S., Kita, Y., Yamaba, H.,
Okazaki, N.: Proposal of a Supporting Method
for Debugging to Reproduce Java Multi-
threaded Programs by Petri-net(2014).

© Stetsenko I.V., Dyfuchyna O. 2


Multithreaded programming and its
problems
Thread A Thread B Thread C

© Stetsenko I.V., Dyfuchyna O. 3


Stochastic Petri net

© Stetsenko I.V., Dyfuchyna O. 4


Petri-object models and simulation

Obj 3 Obj 2

Obj1 Event
initializing Obj 3
Common
place
Obj 1
Obj 2 Obj 4

© Stetsenko I.V., Dyfuchyna O. 5


Implementation of thread’s creating,
starting and ending
public static void main(String[] args) {
Thread thread = new Thread(new Runnable());
thread.start();
}

© Stetsenko I.V., Dyfuchyna O. 6


Implementation of thread’s locking
private final Lock lock = new ReentrantLock();
try{
lock = lock.tryLock();
…// synchronized actions
} finally {
lock.unlock();
}

© Stetsenko I.V., Dyfuchyna O. 7


Implementation of thread’s guarded
block
public synchronized void method() throws InterruptedException {
while (!condition()) {
wait();
}
…// some actions
notifyAll();
}

© Stetsenko I.V., Dyfuchyna O. 8


Implementation of thread’s shared
data access
public synchronized void incMethod(){
local ++;
}

© Stetsenko I.V., Dyfuchyna O. 9


The net of Petri-object Producer in connection
with the net of Petri-object Consumer

Consumer
Producer
signal from
notify
wait

trylock notify signal to


1

put
1 buffer’s free places
k take
lock unlock

buffer’s occupied places

© Stetsenko I.V., Dyfuchyna O. 10


The net of Petri-object Friend

loop bowB++
for{ imp{ trylockA trylockB bowBack
unlockAB
1000

1 bowA++
unlockA
1 lockA
1 unlockB
failure++
for} lockB

trylockB

failure

© Stetsenko I.V., Dyfuchyna O. 11


© Stetsenko I.V., Dyfuchyna O. 12
Connections between two Petri-objects

Friend B
tryLockB tryLockA

lockA lockB
1 1

tryLockA tryLockB

Friend A

© Stetsenko I.V., Dyfuchyna O. 13


Petri-objects creation program code
public static void main(String[] args) throws ExceptionInvalidNetStructure {
//…
class Friend extends PetriSim {
public Friend(String name, int loop) throws ExceptionInvalidNetStructure {
super(NetLibrary.CreateNetFriendUsingCores(name, loop, 2)); // 2 cores
}
public void addFriend(Friend other){
this.getNet().getListP()[7] = other.getNet().getListP()[2]; //lockOther = lock
this.getNet().getListP()[15] = other.getNet().getListP()[15]; // coresOther = cores
}
}
Friend friendA = new Friend("A", 1000);
Friend friendB = new Friend("B", 1000);
Friend friendC = new Friend("C", 1000);
Friend friendD = new Friend("D", 1000);

friendA.addFriend(friendB);
friendA.addFriend(friendC);
friendA.addFriend(friendD);

friendB.addFriend(friendA);
friendB.addFriend(friendC);
friendB.addFriend(friendD);

friendC.addFriend(friendA);
friendC.addFriend(friendB);
friendC.addFriend(friendD);

friendD.addFriend(friendA);
friendD.addFriend(friendB);
friendD.addFriend(friendC);
} © Stetsenko I.V., Dyfuchyna O. 14
Petri-object model program code
public static void main(String[] args) throws ExceptionInvalidNetStructure {
ArrayList<PetriSim> list = new ArrayList<>();

//Petri-objects creation

list.add(friendA);
list.add(friendB);
list.add(friendC);
list.add(friendD);

PetriObjModel model = new PetriObjModel(list);

model.setIsProtokol(false);
model.go(100000000);
}

© Stetsenko I.V., Dyfuchyna O. 15


Model’s experimental research of the threads conflict
dependence on values of time delays.
4• 3 = 12 threads
d is the time delay of transition “for{“ ( conforming to sleep() in program)
d•r is the time delay of other transitions, where r is a ratio of transitions time
delays (conforming to simple instructions performance)
f is failure relative frequency

f 0,8 d=100
0,7 d=10
0,6 d=1
0,5
0,4
0,3
0,2
0,1
0
r=0,001 r=0,01 r=0,1 r=0,5 r=0,8 r=1

© Stetsenko I.V., Dyfuchyna O. 16


The comparison of model’s result
and running of program
Number Multithreaded Simulation model Error
of threads program (d=100, r=0.01)

2 0.981450 0.978650 0.29%

12 0.959042 0.963417 0.46%

10
0.987777 0.979080 0.88%
(90 workers)
20
0.988047 0.980910 0.72%
(380 workers)
50
0.995212 0.981585 1.37%
(2450 workers)

© Stetsenko I.V., Dyfuchyna O. 17


Conclusion
In this research the method of transforming multithreaded
program code into Petri-object models is considered as well as
the ways of investigation of such models. The main
mechanisms of multithreading such as thread’s start, lock,
guarded block, shared data access are considered and placed
in appropriate fragments of stochastic multichannel Petri net.
Future development
• The hard question is finding real time values for program
instructions execution and it needs additional investigation
• Creation of the collection of Petri objects for multithread
program simulation
• The development of automated generation of Petri object
model according to the program code.

© Stetsenko I.V., Dyfuchyna O. 18

You might also like