CORRECTNESS CRITERIA FOR CONCURRENCY & PARALLELISM 6/16/2010 Correctness Criteria for Parallelism & Concurrency 1.

Slides:



Advertisements
Similar presentations
Bounded Model Checking of Concurrent Data Types on Relaxed Memory Models: A Case Study Sebastian Burckhardt Rajeev Alur Milo M. K. Martin Department of.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
Addressing the Challenges of Current Software. Questions to Address Why? What? Where? How?
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
Chapter 15: Transactions Transaction Concept Transaction Concept Concurrent Executions Concurrent Executions Serializability Serializability Testing for.
COMP 655: Distributed/Operating Systems Summer 2011 Dr. Chunbo Chu Week 7: Consistency 4/13/20151Distributed Systems - COMP 655.
Lecture 8: Three-Level Architectures CS 344R: Robotics Benjamin Kuipers.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
LineUp: Automatic Thread Safety Checking
Consistency Models Based on Tanenbaum/van Steen’s “Distributed Systems”, Ch. 6, section 6.2.
1/25 Concurrency and asynchronous computing How do we deal with evaluation when we have a bunch of processors involved?
SEERE, Neum 2009 Runtime verification of Java programs using ITL Vladimir Valkanov, Damyan Mitev Plovdiv, Bulgaria.
Interface Automata 29-September Modeling Temporal Behavior of Component Component behaves with Environment Traditional (pessimistic) approach –
Transaction Processing Lecture ACID 2 phase commit.
Copyright W. Howden1 Lecture 13: Programming by Contract.
CPSC 668Set 16: Distributed Shared Memory1 CPSC 668 Distributed Algorithms and Systems Fall 2006 Prof. Jennifer Welch.
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
Software Testing and Quality Assurance
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
CSE 486/586 CSE 486/586 Distributed Systems PA Best Practices Steve Ko Computer Sciences and Engineering University at Buffalo.
© 2009 Matthew J. Sottile, Timothy G. Mattson, and Craig E Rasmussen 1 Concurrency in Programming Languages Matthew J. Sottile Timothy G. Mattson Craig.
15-740/ Oct. 17, 2012 Stefan Muller.  Problem: Software is buggy!  More specific problem: Want to make sure software doesn’t have bad property.
Linearizability By Mila Oren 1. Outline  Sequential and concurrent specifications.  Define linearizability (intuition and formal model).  Composability.
Chapter 14 System Testing.
Tammy Dahlgren with Tom Epperly, Scott Kohn, and Gary Kumfert Center for Applied Scientific Computing Common Component Architecture Working Group October.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 3 (26/01/2006) Instructor: Haifeng YU.
Chapter 25 Formal Methods Formal methods Specify program using math Develop program using math Prove program matches specification using.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
CS 261 – Data Structures Preconditions, Postconditions & Assert.
Today’s Agenda  Reminder: HW #1 Due next class  Quick Review  Input Space Partitioning Software Testing and Maintenance 1.
Internet Security CSCE 813 Communicating Sequential Processes.
Software Reliability. Risks of faulty software  Example: –Therak 25, –AT&T network failure –Airport traffic control  Costs of software errors can be.
Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness.
Motivation  Parallel programming is difficult  Culprit: Non-determinism Interleaving of parallel threads But required to harness parallelism  Sequential.
Concurrency Properties. Correctness In sequential programs, rerunning a program with the same input will always give the same result, so it makes sense.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Program Correctness. The designer of a distributed system has the responsibility of certifying the correctness of the system before users start using.
Parallel Data Structures. Story so far Wirth’s motto –Algorithm + Data structure = Program So far, we have studied –parallelism in regular and irregular.
An algorithm of Lock-free extensible hash table Yi Feng.
Random Logic l Forum.NET l State Machine Mechanism Forum.NET 1 st Meeting ● December 27, 2005.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
Agenda  Quick Review  Finish Introduction  Java Threads.
1 7 Systems Analysis and Design in a Changing World, 2 nd Edition, Satzinger, Jackson, & Burd Chapter 7 The Object-Oriented Approach to Requirements.
Software Testing Reference: Software Engineering, Ian Sommerville, 6 th edition, Chapter 20.
Maitrayee Mukerji. INPUT MEMORY PROCESS OUTPUT DATA INFO.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Lecture 20: Consistency Models, TM
Background on the need for Synchronization
Input Space Partition Testing CS 4501 / 6501 Software Testing
Algorithm Analysis CSE 2011 Winter September 2018.
Turing Machines Acceptors; Enumerators
Lecture 8 Thread Safety.
Specifying Object Interfaces
Consistency Models.
CS 220: Discrete Structures and their Applications
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
CSE 486/586 Distributed Systems Consistency --- 1
Lecture 22: Consistency Models, TM
Background and Motivation
Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract, Computer,
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Programming with Shared Memory Specifying parallelism
Parallel Data Structures
Presentation transcript:

