C++ await Deon Brewis std::future additions:

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Logic Thread Queue DB Thread Queue ASync …n Sync.
Karel J Robot Chapter 6.
13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
DEV324 C# VB 7.0 Managed Code C# VB 8.0 Generics C# VB 9.0 Language Integrated Query C# VB 10.0 Dynamic + Language Parity.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Potential Languages of the Future Chapel,
Automated Software Verification with a Permission-Based Logic 20 th June 2014, Zürich Malte Schwerhoff, ETH Zürich.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
NewsFlash!! Earth Simulator no longer #1. In slightly less earthshaking news… Homework #1 due date postponed to 10/11.
Stack buffer overflow.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
PARALLEL PROGRAMMING ABSTRACTIONS 6/16/2010 Parallel Programming Abstractions 1.
Managed Code Generics Language Integrated Query Dynamic + Language Parity C# VB 11.0 Windows Runtime + Asynchrony C# VB 7.0 C# VB.
3 string read_string_from_file(string file); string s = read_string_from_file("myfile.txt"); cout
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Consuming REST Services from C# SoftUni Team Technical Trainers Software University
1162 JDK 5.0 Features Christian Kemper Principal Architect Borland.
Lecture 2 Foundations and Definitions Processes/Threads.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Implementation of the Hangman Game in C++
WHO WILL BENEFIT FROM THIS TALK TOPICS WHAT YOU’LL LEAVE WITH ASP.NET developers, including Web Forms & MVC History of async programming in.NET How async.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Synonyms: A _______ is a unit of software that is capable of executing concurrently with other similar units. _______ -- user-defined process in Ada _________.
PLT Final Project---COLOGO Lixing Dong, Zhou Ma, Chao Song, Siyuan Lu, Dongyang Jiang.
Java Thread and Memory Model
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
Programming Languages and Paradigms Activation Records in Java.
1 OS Review Processes and Threads Chi Zhang
Approval Detailed Specification Design Investigation.
Async Made Simple with C++ PPL
Synchronization These notes introduce:
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Assembly Language Co-Routines
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
CS 1428 Final Exam Review. Exam Format 200 Total Points – 60 Points Writing Programs – 45 Points Tracing Algorithms and determining results – 20 Points.
1 Concurrency Abstractions in C#. Concurrency in C# CS 5204 – Fall, Motivation Concurrency critical factor in behavior/performance affects semantics.
Practical Session 9 Computer Architecture and Assembly Language.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Asynchronous Programming with C# v.Next
The Future of C# and Visual Basic
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Advanced C++ Programming
Chapter 5: Loops and Files.
CS 1428 Exam II Review.
CS 1428 Exam II Review.
While Loop Design ENGI 1020 Fall 2018.
Stack buffer overflow.
Unit 3 - The while Loop - Extending the Vic class - Examples
CS 1428 Final Exam Review.
CSE 451 Autumn 2003 Section 3 October 16.
Dynamic Memory A whole heap of fun….
Computer Science Core Concepts
Computer Architecture and Assembly Language
Chapter 17 Recursion.
Repetition Statements (Loops) - 2
Introduction to Computer Science
Representation and Management of Data on the Internet
Some Assembly (Part 2) set.html.
Foundations and Definitions
Computer Architecture and Assembly Language
Loops CGS3416 Spring 2019 Lecture 7.
How Memory Leaks Work with Memory Diagram
Threads and concurrency / Safety
Presentation transcript:

C++ await Deon Brewis std::future additions: isocpp.org/files/papers/N3721.pdf resumable functions: isocpp.org/files/papers/N3722.pdf

PPL vs. std::future PPL std::future Requires future.then – i.e. N3721 concurrency::task<int> Calc( int x, int y ) resumable { int i = await concurrency::create_task( [=] { return x + y; } ); return i + 42; } std::future<int> Calc( int x, int y ) resumable { int i = await std::async( [=] { return x + y; } ); return i + 42; } Requires future.then – i.e. N3721

