Imperative Data Parallelism (Correctness)

Similar documents
A Short Introduction to Hoare Logic

Program Analysis Part I : Sequential Programs

Flow Interfaces Compositional Abstractions of Concurrent Data Structures. Siddharth Krishna, Dennis Shasha, and Thomas Wies

Flow Interfaces Compositional Abstractions of Concurrent Data Structures. Siddharth Krishna, Dennis Shasha, and Thomas Wies

In this episode of The Verification Corner, Rustan Leino talks about Loop Invariants. He gives a brief summary of the theoretical foundations and

Automata-Theoretic Model Checking of Reactive Systems

Path Testing and Test Coverage. Chapter 9

Path Testing and Test Coverage. Chapter 9

What happens to the value of the expression x + y every time we execute this loop? while x>0 do ( y := y+z ; x := x:= x z )

Programming Languages and Compilers (CS 421)

Deductive Verification

The algorithmic analysis of hybrid system

Sequential Logic (3.1 and is a long difficult section you really should read!)

CSE20: Discrete Mathematics

INF 4140: Models of Concurrency Series 3

Design Patterns and Refactoring

Mathematical Induction. How does discrete math help us. How does discrete math help (CS160)? How does discrete math help (CS161)?

Static Program Analysis

Algorithms Exam TIN093 /DIT602

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

Model Checking: An Introduction

CS 7B - Spring Assignment: Ramsey Theory with Matrices and SFML. due 5/23/18

Automatic Verification of Parameterized Data Structures

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

Hoare Logic (I): Axiomatic Semantics and Program Correctness

Foundations of Computation

Problem One: Order Relations i. What three properties does a binary relation have to have to be a partial order?

Marwan Burelle. Parallel and Concurrent Programming. Introduction and Foundation

Double Header. Model Checking. Model Checking. Overarching Plan. Take-Home Message. Spoiler Space. Topic: (Generic) Model Checking

Design of Distributed Systems Melinda Tóth, Zoltán Horváth

Recent developments in concurrent program logics

Floyd-Hoare Style Program Verification

Robust Programs with Filtered Iterators

A Humble Introduction to DIJKSTRA S A A DISCIPLINE OF PROGRAMMING

Register machines L2 18

CSC 1700 Analysis of Algorithms: Warshall s and Floyd s algorithms

Lecture 22: Multithreaded Algorithms CSCI Algorithms I. Andrew Rosenberg

Reasoning About Imperative Programs. COS 441 Slides 10b

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

Hoare Logic: Reasoning About Imperative Programs

Routing Algorithms. CS60002: Distributed Systems. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CSE613: Parallel Programming, Spring 2012 Date: May 11. Final Exam. ( 11:15 AM 1:45 PM : 150 Minutes )

Synthesizing from Components: Building from Blocks

Synchronous Reactive Systems

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

Computer Science Introductory Course MSc - Introduction to Java

Blocking Synchronization: Streams Vijay Saraswat (Dec 10, 2012)

Real Time Operating Systems

The Weakest Failure Detector to Solve Mutual Exclusion

Power System Analysis Prof. A. K. Sinha Department of Electrical Engineering Indian Institute of Technology, Kharagpur. Lecture - 21 Power Flow VI

The Underlying Semantics of Transition Systems

Decision Procedures. Jochen Hoenicke. Software Engineering Albert-Ludwigs-University Freiburg. Winter Term 2016/17

A Simple Implementation Technique for Priority Search Queues

Latches. October 13, 2003 Latches 1

Infinite-State Backward Exploration of Boolean Broadcast Programs

1 Introduction. 1.1 The Problem Domain. Self-Stablization UC Davis Earl Barr. Lecture 1 Introduction Winter 2007

Ivy: Safety Verification by Interactive Generalization

Safety and Liveness. Thread Synchronization: Too Much Milk. Critical Sections. A Really Cool Theorem

27 : Distributed Monte Carlo Markov Chain. 1 Recap of MCMC and Naive Parallel Gibbs Sampling

Generalized Consensus and Paxos

Principles of Computing, Carnegie Mellon University. The Limits of Computing

Efficient Dependency Tracking for Relevant Events in Concurrent Systems

Abstractions and Decision Procedures for Effective Software Model Checking

Dynamic Semantics. Dynamic Semantics. Operational Semantics Axiomatic Semantics Denotational Semantic. Operational Semantics

Formal Reasoning CSE 331. Lecture 2 Formal Reasoning. Announcements. Formalization and Reasoning. Software Design and Implementation