CORRECTNESS CRITERIA FOR CONCURRENCY & PARALLELISM 6/16/2010 Correctness Criteria for Parallelism & Concurrency 1

Contracts – Correctness for Sequential Code 6/16/2010Correctness Criteria for Parallelism & Concurrency 2

Contracts – Correctness for Sequential Code Assertions A predicate expected to hold at a particular program point Precondition A predicate expected to hold at a function call A failure can be blamed on the caller Postcondition A predicate expected to hold at a function return A failure can be blamed on the callee 6/16/2010Correctness Criteria for Parallelism & Concurrency 3

int Divide(int n, int d) { return n/d; } Code Contracts for.Net 6/16/2010Correctness Criteria for Parallelism & Concurrency 4

int Divide(int n, int d) { Contract.Requires( 0 != d ); return n/d; } Preconditions using Requires 6/16/2010Correctness Criteria for Parallelism & Concurrency 5

int Divide(int n, int d) { Contract.Requires( 0 != d ); Contract.Ensures( Contract.Result () * d <= n && Contract.Result () * d > n-d ); return n/d; } Preconditions using Ensures 6/16/2010Correctness Criteria for Parallelism & Concurrency 6

Example: Library APIs 6/16/2010Correctness Criteria for Parallelism & Concurrency 7

Example: System Call API 6/16/2010Correctness Criteria for Parallelism & Concurrency 8

Correctness for Concurrency & Parallelism Reuse contracts written for sequential code Relate correctness of concurrent/parallel executions to correctness of appropriate sequential executions 6/16/2010Correctness Criteria for Parallelism & Concurrency 9

Coin Sorting Example Vending Machine Sort Coins accept reject 6/16/2010Correctness Criteria for Parallelism & Concurrency 10

Use Contracts for Correctness Criteria SortCoins accepts a set of coins and returns a set of bad ones Parallelizing SortCoins should not change the contract SortCoins(…) { Contract.Requires(…); Contract.Ensures(…); } Sequential Implementation SortCoins(…) { Contract.Requires(…); Contract.Ensures(…); } Parallel Implementation 6/16/2010Correctness Criteria for Parallelism & Concurrency 11

Parallelizing can sometimes produce correct but different outputs Sequential SortCoins accept reject Parallel SortCoins accept reject The order of coins can change 6/16/2010Correctness Criteria for Parallelism & Concurrency 12

Restrictive Contracts Can Limit Parallelism The order of coins returned by SortCoins might be different from the input order Do care about the total amount returned If the contracts enforce the ordering, resulting parallelization might be unacceptably slow Design interfaces in such a way that contracts are not restrictive 6/16/2010Correctness Criteria for Parallelism & Concurrency 13

Strategies For Reasoning About Parallel Code How do we know if a parallel loop satisfies its contract? Reasoning about parallel executions is hard General Strategy: 1. Make sure that every parallel behavior is equal to some sequential behavior 2. Convince that the sequential behavior satisfies the contract 6/16/2010Correctness Criteria for Parallelism & Concurrency 14

Independent Loops Let m(1) … m(n) be the loop iterations Two iterations m(j) and m(k) (j ≠ k) are dependent if they access the same memory location and at least one of them is a write. Loop iterations are independent if no two of them are dependent 6/16/2010Correctness Criteria for Parallelism & Concurrency 15

Dependent Loops Dependencies need to be made explicit No data races Reason that order of dependent operations don’t matter e.g. These operations are commutative and associative Recall: reduce/scan 6/16/2010Correctness Criteria for Parallelism & Concurrency 16

