Imperative Data Parallelism (Correctness)

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

Modular and Verified Automatic Program Repair Francesco Logozzo, Thomas Ball RiSE - Microsoft Research Redmond.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Models of Concurrency Manna, Pnueli.
1 Symbolic Execution for Model Checking and Testing Corina Păsăreanu (Kestrel) Joint work with Sarfraz Khurshid (MIT) and Willem Visser (RIACS)
1/20 Generalized Symbolic Execution for Model Checking and Testing Charngki PSWLAB Generalized Symbolic Execution for Model Checking and Testing.
K. Rustan M. Leino RiSE, Microsoft Research, Redmond joint work with Peter Müller and Jan Smans Lecture 0 1 September 2009 FOSAD 2009, Bertinoro, Italy.
PARALLEL PROGRAMMING ABSTRACTIONS 6/16/2010 Parallel Programming Abstractions 1.
Maria-Cristina Marinescu Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology A Synthesis Algorithm for Modular Design of.
15-740/ Oct. 17, 2012 Stefan Muller.  Problem: Software is buggy!  More specific problem: Want to make sure software doesn’t have bad property.
Runtime Refinement Checking of Concurrent Data Structures (the VYRD project) Serdar Tasiran Koç University, Istanbul, Turkey Shaz Qadeer Microsoft Research,
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.
Programming -- Levels of Difficulty Sequential –Termination –Determinism Concurrent –Non-Termination –Non-Determinism Distributed –Multiple Computers –Partial.
CORRECTNESS CRITERIA FOR CONCURRENCY & PARALLELISM 6/16/2010 Correctness Criteria for Parallelism & Concurrency 1.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Module 8 Enhancing User Interface Responsiveness.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
OpenMP Lab Antonio Gómez-Iglesias Texas Advanced Computing Center.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Recursion Topic 5.
Regression Testing with its types
Chapter 6 CS 3370 – C++ Functions.
Parallel Patterns Reduce & Scan
Selection (also known as Branching) Jumail Bin Taliba by
Repetition (While-Loop) version]
CS 326 Programming Languages, Concepts and Implementation
Chapter 8 – Software Testing
CSE 374 Programming Concepts & Tools
Testing and Debugging.
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Data Structures and Algorithms
Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
White-Box Testing Using Pex
Design by Contract Fall 2016 Version.
Common Correctness and Performance Issues
CS October 2008 The while loop and assertions
Conditional Statements
CS 1430: Programming in C++ No time to cover HiC.
Practical Parallel and Concurrent Programming
Introduction to Object-Oriented Programming with Java--Wu
Improving the structure of existing code
Loops CIS 40 – Introduction to Programming in Python
Chapter 6: Repetition Statements
Data Races and Locks Unit 2.a 6/22/2010
Guest Lecture by David Johnston
A Refinement Calculus for Promela
Patterns for Programming Shared Memory
CIS 199 Final Review.
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CUTE: A Concolic Unit Testing Engine for C
Introduction to Programming
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
CSE 1020:Software Development
Patterns for Programming Shared Memory
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Software Design Lecture : 39.
CSS161: Fundamentals of Computing
Loops CGS3416 Spring 2019 Lecture 7.
CSE 332: Concurrency and Locks
CSE 206 Course Review.
Lecture 20 Parallel Programming CSE /8/2019.
Testing Slides adopted from John Jannotti, Brown University
Lecture 6 - Recursion.
Presentation transcript:

Imperative Data Parallelism (Correctness) Unit 1.b 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Acknowledgments Authored by Thomas Ball, MSR Redmond 4/4/2019 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Concepts Parallel.Invoke Parallel.ForEach Schedules and determinism Code Concept Parallel.Invoke Parallel.ForEach Schedules and determinism Assertions/Invariants Unit Testing Correctness Concept 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Parallel.Invoke int x = 0; Parallel.Invoke( static void Invoke(params Action[] actions); int x = 0; Parallel.Invoke( () => { x=1; }, () => { x=2; } ); Console.WriteLine(“x={0}”, x); Alpaca Project ParallelSamples.cs 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Parallel DAG and Happens-before Edges x=0 x=1 x=2 WriteLine(x) 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

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/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Different schedules, different outputs x=0 x=0 x=1 x=2 x=2 x=1 WriteLine(x) WriteLine(x) x = 2 x = 1 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Determinism For the same initial state, observe the same final state, regardless of the schedule Determinism desirable for most data-parallel problems 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Parallel Ray Tracing: Deterministic void Render(Scene scene, Color[,] rgb) { Parallel.For(0, screenHeight, (y) => for (int x = 0; x < screenWidth; x++) rgb[x,y] = TraceRay(new Ray(scene,x,y)); } }); 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

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 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