Dataflow Analysis. A sample program int fib10(void) { int n = 10; int older = 0; int old = 1; Simple Constant Propagation

Computational Boolean Algebra. Pingqiang Zhou ShanghaiTech University

Invariant Generation for Parametrized Systems using Self-Reflection

Software Verification

An Invitation to Mathematics Prof. Sankaran Vishwanath Institute of Mathematical Sciences, Chennai. Unit I Polynomials Lecture 1A Introduction

Heaps Induction. Heaps. Heaps. Tirgul 6

ICS141: Discrete Mathematics for Computer Science I

Hoare Calculus and Predicate Transformers

Lecture Notes: Axiomatic Semantics and Hoare-style Verification

Lecture 4. Finite Automata and Safe State Machines (SSM) Daniel Kästner AbsInt GmbH 2012

Parallelism and Machine Models

Axiomatic Semantics. Operational semantics. Good for. Not good for automatic reasoning about programs

Proofs of Correctness: Introduction to Axiomatic Verification

Program Analysis and Verification

How to deal with uncertainties and dynamicity?

Verifying Array Manipulating Programs

Abstract Interpretation and Static Analysis

Notation. Bounds on Speedup. Parallel Processing. CS575 Parallel Processing

Evaluation and Validation

CS-206 Concurrency. Lecture 8 Concurrent. Data structures. a b c d. Spring 2015 Prof. Babak Falsafi parsa.epfl.ch/courses/cs206/ remove(c) remove(b)

Central Algorithmic Techniques. Iterative Algorithms

Algorithmic verification

Unit: Blocking Synchronization Clocks, v0.3 Vijay Saraswat

1 Lamport s Bakery Algorithm

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

CS558 Programming Languages

Deterministic Program The While Program

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

Course Announcements. John Jannotti (cs32) Scope, Collections & Generics Feb 8, / 1

Supplement to Sampling Strategies for Epidemic-Style Information Dissemination 1

Searching. Sorting. Lambdas

On the Probabilistic Symbolic Analysis of Software. Corina Pasareanu CMU-SV NASA Ames

Softwaretechnik. Lecture 13: Design by Contract. Peter Thiemann University of Freiburg, Germany

Transcription:

Imperative Data Parallelism (Correctness) Unit 1.b 1

Acknowledgments Authored by Thomas Ball, MSR Redmond 9/4/2010 2

Concepts Code Concept Parallel.Invoke Parallel.ForEach Correctness Concept Schedules and determinism Assertions/Invariants Unit Testing 3

tjb2 Parallel.Invoke static void Invoke(params Action[] actions); int x = 0; Parallel.Invoke( () => { x=1; }, () => { x=2; } ); Console.WriteLine( x={0}, x); Alpaca ParallelSamples.cs Project 4

Slide 4 tjb2 Maybe motivate with a prior slide with a more realistic example where Parallel.For is not quite what we want. i.e. what if we have two specific things we want to do in parallel? Then have this simple example where stuff breaks. Tom Ball, 8/14/2010

Parallel DAG and Happens-before Edges x=0 x=1 x=2 WriteLine(x) 5

Schedule, Informally A topological sort (serialization) of the nodes in a parallel DAG - A sequential ordering of the nodes that respects the happens-before edges 6

Different schedules, different outputs x=0 x=0 x=1 x=2 x=2 x=1 WriteLine(x) WriteLine(x) x Practical = 2Parallel and Concurrent Programming x = 1 7

CS9 Determinism For the same initial state, observe the same final state, regardless of the schedule Determinism desirable for most dataparallel problems 8

Slide 8 CS9 How is determinism reflected on a happens-before graph? Caitlin Sadowski, 7/8/2010

Parallel Ray Tracing: Deterministic void Render(Scene scene, Color[,] rgb) { Parallel.For(0, screenheight, (y) => { for (intx = 0; x < screenwidth; x++) { rgb[x,y] = TraceRay(new Ray(scene,x,y)); } }); } 9

Unit Testing The goal of unit testing is to isolate each part of the program and show that the individual parts are correct A unit test is a closed program that sets up conditions to run a program unit and check the results 6/22/2010 10

System vs. Unit Testing System Testing Test entire application Needed to find integration errors Does not put much stress on individual components Unit Testing Better coverage, but more work Necessity for libraries and frameworks Good idea for tricky parallel/concurrent components 6/22/2010 11

Checking Determinism How can we test the correctness of the parallel Ray Trace application? Create unit test to compare the parallel version the sequential version Should we be satisfied with such tests? Do unit tests work well for parallel programs? Alpaca RayTracerTest.cs Project 12

IEnumerableand Parallel.ForEach Parallel.ForEach is not limited to integer ranges and arrays! Generic enumerations IEnumerable<T> Lists, sets, maps, dictionaries, 13

CS12 Parallel.ForEach public static ParallelLoopResult ForEach<TSource>( IEnumerable<TSource> source, Action<TSource> body ); 14

Slide 14 CS12 Could we add a graphic? Caitlin Sadowski, 7/8/2010

Speedup Demo: Antisocial Robots fps = frames per second 15

Speedup: Over 3x on a 4-core! 16

The Difference in the Code? void PerformSimulationStep() { if (naiveparallel.ischecked.value) { _robotsim.parallelstep(); } else { _robotsim.sequentialstep(); }... public void SequentialStep() { foreach (Robot robot in _robots) SimulateOneStep(robot); } public void ParallelStep() { Parallel.ForEach(_robots, r => SimulateOneStep(r)); } 17

Key Data Structures struct RoomPoint { public int X; public int Y; } (0,0) class Robot { public RoomPoint Location; } List<Robot> _robots; Robot[][] _roomcells; r1 _roomcells; r2 18

SimulateOneStep(Robot r1) Determine new cell for r1 Move r1to new cell, if not already occupied r1 r2 r1 r2 19

0 1 2 3 4 0 r r r r PerformSimulationStep 1 2 r r1 r2 r r r r r r PerformSimulationStep 0 1 2 3 4 0 1 2 3 4 0 1 2 r r r1 r r r r2 r r r r r r 0 1 2 r r r2 r r r r1 r r r r r r 20

0 1 2 3 4 0 1 2 r r r r r r1 r2 r r r r r r 0 1 2 3 4 0 1 2 r r r2 r r r r r r r r r 21

Pigeonhole Principle Two robots can t occupy the same cell foreach (var in _robots) Debug.Assert(_roomCells[r.Location.X,r.Location.Y] == r, Can t have two robots in the same cell! ); If it is true before execution of PerformSimulationStep then it should be true afterward, regardless of sequential/parallel implementation 22

Assert Statement Assert(e) e a Boolean expression (state predicate) eshould always evaluate true when statement executes; otherwise program has an error Helpful assertions have messages: Assert(balance>=0, account balance should be non-negative ) 23

Invariant State predicate e is invariantto program fragment S provided that Ifpredicate e is true before execution of S then Thenpredicate e is true after execution of S So, State predicate Two robots can t occupy the same cell Is invariant to PerformSimulationStep 24

AntisocialRobots.csproj 1. Antisocial Robots has a Bug 2. It s Hard to Expose Concurrency Bugs! 25

26

Run Alpaca [UnitTestMethod] to getmore reliable reproduction of bug Alpaca RobotSimulationInterferenceTest.cs Project 27

High-level Problem SimulateOneStep(r1) and SimulateOneStep(r2) interfere with one another when r1 wants to move to cell (X,Y), and r2 wants to move to cell (X,Y) Sequential version: invariant is maintained Parallel version: invariant breaks! 28

Two Bugs in Three Lines: Updating Robot r slocation SimulateOneStep(Robot r) { RoomPoint ptr; // compute new location of Robot r into ptr... // update robot location if (((ptr.x!= r.location.x) (ptr.y!= r.location.y)) && (_roomcells[ptr.x, ptr.y] == null)) { _roomcells[r.location.x, r.location.y] = null; _roomcells[ptr.x, ptr.y] = r; r.location = new RoomPoint(ptR.X, ptr.y); } 29

0 1 2 0 1 2 3 4 r r r r r r1 r2 r r r r r r Order of Statements Leading to Invariant Failure Time SimulateOneStep(r1) if (_roomcells[2,0] == null) _roomcells[1,1] = null; _roomcells[2,0] = r1; r1.location = (2,0); SimulateOneStep(r2) if (_roomcells[2,0] == null) _roomcells[3,1] = null; _roomcells[2,0] = r2; r2.location = (2,0); 30

0 1 2 0 1 2 3 4 r r r2 r r r r r r r r r SimulateOneStep(r1) Order of Statements Leading to Invariant Failure SimulateOneStep(r2) if (_roomcells[2,0] == null) _roomcells[1,1] = null; _roomcells[2,0] = r1; r1.location = (2,0); if (_roomcells[2,0] == null) _roomcells[3,1] = null; _roomcells[2,0] = r2; r2.location = (2,0); 31

Question: What is the Second Bug? Think about the struct RoomPoint Come up with a scenario Ordering of statements leading to invariant violation 32

Parallel.For/ForEachand Correctness No interference between delegates on different loop iterations Avoid Writing to Shared Memory Locations Avoid Calls to Non-Thread-Safe Methods No interference: implies determinism? Only the GUI thread can access GUI state Don t execute Parallel.For on the GUI thread 33

Parallel Programming with Microsoft.NET Chapter 2 (Parallel Loops) Parallel.For/ForEach Appendix B (Debugging and Profiling Parallel Applications) 34