COMP60621 Designing for Parallelism

Slides:



Advertisements
Similar presentations
The SPIN System. What is SPIN? Model-checker. Based on automata theory. Allows LTL or automata specification Efficient (on-the-fly model checking, partial.
Advertisements

The SPIN System. What is SPIN? Model-checker. Based on automata theory. Allows LTL or automata specification Efficient (on-the-fly model checking, partial.
CS 267: Automated Verification Lecture 8: Automata Theoretic Model Checking Instructor: Tevfik Bultan.
Models of Concurrency Manna, Pnueli.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Concurrency: introduction1 ©Magee/Kramer 2 nd Edition Concurrency State Models and Java Programs Jeff Magee and Jeff Kramer.
Sept COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 12 The Critical Section problem John Gurd, Graham Riley Centre for Novel.
The Spin Model Checker Promela Introduction Nguyen Tuan Duc Shogo Sawai.
1 Spin Model Checker Samaneh Navabpour Electrical and Computer Engineering Department University of Waterloo SE-464 Summer 2011.
Spin Tutorial (some verification options). Assertion is always executable and has no other effect on the state of the system than to change the local.
Chapter 2: Algorithm Discovery and Design
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
© 2009 Matthew J. Sottile, Timothy G. Mattson, and Craig E Rasmussen 1 Concurrency in Programming Languages Matthew J. Sottile Timothy G. Mattson Craig.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Correctness requirements. Basic Types of Claims Basic assertions End-state labels Progress-state labels Accept-state labels Never claims Trace assertions.
Scientific Computing By: Fatima Hallak To: Dr. Guy Tel-Zur.
Lecture 2 Foundations and Definitions Processes/Threads.
Sept COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 15 More Advanced Program Properties: Temporal logic and jSpin John Gurd,
Temporal Logic Model-checking with SPIN
CIS 842: Specification and Verification of Reactive Systems Lecture INTRO-Examples: Simple BIR-Lite Examples Copyright 2004, Matt Dwyer, John Hatcliff,
Introduction to Loops Iteration Repetition Counting Loops Also known as.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Lecture 4 Introduction to Promela. Promela and Spin Promela - process meta language G. Holzmann, Bell Labs (Lucent) C-like language + concurrency dyamic.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Software Systems Verification and Validation Laboratory Assignment 4 Model checking Assignment date: Lab 4 Delivery date: Lab 4, 5.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Sept COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 9 Promela, jSpin and the problem of Interference John Gurd, Graham Riley.
Agenda  Quick Review  Finish Introduction  Java Threads.
Sept COMP60611 Fundamentals of Parallel and Distributed Systems Lecture 8 Introduction to Modelling Concurrency John Gurd, Graham Riley Centre for.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Tutorial 2: Homework 1 and Project 1
C++ LANGUAGE MULTIPLE CHOICE QUESTION
14 Compilers, Interpreters and Debuggers
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 2 - Introduction to C Programming
Formal verification in SPIN
CSE 503 – Software Engineering
Atomic Operations in Hardware
Chapter 5: Control Structures II
Loop Structures.
Systems Analysis and Design
COMP60611 Fundamentals of Parallel and Distributed Systems
About the Presentations
Quick Test What do you mean by pre-test and post-test loops in C?
Lecture 2 Introduction to Programming
Chapter 5: Process Synchronization
Chapter 2 - Introduction to C Programming
UNIT 3 CHAPTER 1 LESSON 4 Using Simple Commands.
Shared Memory Programming
Chapter 15 Debugging.
COMP60611 Fundamentals of Parallel and Distributed Systems
COMP60621 Fundamentals of Parallel and Distributed Systems
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
An explicit state model checker
A Refinement Calculus for Promela
CSE 153 Design of Operating Systems Winter 19
Chapter 6: Synchronization Tools
Programming with Shared Memory - 2 Issues with sharing data
COMP60621 Designing for Parallelism
Foundations and Definitions
Programming with Shared Memory Specifying parallelism
COMP60621 Designing for Parallelism
COMP60611 Fundamentals of Parallel and Distributed Systems
CHAPTER 6 Testing and Debugging.
CSE 503 – Software Engineering
Presentation transcript:

COMP60621 Designing for Parallelism Lecture 4 Promela, jSpin and the problem of Interference John Gurd, Graham Riley Centre for Novel Computing School of Computer Science University of Manchester November 2012

Introduction Review of interference. Overview of jSpin and the Promela modelling language Use of assertions in model checking For checking safety properties Summary November 2012

The Oriental Garden problem - Interference People enter an ornamental garden through either of two turnstiles. Management wish to know how many are in the garden at any time. The concurrent program consists of two concurrent threads and a shared people ‘value’ variable. November 2012 From Magee & Kramer

Graphic of possible outcome After the East and West turnstile threads have each incremented the people count 20 times, the garden people counter is not the sum of the counts displayed. Counter increments have been lost. Why? November 2012 Magee & Kramer

Concurrent processes! west east Turnstyle threads for east and west may be executing the code for the increment function ‘at the same time’. west east shared code PC PC increment: read value write value + 1 program counter program counter We know, that without some form of locking to ensure mutual exclusion, writes can be lost and the wrong total computed. November 2012

The modelling strategy Our approach is to use a modelling language to explore the behaviour of parallel algorithms at the design stage. We can, of course, always build models of existing systems to investigate their behaviour. Our models will aim to capture the fundamental aspects of the concurrent behaviour. We wish to use tools to help us explore the behaviour of the models to help ensure that they are correct : do the right things, and don’t do wrong things i.e. have the right properties. November 2012

Spin and Promela We will take a fairly pragmatic approach to the use of the model checker tool, Spin, and its modelling language Promela. The main aim will be to see how we can use Spin to help us check that our parallel algorithms are ‘correct’. We will not worry too much about exactly how Spin works. We take an engineering approach… Though we will recognise the important role that the state diagram of a parallel program plays in the process. November 2012

jSpin overview A java front end to the Spin model checker Spin is widely used in industry See: http://spinroot.com See the Reference Sheet for installation instructions jSpin provides a simplified interface to the full Spin model checker. Good for teaching purposes. Spin is a sophisticated model checker! Spin takes in models (of concurrent systems) written in the Promela modelling language and provides support for checking properties of the underlying state transition diagram of the model. November 2012

Features of Spin Syntax checking of Promela models. ‘Random’ simulation of a Promela model. Executes a computation of the program. Promela supports non-deterministic programs so a program may have many computations. Interleaving as well as non-determinism in IF and DO statements (see later) is resolved randomly (i.e. a single computation is executed). ‘Interactive’ execution of a Promela model The user can explore computations by selecting the next statement to be executed (i.e. the next transition to be taken). ‘Guided’ execution If Spin finds an error, it can leave a ‘trail’ of a computation which led to the error, which can be examined in Guided mode. November 2012

Getting a feel for jSpin An example of a sequential program in Promela can be found in intDivisor.pml In ~griley/COMP60621/source/labs/extras/intDivisor Don’t worry about details of the syntax at this point. Load this program in jSpin. Execute it in Random mode. Use Interactive mode to step through the code. Note the use of assertions to check both pre- and post-conditions on the state of the sequential program. Verify (shows no errors). Change dividend to be 16, change the statement: remainder >= divisor; to remainder > divisor; Verify and note the error reported. November 2012

Example Promela code As an example of a parallel program, briefly look at cs_attempt1.pml In ~griley/COMP60621/source/labs/extras/CriticalSection. Execute in Random mode. Execute in Interactive mode and see how the choices of statements are mode available and change during execution. Verify and note no errors are reported. November 2012

More Spin features Spin can be used to check (verify) both safety and liveness properties Safety mode for safety properties. Acceptance mode and Non Progress mode safety and liveness properties specified in Temporal Logic – more later. Safety properties can be specified as assertions in the Promela code. We will look at the use of assertions in a future lecture. Spin supports the description of properties in linear temporal logic (LTL) Allows the checking of sophisticated safety and liveness properties. We will take an introductory look at LTL later. November 2012

Promela modelling language See Chapter 1 of “Principles of the Spin model Checker”, M. Ben-Ari, Springer, 2008. Available as an electronic resource from the John Rylands library. Also see Principles of Concurrent and Distributer Programming, 2nd Edition. Ben-Ari. A small language with a C-like syntax designed specifically to build models of concurrent systems that can be ‘checked’ using tools like Spin. Best way to learn a lauguage is to read and play! So, please read the book(s) and play with models using jSpin. November 2012

Programs in Promela Programs consist of a set of Processes Sequential units of execution Processes can have both global and local variables Programs do not have to have global variables. Promela supports channels for modelling distributed systems. Small number of (small) data types bool, byte, int, unsigned etc. Control statements based on guarded commands Introduced by Edgar Dijkstra. Well suited for expressing non-determinism. You may not be very familiar with guarded commands… yet. November 2012

Example: global variables Process definition. byte n=0; active proctype P() { byte temp; temp = n+1; n = temp; printf( “Process P, n=%d\n”,n); } active proctype Q() { printf( “Process Q, n=%d\n”,n); Process definition. Active means runs on startup. local variables C-like print statement November 2012

Notes In Promela, the semicolon (;) is used as a statement separator NOT as a statement terminator, as in C and Java… so they are not always necessary. This can be confusing… it is a language designers folly! Usually ok to use semicolons ‘naturally’, the compiler is fairly tolerent. November 2012

Modelling choices Statements in Promela are executed atomically. So n=n+1; is executed atomically in Promela but as we have seen, in most ‘real’ languages (C, Java, Fortran…) this would be executed as an atomic read of n (to a register variable) followed by an increment, followed by an atomic write (from a register).’ Allowing the possibility of interference. We can model this with three atomic statements: temp=n; temp=temp+1; n=temp; We could also model this in Promela as: temp=n+1; n=temp; Two atomic statements (or temp=n; n=temp+1;) We only need to model to the level which shows the effect we are interested in! In this case, we are modelling interference, so two atomic statements are sufficient. November 2012

Alternative version byte n; proctype P() { byte temp; temp = n+1; n = temp; printf( “Process P, n=%d\n”); } init() { n=0 ; atomic { run P(); Init() is special and is the first process to run. Used to init globals and create other processes. Atomic ensures both instances of P start together. November 2012

Init() and final output atomic { run P(); } (_nr_pr == 1) -> printf(“n=%d\n”,n); Waits for the ‘built-in’ variable containing the number of active processes to be 1 – i.e. only the init() process is ‘alive’ - before printing. This is an example of a guarded command. November 2012

Guarded commands – beware! Any alternative whose guard evaluates to true may be executed. non-determinism The else is only executed if none of the guards is true. Not quite what you are probably used to… guard command If :: a > 6 -> a+1; :: a < 6 -> a-1; :: else -> a=2*a; fi; November 2012

Guarded command – do loops Any alternative whose guard evaluates to true may be executed. non-determinism break exits the loop. Again, not quite what you are probably used to… guard command do :: a > 6 -> a-1; :: a < 6 -> a+1; :: a==6 -> break; od; November 2012

Counting loops – two ways #define N 10; active proctype P() { int sum=0; byte i=1; do :: i>N -> break; :: else -> sum=sum+1; i++; od; } #include “for.h” #define N 10; active proctype P() { int sum=0; for (i,1,N) sum=sum+i; rof(i); } A copy of file for.h needs to be available in the local directory. November 2012

An unfamiliar concept(?) – blocking statements in Promela In Promela, a conditional statement will only execute if it evaluates to true Otherwise the statement will block i.e. the process will wait until the expression becomes true: bool my_turn = false; my_turn; /* if my_turn=false, the process blocks */ a=2; /* statement executes once my_turn is set true */ The statement my_turn; is equivalent to my_turn==true; We could have used, say, !his_turn; equivalently! This makes sense in a multi-process environment! It provides a synchronisation mechanism between processes Presumably, another process will act to make the conditional true at some point to allow the blocked process to progress. November 2012

Other features of Promela In Promela, the use of global variables enables the simulation of ‘shared memory’ programs (for execution on shared memory machines). Promela also supports the message passing paradigm through channels. Channels can be used to pass data directly between the local variables of processes without using shared memory. Channels enable the modelling of distributed algorithms for use on distributed memory computers. We will focus on shared memory problems Relevant to programming multi-core processors. November 2012

Ornamental Garden - model #include "for.h" byte n=0; byte MAX=5 proctype T() { byte temp; for (i,1,MAX) temp=n+1; n=temp; rof (i); } init { atomic { run T(); } (_nr_pr == 1) -> printf("The value is %d\n", n); assert (n==2*MAX); November 2012

Use jSpin to investigate… Use Random mode and Interactive mode to investigate manually. Use the assertion on the result with Verify to generate a trail that can be examined. Problem: the update to the global variable ‘value’ are not atomic. The read and write statementent from both processes get interleaved and so can produce wrong results. Solution: use locks to ensure ‘mutual exclusion’ in the updates to the shared variable. i.e. only allow one process at a time to read and update. Do this in the model, to check all computations are ok, then in the code, so any actual computation will be ok – if the implementation is faithfull to the model. At least we will know the design is correct. November 2012

Fix using locks… model #include "for.h“ init { #include “lock.pml” bool lock=false; byte n=0; byte MAX=5 proctype T() { byte temp; for (i,1,MAX) setLock(lck); temp=n+1; n=temp; releaseLock(lck); rof (i); } init { atomic { run T(); } (_nr_pr == 1) -> printf("The value is %d\n", n); assert (n==2*MAX); November 2012

Test using jSpin and fix program Verify the assertion. That’s it! – tested for all possible computations! Can also use Random and Interactive mode to get a feel for how the program executes. Fix in the program and test: for(arrive=0;arrive<GARDEN_MAX;arrive++) { pthread_mutex_lock(&value_mutex); value++; pthread_mutex_unlock(&value_mutex); } November 2012

Hidden assumptions We have modelled the non-atomic behaviour of incrementing in ‘real’ programming languages, i.e. n=n+1; No reason to think we have missed anthing here. We have modelled the action of pthread locks in Promela (O.k, I have...). We appear to have modelled pthread locks to a level sufficient to demonstrate solving the interference problem. But note there are complexities in the behaviour of locks that we have not modelled To do with more than two processes and fairness issues. Our (my!) Promela model does not necessarily generalise to more than two processes. See this more clearly when we have looked at liveness and fairness. The lesson is to be aware of the limitations of a model. Practice and experience. November 2012

Summary Interference bugs are generally extremely difficult to locate in parallel programs. The general solution is to give processes mutually exclusive access to shared data. Through, for example, the use of so-called monitor constructs. Encapsulated shared data and mutually exclusive access methods. Our next task is to choose an application to model and see how jSpin can help us to check its correctness. We will choose the classic critical section problem. Illustrates most of the common, fundamental concurrency issues. The lessons learned generalise to real, complex applications. November 2012