How D can make concurrent programming a piece of cake Bartosz Milewski D Programming Language.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Software Transactional Objects Guy Eddon Maurice Herlihy TRAMP 2007.
DATA STRUCTURES USING C++ Chapter 5
Concurrency Control III. General Overview Relational model - SQL Formal & commercial query languages Functional Dependencies Normalization Physical Design.
Concurrency unlocked transactional memory for composable concurrency Tim Harris Maurice Herlihy Simon Marlow Simon Peyton Jones.
Optimistic Methods for Concurrency Control By : H.T. Kung & John T. Robinson Presenters: Munawer Saeed.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Transactional Locking Nir Shavit Tel Aviv University (Joint work with Dave Dice and Ori Shalev)
COS 461 Fall 1997 Transaction Processing u normal systems lose their state when they crash u many applications need better behavior u today’s topic: how.
PARALLEL PROGRAMMING with TRANSACTIONAL MEMORY Pratibha Kona.
Parallelism and Concurrency Koen Lindström Claessen Chalmers University Gothenburg, Sweden Ulf Norell.
1 Lecture 21: Transactional Memory Topics: consistency model recap, introduction to transactional memory.
1 Lecture 7: Transactional Memory Intro Topics: introduction to transactional memory, “lazy” implementation.
EPFL - March 7th, 2008 Interfacing Software Transactional Memory Simplicity vs. Flexibility Vincent Gramoli.
1 Lecture 23: Transactional Memory Topics: consistency model recap, introduction to transactional memory.
Transaction Management and Concurrency Control
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Transaction Management
CS510 Concurrent Systems Class 13 Software Transactional Memory Should Not be Obstruction-Free.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
1 Lecture 10: TM Implementations Topics: wrap-up of eager implementation (LogTM), scalable lazy implementation.
Data Structures Using C++ 2E
INTRODUCTION TO TRANSACTION PROCESSING CHAPTER 21 (6/E) CHAPTER 17 (5/E)
CS510 Concurrent Systems Introduction to Concurrency.
Software Transactional Memory for Dynamic-Sized Data Structures Maurice Herlihy, Victor Luchangco, Mark Moir, William Scherer Presented by: Gokul Soundararajan.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
Sutirtha Sanyal (Barcelona Supercomputing Center, Barcelona) Accelerating Hardware Transactional Memory (HTM) with Dynamic Filtering of Privatized Data.
Concurrency Server accesses data on behalf of client – series of operations is a transaction – transactions are atomic Several clients may invoke transactions.
Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Non-Blocking Concurrent Data Objects With Abstract Concurrency By Jack Pribble Based on, “A Methodology for Implementing Highly Concurrent Data Objects,”
Software Transactional Memory Should Not Be Obstruction-Free Robert Ennals Presented by Abdulai Sei.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions.
A Methodology for Implementing Highly Concurrent Data Objects by Maurice Herlihy Slides by Vincent Rayappa.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
MULTIVIE W Slide 1 (of 21) Software Transactional Memory Should Not Be Obstruction Free Paper: Robert Ennals Presenter: Emerson Murphy-Hill.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Optimistic Design CDP 1. Guarded Methods Do something based on the fact that one or more objects have particular states Make a set of purchases assuming.
Read-Log-Update A Lightweight Synchronization Mechanism for Concurrent Programming Alexander Matveev (MIT) Nir Shavit (MIT and TAU) Pascal Felber (UNINE)
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
R 1 Transactional abstractions: beyond atomic update Tim Harris r.
pThread synchronization
Novel Paradigms of Parallel Programming Prof. Smruti R. Sarangi IIT Delhi.
Parallelism and Concurrency Koen Lindström Claessen Chalmers University Gothenburg, Sweden Patrik Jansson.
Memory Management.
Parallelism and Concurrency
Transaction Management
Background on the need for Synchronization
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Ch 21: Transaction Processing
Lecture 6: Transactions
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Doubly Linked List Implementation
Lecture 22: Consistency Models, TM
Software Transactional Memory Should Not be Obstruction-Free
Multicore programming
Multicore programming
CSE 153 Design of Operating Systems Winter 19
Data Structures & Algorithms
Doubly Linked List Implementation
Concurrency control (OCC and MVCC)
SPL – PS3 C++ Classes.
Presentation transcript:

How D can make concurrent programming a piece of cake Bartosz Milewski D Programming Language

Multi-Core is here to stay Programmers must use concurrency (Dead-) Lock Oriented Programming is BAD New paradigm badly needed

void Toggle () { if (x == 0) x = 1; else x = 0; } Fetch value of x Store it in a temporary Compare with zero Based on the result, write new value What if, in the meanwhile, the original x was modified by another thread? Write is based on incorrect assumption!