Motivation .get(), .then() and await comparison

Simple case .get size_t file_sizes( string file1, string file2 ) { task<file> f1 = open(file1); task<file> f2 = open(file2); return f1.get().size() + f2.get().size(); }

Simple case .get .then size_t file_sizes( string file1, string file2 ) { task<file> f1 = open(file1); task<file> f2 = open(file2); return f1.get().size() + f2.get().size(); } task<size_t> file_sizes( string file1, string file2 ) { task<file> f1 = open(file1); task<file> f2 = open(file2); return f1.then( [f2] (task<file> tr1 ) { f2.then( [tr1] (task<file> tr2) return tr1.get().size() + tr2.get().size(); }) }); }

Simple case .get .then .then + await size_t file_sizes( string file1, string file2 ) { task<file> f1 = open(file1); task<file> f2 = open(file2); return f1.get().size() + f2.get().size(); } task<size_t> file_sizes( string file1, string file2 ) { task<file> f1 = open(file1); task<file> f2 = open(file2); return f1.then( [f2] (task<file> tr1 ) { f2.then( [tr1] (task<file> tr2) return tr1.get().size() + tr2.get().size(); }) }); } .then + await task<size_t> file_sizes( string file1, string file2 ) resumable { task<file> f1 = open(file1); task<file> f2 = open(file2); return (await f1).size() + (await f2).size(); }

Branches and loops ? .get .then string read( string file, string suffix ) { istream fi = open(file).get(); string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; } task<string> read( string file, string suffix ) { return open(file) .then([=](istream fi) { string ret, chunk; while( ?

Branches and loops .get .then string read( string file, string suffix ) { istream fi = open(file).get(); string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; } task<string> read( string file, string suffix ) { return open(file) .then([=](istream fi) { auto ret = make_shared<string>(); auto next = make_shared<std::function<task<string>()>>( [=] { fi.read() .then([=](string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return next(); } return task_from_result(*ret); }); return (*next)();

Branches and loops .get .then .then + await string read( string file, string suffix ) { istream fi = open(file).get(); string ret, chunk; while( (chunk = fi.read().get()).size() ) ret += chunk + suffix; return ret; } task<string> read( string file, string suffix ) { return open(file) .then([=](istream fi) { auto ret = make_shared<string>(); auto next = make_shared<std::function<task<string>()>>( [=] { fi.read() .then([=](string chunk) { if( chunk.size() ) { *ret += chunk + suffix; return next(); } return task_from_result(*ret); }); return (*next)(); .then + await task<string> read( string file, string suffix ) resumable { istream fi = await open(file); string ret, chunk; while( (chunk = await fi.read()).size() ) ret += chunk + suffix; return ret; }

Technical drill-down

Await implementations Iterator-Based Co-routines Rewrite resumable function into a resumable state machine Requires all locals to be hoisted to the heap Resumable Side Stacks Do not rewrite function logic Each resumable function execution has its own “side” stack Side stack lives beyond suspension/resuming until logical completion C# C++ IBC: harder to implement in our current compiler (temp lifetime); also slower for C++ (ctor and dtor calls need to be injected) RSS: A sidestack requires 8k commit (4k + guard page), it reserves 1MB ASLA: for scalability reasons (supporting >1million concurrent resumable functions)

Resumable Side Stack simulation void foo() { task<bool> t = async_bar(); somefunc(); bool b = t.get(); } task<bool> async_bar() resumable do_work(); ... // sync calls await async_work(); // More sync calls return true; task<void> async_work() resumable await create_task( [] { longrunning (); }); return; Thread #1 Thread #2 Main Stack Side Stack Side Stack <blocked> <completed> done! somefunc(); bool b = t.get() t = async_bar() <suspended> foo(); async_work is done! Longrunning is done! <completed> … return task<bool> … <suspended> … … … … … return task<void> … do_work(); More sync calls await async_work(); do_work(); await create_task(); … return true; async_bar(); async_work(); return;

Demo