Determinism: A New Correctness Criteria Pre and post conditions do two things Specify how the function behaves sequentially Enforce the same behavior when parallelized int ComputeSum ( IEnumerable input) { Contract.Requires ( input != null); Contract.Ensures ( Contract.Result () == input.Sum(i => i) )); //implementation } int ComputeSum ( IEnumerable input) { Contract.Requires ( input != null); Contract.Ensures ( Contract.Result () == input.Sum(i => i) )); //implementation } 6/16/2010Correctness Criteria for Parallelism & Concurrency 17

Determinism Contract Allows you to check parallel correctness Without having to specify the sequential contract The output of the function does not depend on task interleavings for a given input int ComputeSum ( IEnumerable input) { Contract.Requires ( input != null); IsDeterministic ( Contract.Result (), input ); //implementation } int ComputeSum ( IEnumerable input) { Contract.Requires ( input != null); IsDeterministic ( Contract.Result (), input ); //implementation } 6/16/2010Correctness Criteria for Parallelism & Concurrency 18

Determinism Checking Is same as saying For some deterministic function F Very useful when specifying F is tedious Contract.IsDeterministic ( output, {input1, input2, … }) Contract.IsDeterministic ( output, {input1, input2, … }) Contract.Ensures ( output == F ( input1, input2, … ) ) Contract.Ensures ( output == F ( input1, input2, … ) ) 6/16/2010Correctness Criteria for Parallelism & Concurrency 19

Determinism Checking Is same as saying For some deterministic function F Contract.IsDeterministic ( output, {input1, input2, … }, comp) Contract.IsDeterministic ( output, {input1, input2, … }, comp) Contract.Ensures ( comp(output, F ( input1, input2, … ) ) Contract.Ensures ( comp(output, F ( input1, input2, … ) ) 6/16/2010Correctness Criteria for Parallelism & Concurrency 20

Strategies for Checking Determinism 6/16/2010Correctness Criteria for Parallelism & Concurrency 21

Concurrent Objects Can be called concurrently by many threads Examples Work Stealing Queue 6/16/2010Correctness Criteria for Parallelism & Concurrency 22

Concurrent Objects Can be called concurrently by many entities Examples Work Stealing Queue C Runtime library Operating System Data bases 6/16/2010Correctness Criteria for Parallelism & Concurrency 23

Correctness Criteria Informally called “thread safety” What does “thread safety” mean to you? 6/16/2010Correctness Criteria for Parallelism & Concurrency 24

A Simple Concurrent Object Sequential Queue Add(item) TryTake() returns an item or “empty” Size() returns # of items in queue Consider ConcurrentQueue and its relationship to Queue 6/16/2010Correctness Criteria for Parallelism & Concurrency 25

Let’s Write a Test q = new ConcurrentQueue(); q.Add(10) t=q.TryTake() Assert( ? )

q = new ConcurrentQueue(); q.Add(10) t=q.TryTake() Assert: q.Size() is 0 or 1 Assert: q.Size() is 0 or 1 Let’s Write a Test

q = new ConcurrentQueue(); q.Add(10); t=q.TryTake(); Assert: t = fail && q.size() = 1 || t = 10 && q.size() = 0 Assert: t = fail && q.size() = 1 || t = 10 && q.size() = 0 Let’s Write a Test

q = new ConcurrentQueue(); q.Add(10) t=q.TryTake() q.Add(10) t=q.TryTake() q.Add(20) u = q.TryTake() q.Add(20) u = q.TryTake() Assert ( ? ) Let’s Write a Test

q = new ConcurrentQueue(); Assert: q.Size() == 0 t = 10 || t = 20 u = 10 || t = 20 u != t Assert: q.Size() == 0 t = 10 || t = 20 u = 10 || t = 20 u != t q.Add(10) t=q.TryTake() q.Add(10) t=q.TryTake() q.Add(20) u = q.TryTake() q.Add(20) u = q.TryTake() Let’s Write a Test

Linearizability The correctness notion closest to “thread safety” A concurrent component behaves as if only one thread can enter the component at a time 6/16/2010Correctness Criteria for Parallelism & Concurrency 31

“Expected” Behavior? Client 1 Client 2 Client 3 Add 10returnAdd 20return TryTakereturn10 TryTake return “empty” 6/16/2010Correctness Criteria for Parallelism & Concurrency 32

“Expected” Behavior? Client 1 Client 2 Client 3 Add 10returnAdd 20return TryTakereturn10 TryTake return “empty” Client 1 Client 2 Client 3 Add 10return Add 20return TryTakereturn20 TryTakereturn “empty” 6/16/2010Correctness Criteria for Parallelism & Concurrency 33

Linearizability Component is linearizable if all operations Appear to take effect atomically at a single temporal point And that point is between the call and the return “As if the requests went to the queue one at a time” Client 1 Client 2 Client 3 Add 10returnAdd 20return TryTakereturn10 TryTake return “empty” 6/16/2010Correctness Criteria for Parallelism & Concurrency 34

Linearizability vs Seriazliability? Serializability All operations (transactions) appear to take effect atomically at a single temporal point 6/16/2010Correctness Criteria for Parallelism & Concurrency 35

Linearizability vs Seriazliability? Serializability All operations (transactions) appear to take effect atomically at a single temporal point Linearizability All operations to take effect atomically at a single temporal point That point is between the call and return 6/16/2010Correctness Criteria for Parallelism & Concurrency 36

Serializable behavior that is not Linearizable Linearizability assumes that there is a global observer that can observe that Thread 1 finished before Thread 2 started Thread 1 Thread 2 Add 10return TryTakereturn “empty” 6/16/2010Correctness Criteria for Parallelism & Concurrency 37

Serializability does not compose The behavior of the blue queue and green queue are individually serializable But, together, the behavior is not serializable Thread 1 Thread 2 Add 10return TryTakereturn “empty”Add 10return TryTakereturn “empty” 6/16/2010Correctness Criteria for Parallelism & Concurrency 38

Formalizing Linearizability Define the set of observables for each operation Call operation: value of all the arugments Return operation: An event: Thread Id, Object Id, Call/Return, Operation, Observables 6/16/2010Correctness Criteria for Parallelism & Concurrency 39 Add 10return

A Concurrent History Sequence of Events 6/16/2010Correctness Criteria for Parallelism & Concurrency 40 Thread 1 Thread 2 Add 10return TryTake return “empty”

A Concurrent History Sequence of Events 6/16/2010Correctness Criteria for Parallelism & Concurrency 41 Thread 1 Thread 2 Add 10return TryTake return “empty” We will only focus on single object histories

A Concurrent History Sequence of Events 6/16/2010Correctness Criteria for Parallelism & Concurrency 42 Thread 1 Thread 2 Add 10return TryTake return “empty” Also, we will only focus on complete histories – every call has a return Also, we will only focus on complete histories – every call has a return

A Serial History A concurrent history where every call is followed by its matching return 6/16/2010Correctness Criteria for Parallelism & Concurrency 43 Thread 1 Thread 2 Add 10return TryTake return “empty”

Sequential Specification of an Object The set of all serial histories define the sequential behavior of an object Assume we have a mechanism to enumerate this set and store the set in a database 6/16/2010Correctness Criteria for Parallelism & Concurrency 44

Equivalent Histories Two concurrent histories are equivalent if Each thread performs operations in the same order And sees the same observations 6/16/2010Correctness Criteria for Parallelism & Concurrency 45 Client 1 Client 2 Add 10returnAdd 20return TryTakereturn10 Client 1 Client 2 Add 10returnAdd 20return TryTake return10

Concurrent Operations in a History Two operations p and q are concurrent in a history if their duration overlap ! (p.ret < q.call || q.ret < p.call) 6/16/2010Correctness Criteria for Parallelism & Concurrency 46 Thread 1 Thread 2 Add 10return TryTake return “empty”

Concurrent Operations in a History Two operations p and q are concurrent in a history if their duration overlap ! (p.ret < q.call || q.ret < p.call) Non-Concurrent operations define a “performed- before” order 6/16/2010Correctness Criteria for Parallelism & Concurrency 47 Client 1 Client 2 Add 10returnAdd 20return TryTake return10

Linearizability A concurrent history is linearizable if it is equivalent to a (serial) history in the sequential specification, Such that all operations that are “performed before” in the concurrent history are also “performed before” in the serial history 6/16/2010Correctness Criteria for Parallelism & Concurrency 48

6/16/2010Correctness Criteria for Parallelism & Concurrency 49