Techniques and Structures in Concurrent Programming Wilfredo Velazquez.

Slides:



Advertisements
Similar presentations
Implementation and Verification of a Cache Coherence protocol using Spin Steven Farago.
Advertisements

Optimistic Methods for Concurrency Control By : H.T. Kung & John T. Robinson Presenters: Munawer Saeed.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Concurrency The need for speed. Why concurrency? Moore’s law: 1. The number of components on a chip doubles about every 18 months 2. The speed of computation.
A Lock-Free Multiprocessor OS Kernel1 Henry Massalin and Calton Pu Columbia University June 1991 Presented by: Kenny Graunke.
Pessimistic Software Lock-Elision Nir Shavit (Joint work with Yehuda Afek Alexander Matveev)
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Progress Guarantee for Parallel Programs via Bounded Lock-Freedom Erez Petrank – Technion Madanlal Musuvathi- Microsoft Bjarne Steensgaard - Microsoft.
PARALLEL PROGRAMMING with TRANSACTIONAL MEMORY Pratibha Kona.
“THREADS CANNOT BE IMPLEMENTED AS A LIBRARY” HANS-J. BOEHM, HP LABS Presented by Seema Saijpaul CS-510.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
CS 584. A Parallel Programming Model We need abstractions to make it simple. The programming model needs to fit our parallel machine model. Abstractions.
Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09.
Concurrency CS 510: Programming Languages David Walker.
CS510 Concurrent Systems Class 2 A Lock-Free Multiprocessor OS Kernel.
Chapter 2.3 : Interprocess Communication
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
What is Concurrent Programming? Maram Bani Younes.
© 2009 Matthew J. Sottile, Timothy G. Mattson, and Craig E Rasmussen 1 Concurrency in Programming Languages Matthew J. Sottile Timothy G. Mattson Craig.
Parallel Programming Philippas Tsigas Chalmers University of Technology Computer Science and Engineering Department © Philippas Tsigas.
CS510 Concurrent Systems Jonathan Walpole. A Lock-Free Multiprocessor OS Kernel.
Concurrency, Mutual Exclusion and Synchronization.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Games Development 2 Concurrent Programming CO3301 Week 9.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
6.852: Distributed Algorithms Spring, 2008 Class 13.
Maged M.Michael Michael L.Scott Department of Computer Science Univeristy of Rochester Presented by: Jun Miao.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
CS162 Week 5 Kyle Dewey. Overview Announcements Reactive Imperative Programming Parallelism Software transactional memory.
CSE 425: Concurrency II Semaphores and Mutexes Can avoid bad inter-leavings by acquiring locks –Guard access to a shared resource to take turns using it.
DOUBLE INSTANCE LOCKING A concurrency pattern with Lock-Free read operations Pedro Ramalhete Andreia Correia November 2013.
Wait-Free Multi-Word Compare- And-Swap using Greedy Helping and Grabbing Håkan Sundell PDPTA 2009.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Software Transactional Memory Should Not Be Obstruction-Free Robert Ennals Presented by Abdulai Sei.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Copyright © Curt Hill Concurrent Execution An Overview for Database.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Execution Replay and Debugging. Contents Introduction Parallel program: set of co-operating processes Co-operation using –shared variables –message passing.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Distributed Mutual Exclusion Synchronization in Distributed Systems Synchronization in distributed systems are often more difficult compared to synchronization.
SMP Basics KeyStone Training Multicore Applications Literature Number: SPRPxxx 1.
Concurrency and Performance Based on slides by Henri Casanova.
Agenda  Quick Review  Finish Introduction  Java Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
G.Anuradha Reference: William Stallings
Atomic Operations in Hardware
Chapter 4: Threads.
Shared Memory Programming
Thread Implementation Issues
Concurrency: Mutual Exclusion and Process Synchronization
Kernel Synchronization II
Chapter 4: Threads & Concurrency
Chapter 4: Threads.
CSE 542: Operating Systems
Presentation transcript:

Techniques and Structures in Concurrent Programming Wilfredo Velazquez

Outline Basics of Concurrency Concepts and Terminology Advantages and Disadvantages Amdahl’s Law Synchronization Techniques Concurrent Data Structures Parallel Correctness Treading A.P.I.’s

Basics of Concurrency  A concurrent program is any in which two or more of its modules or sections are run either by a separate process, or by another thread  Not much attention given historically  Concurrent programs are much more difficult to reason about and implement  Physical limits of modern processors are being reached, Moore’s Law no longer applies  Instead of faster processors, use more of them

Concepts and Terminology  Process  A ‘program’, which has its own memory space, stack, etc.  Difficult to communicate between processes –Message Passing Communication  Thread  A ‘sub-program’  Threads share all program features with that of their parent process. That is to say, same memory space, stack, etc.  Easy to communicate between threads –Shared Memory Communication

Concepts and Terminology  Concurrent Program  Processes/threads which execute tasks in an ordering relative to each-other that is not defined  Essentially covers all multi-process/multi-threaded programs  Parallelism  Processes/threads that execute completely simultaneously  Parallelism is more readily applied to sections of a program  Impossible in single-core processors (those still exist?)  Increased parallelism = more processors used  Atomic action  An action (instruction) that either happens, completely without interruption, or not at all  For many purposes, the idea that an action ‘looks’ atomic is enough to classify it as such

Advantages and Disadvantages  Advantages:  Concurrent Programs + More Processors = Faster Programs  Some problems more easily described in parallel environments  General Multitasking  Non-Determinism  Disadvantages  Concurrent Programs + Few Processors = Slower Programs  Most problems more difficult to implement in parallel environments  Non-Determinism

Amdahl’s Law  Relates the speed-up of a program when more processors are added  Has very limiting implications

Outline Basics of Concurrency Synchronization Techniques Mutual Exclusion and Locks The Mighty C.A.S. Lock-free and Wait-free Algorithms Transactional Algorithms Concurrent Data Structures Treading A.P.I.’s

Synchronization Techniques  These are techniques that assure program correctness in areas where the non-determinism inherited from a concurrent environment would cause undesirable behavior  Example: Let T1 and T2 be threads, x be a shared variable between them  x = 0;//initially  T1::x++;  T2::x++;  Value of x ?

Synchronization Techniques x++ becomes read x; add 1; write x; So T1 and T2’s instructions could occur in the following order: T1::read x //reading 0 T2::read x //reading 0 T1::add 1 //0+1 T2::add 1 //0+1 T1::write x //writing 1 T2::write x //writing 1

Mutual Exclusion and Locks  Algorithm that allows only one thread to execute a certain ‘area’ of code at a time  It essentially ‘locks out’ all other threads from accessing the area, thus ‘mutex’ and ‘lock’ are typically used synonymously  Varying algorithms exist for implementation, differing in robustness and performance  Typically easy to reason about their use  High overhead compared to other synchronization techniques  Can cause problems such as Deadlock, Livelock, and Starvation

The Mighty C.A.S.  Compare And Swap  Native instruction on many modern multiprocessors  Widely used in synchronizing threads  Cheap, compared to using locking algorithms  Expensive, compared to loading-storing as uses a hardware lock  ABA > CAS boolean CAS(memoryLocation, old, new) { If(*memoryLocation == old) { *memoryLocation = new; return true; } return false; }

Lock-Free and Wait-Free Algorithms  Wait-Free Algorithm  An algorithm is defined to be ‘wait-free’ if it guarantees that for any number of threads, all of them will make progress in a finite number of steps  Deadlock-free, Livelock-free, Starvation-free  Lock-Free Algorithm  An algorithm is defined to be ‘lock-free’ if it guarantees that for any number of threads, at least one will make progress in a finite number of steps  Deadlock-free, Livelock-free  All wait-free algorithms are also lock-free, though not vice versa  Note that neither definition actually forbids the use of locks, thus a lock-free algorithm could be implemented with locks