Theorem: – Hypothesis: x == 0 (read x, compare to 0) – Conclusion: x = 1 (write 1 into x) Problem: Hypothesis invalidated before conclusion reached Must re-check the hypothesis before writing!

Delay checking: Log read values for later Must re-check before writing: Log the “intent to write” (speculative writes) for later execution Verify hypothesis : Are read values unchanged? Reach conclusion: Execute writes from log to memory

Concurrency issues postponed to the commit phase (read-check and write) Commit uses generic code, which can be optimized and tested once for all User code simple, less error-prone—code as if there were a single global lock Increased concurrency—executes as if every word were separately locked No deadlocks!

Start a transaction: create log Speculative execution—reads and writes logged Commit phase (atomic) – Read-check – Write to memory If failure, restart transaction

Combining atomic operations using locks—almost impossible! Atomic (transacted) withdrawal atomic { acc.Withdraw (sum); } Atomic deposit—similar Atomic transfer atomic { accOne.Withdraw (sum); accTwo.Deposit (sum); }

Example: Producer/Consumer atomic { Item * item = pcQueue.Get (); } Item * Get () atomic // PCQueue method { if (_queue.Count () == 0) retry; else return _queue.pop_front (); }

Restart transaction without destroying the log Make the read-log globally available Block until any of the logged read locations changes Every commit checks the read-sets of blocked transactions and unblocks the ones that overlap with its write-set

Consumer doesn’t have to specify what it’s waiting for Producer doesn’t have to signal anybody Composability: Wait for two items atomic { item1 = pcQueue.Get (); item2 = pcQueue.Get (); }

Transactable (atomic) objects – Visible as opaque handles – Can be opened only inside transaction – Open (for read) returns a const pointer to the actual object – Open for write clones the object and returns pointer to the clone atomic struct Foo { int x; } atomic Foo f (new Foo); // an opaque handle atomic { // start transaction Foo * foo = f.open_write (); ++foo.x; }

Deep copy of the object Embedded atomic handles are copied but not the objects they refer to Transactable data structures build from small atomic objects (tree from nodes) Value-semantic objects (e.g. structs) cloned by copy construction

Struct or class marked as “atomic”—all methods (except constructor) “atomic” Open and open_write can be called only inside a transaction—i.e. from inside: – Atomic block – Atomic function/method Atomic function/method may only be called from inside a transaction

struct Slist // not atomic { this () { // Insert sentinels SNode * last = new SNode (Infin, null); _head = new SNode (MinusInfin, last); } // atomic methods const (SNode) * Head () const atomic { retrun _head.open (); } void Insert (int i) atomic; void Remove (int i) atomic; private: atomic SNode _head; // atomic handle }

struct SNode atomic { public: this (int i, const (SNode) * next) { _val = i; _next = next; } // atomic methods (by default) int Value () const { return _val; } const (SNode) * Next () const { return _next.open (); } Snode * SetNext (const (SNode) * next) { SNode * self = this.open_write () self._next = next; return self; } private: int_val; atomic Snode_next; }

atomic { myList.Insert (x); } // transactioned void Insert (int i) atomic { const (SNode) * prev = Head (); // sentinel const (SNode) * cur = prev.Next (); while (cur._val < i) { prev = cur; cur = prev.Next (); } assert (cur != 0); // at worst high sentinel SNode * newNode = new SNode (i, cur); (void) prev.SetNext (newNode); }

Versioning and Locking – Global Version Clock (always even) – Version numbers always increase – (Hidden) version/lock word per atomic object (lock is the lowest bit) Consistent Snapshot maintenance – Version checks when opening an object – Read-check during commit

Transaction starts by reading Version Clock into the transaction’s read-version variable Open object – Check the object lock (bit). If taken, abort – Check object version number. If it’s greater than read-version abort

Every open is recorded in read-log – Pointer to original object (from which its version lock can be retrieved) Every open_write is recorded in read-log and write_log – Pointer to original object – Pointer to clone Okay to call open_write after open (read)

Lock all objects recorded in the write-log – Bounded spinlock on each version lock Increment global Version Clock—store as transaction’s write-version Sequence Point (if transaction commits, that’s when it “happened”) Read-check – Re-check object version numbers against read-version

For each location in the write-log – Swap the clone in place of original – Stamp it with write-version – Unlock

We have C implementation (Brad Roberts’ port of GPL’d Dice, Shalev, and Shalit) Write D implementation Modify type system

Dave Dice, Ori Shalev, and Nir Shavit. Transactional Locking II Transactional Locking II Tim Harris, Simon Marlow, Simon Peyton Jones, and Maurice Herlihy. Composable Memory Transactions. ACM Conference on Principles and Practice of Parallel Programming 2005 (PPoPP'05) Composable Memory Transactions