System vs. Unit Testing System Testing Unit 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 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

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 Project RayTracerTest.cs 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

IEnumerable and Parallel.ForEach Parallel.ForEach is not limited to integer ranges and arrays! Generic enumerations IEnumerable<T> Lists, sets, maps, dictionaries, … Works for generics only. 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Parallel.ForEach public static ParallelLoopResult ForEach<TSource>( IEnumerable<TSource> source, Action<TSource> body ); 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Speedup Demo: Antisocial Robots fps = frames per second 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Speedup: Over 3x on a 4-core! 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

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)); } 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Key Data Structures r1 r2 struct RoomPoint { public int X; public int Y; } class Robot { public RoomPoint Location; List<Robot> _robots; Robot[][] _roomCells; (0,0) r1 r2 _roomCells; 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

SimulateOneStep(Robot r1) Determine new cell for r1 Move r1 to new cell, if not already occupied r1 r2 r1 r2 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

r r1 r2 r r1 r2 r r2 r1 1 2 3 4 PerformSimulationStep 1 2 3 4 r r1 r2 PerformSimulationStep PerformSimulationStep 1 2 3 4 r r1 r2 1 2 3 4 r r2 r1 Non-determinism is acceptable in anti-social robots. 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

r r1 r2 r r2 1 2 3 4 1 2 3 4 But this is not acceptable. 1 2 3 4 r r1 r2 1 2 3 4 r r2 But this is not acceptable. 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Pigeonhole Principle “Two robots can’t occupy the same cell” If it is true before execution of PerformSimulationStep then it should be true afterward, regardless of sequential/parallel implementation foreach (var in _robots) Debug.Assert(_roomCells[r.Location.X,r.Location.Y] == r, “Can’t have two robots in the same cell!”); 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Assert Statement Assert(e) Helpful assertions have messages: e a Boolean expression (state predicate) e should always evaluate true when statement executes; otherwise program has an error Helpful assertions have messages: Assert(balance>=0, “account balance should be non-negative”) 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Invariant State predicate e is invariant to program fragment S provided that If predicate e is true before execution of S then Then predicate e is true after execution of S So, State predicate “Two robots can’t occupy the same cell” Is invariant to PerformSimulationStep 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

1. Antisocial Robots has a Bug AntisocialRobots.csproj 1. Antisocial Robots has a Bug 2. It’s Hard to Expose Concurrency Bugs! First, run the application and choose a board size of 20x20. Now, enable parallelism and checking. Play around for a while. Use F5 to reset the board. Try to get the error message dialog to pop up. Then use the Alpaca test instead. 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Run Alpaca [UnitTestMethod] to get more reliable reproduction of bug First, run the application and choose a board size of 20x20. Now, enable parallelism and checking. Play around for a while. Use F5 to reset the board. Try to get the error message dialog to pop up. Then use the Alpaca test instead. Alpaca Project RobotSimulationInterferenceTest.cs 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

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! 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Two Bugs in Three Lines: Updating Robot r’s Location 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); } 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Order of Statements Leading to Invariant Failure 1 2 3 4 r r1 r2 Order of Statements Leading to Invariant Failure SimulateOneStep(r1) SimulateOneStep(r2) Time 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); 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Order of Statements Leading to Invariant Failure 1 2 3 4 r r2 Order of Statements Leading to Invariant Failure SimulateOneStep(r1) 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); 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Question: What is the Second Bug? Think about the struct RoomPoint Come up with a scenario Ordering of statements leading to invariant violation 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Parallel.For/ForEach and 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 Abstracting away from this detail: purity!!! 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com

Parallel Programming with Microsoft .NET Chapter 2 (Parallel Loops) Parallel.For/ForEach Appendix B (Debugging and Profiling Parallel Applications) 6/16/2010 Practical Parallel and Concurrent Programming DRAFT: comments to msrpcpcp@microsoft.com