Transactional Algorithms  Inspired by database systems 1. Gather data from memory locations (optional) 2. Make local changes to the locations 3. Commit changes to the actual locations as an atomic step 4. If commit fails (another transaction occurred), start again  Essentially a generalization of CAS, except that no prior knowledge of the data is needed (for CAS we needed an ‘expected’ value)

Outline Basics of Concurrency Synchronization Techniques Concurrent Data Structures Safety and Liveliness Properties Differing Semantics Treading A.P.I.’s

Concurrent Data Structures  In sequential programming, data structures are invaluable as programming abstractions as they:  Provide abstraction of the inner-workings via interfaces  Provide a set of properties and guarantees as per what happens when certain operations are performed  Increase modularity of code  In concurrent programming they provide similar benefits, in addition to:  Allows threads to communicate in a simple and maintainable manner  Can be used as a focal point for the work done by multiple threads

Safety and Liveliness Properties  Safety  Assures that ‘nothing bad will happen’, for example, two calls to the ‘push’ function of a stack should result in two elements being added to the stack  Liveliness  Assures that progress continues  Deadlock  Livelock  Starvation  All bad!

Differing Semantics  Structures must share properties and guarantees with the sequential versions which they mimic, thus their operations must be deterministic (with a few exceptions)  Semantics of use and implementation differ greatly purely due to the concurrent environment  Example:  The result obtained from popping the stack is non- deterministic, even though the implementation of the interfaces themselves are deterministic

Differing Semantics  So how can we write the program in such a way that it is well-behaved for our purposes?  De-Facto standard: Use a lock  Parallelism suffers, as other threads may not operate at all during the entire given section of code  Introduces liveliness problems

Constructing Concurrent Data Structures  A concurrent data structure must abide by its sequential counter-part’s properties and guarantees when operations are performed on it  It must be ‘thread-safe’, no matter how many parallel calls are made to it, the data structure will never be corrupted  It should be free from any liveliness issues such as Deadlock  Just as sequential ones are constructed for abstraction, concurrent data structures should be opaque in their implementation

Constructing Concurrent Data Structures

 The sequential version of this data structure  Not suitable as-is for concurrent programming  Lacks any safety properties, though it has no liveliness issues  How can we resolve the issue?  Lock it

Constructing Concurrent Data Structures

 Safety is no longer a concern, though liveliness now is  Deadlock possible should a thread die during execution  Starvation in case of an interrupt  Lock overhead will overwhelm applications with many pops/push  Look back to original implementation; What sequential assumptions were made? (push)

Constructing Concurrent Data Structures  Correct, but original property lost: pushing on to a stack does not always place the element on the stack  Easy solution: Keep trying

Constructing Concurrent Data Structures  Pop implemented using the same logic:

Outline Basics of Concurrency Synchronization Techniques Concurrent Data Structures Treading A.P.I.’s pthreads M.C.A.S., W.S.T.M., O.S.T.M.

Threading API’s  pthreads  C library for multithreading. Contains utilities such as mutexes, semaphores, and others  Available on *nix platforms, though subset ports exist for windows  MCAS  A C API that allows the use of a software-built MCAS (Multiple-Compare-And-Swap) function  Very powerful, though larger overhead than CAS  WSTM  Word-Based Software Transactional Memory  API for easy use of the Transactional Model  Mixes normal objects with WSTM datatypes  Easy to implement on existing systems  OSTM  Object-Based Software Transactional Memory  Similar to WSTM, except that it is more streamlined in its implementation due to operating exclusively on its own data types  More difficult to implement on existing systems

Refferences  Concurrent Programming Without Locks  us/um/people/tharris/papers/2007-tocs.pdf us/um/people/tharris/papers/2007-tocs.pdf  MCAS, WSTM, OSTM implemented in paper  The art of Pultiprocessor Programming  By Maurice Herlihy, Nir Shavit  ontcover#v=onepage&q&f=false ontcover#v=onepage&q&f=false  DCAS is not a Silver Bullet for Nonblocking Algorithm Design 