Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)

61

description

An overview of the Task Parallel Library and its use in the Pithos for Windows client, including a recipe of Parallel Revani

Transcript of Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)

Page 1: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)
Page 2: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

Parallel and Asynchronous Programming

Or how we buitl a Dropbox clone without a PhD in Astrophysics

Panagiotis KanavosDotNetZone [email protected]

Page 3: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Processors are getting smaller

• Networks are getting worse

• Operating Systems demand it

• Only a subset of the code can run in parallel

Why

Page 4: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Once, a single-thread process could use 100% of the CPU

• 16% ΜΑΧ ona Quad core LAPTOP with HyperThreading

• 8% ΜΑΧ on an 8 core server

Processors are getting smaller

Page 5: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Hand-coded threads and synchronization

• BackgroundWorker Heavy, cumbersome, single threaded, inadequate progress

reporting

• EAP: From event to event Complicated, loss of continuity

• APM: BeginXXX/EndXXX Cumbersome, imagine socket programming with Begin/End!

or rather ...

What we used to have

Page 7: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Collisions Reduced throughput Deadlocks

• Solution: Limit the number of threads ThreadPools Extreme: Stackless Python Copy data instead of shared access Extreme: Immutable programming

The problem with threads

Page 8: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• How can I speed-up my algorithm?

• Which parts can run in parallel?

• How can I partition my data?

Why should I care about threads?

Page 9: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

Example

Revani

Page 10: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Beat the yolks with 2/3 of sugar until fluffy• Beat the whites with 1/3 of sugar to stiff meringue• and add half the mixture to the yolk mixture.• Mix semolina with flour and ground coconut , • add rest of meringue and mix• Mix and pour in cake pan• Bake in pre-heated oven at 170οC for 20-25 mins. • Allow to cool • Prepare syrup, boil water, sugar, lemon for 3 mins.• Pour warm syrup over revani• Sprinkle with ground coconut.

Synchronous Revani

Page 11: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

Parallel Revani

• Beat yolks • Beat Whites

• Add half mixture• Mix semolina• Add rest of meringue• Mix• Pour in cake pan

• Pour syrup• Sprinkle

• Bake • Prepare syrup

Page 12: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Support for multiple concurrency scenarios

• Overall improvements in threading

• Highly Concurrent collections

What we have now

Page 13: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

Scenaria

• Faster processing of large data• Number crunching

• Execute long operations

• Serve high volume of requests• Social Sites, Web sites, Billing, Log aggregators

• Tasks with frequent blocking• REST clients, IT management apps

Page 14: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Data Parallelism

• Task Parallelism

• Asynchronous programming

• Agents/Actors

• Dataflows

Scenario Classification

Page 15: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Partition the data

• Implement the algorithm in a function

• TPL creates the necessary tasks

• The tasks are assigned to threads

• I DON’T’T have to define the number of Tasks/Threads!

Data Parallelism – Recipe

Page 16: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Parallel.For / Parallel.ForEach

• PLINQ

• Partitioners

Data Parallelism - Tools

Page 17: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Parallel execution of lambdas

• Blocking calls!

• We specify Cancellation Token Maximum number of Threads Task Scheduler

Parallel class Methods

Page 18: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• LINQ Queries

• Potentially multiple threads

• Parallel operators

• Unordered results

• Beware of racesList<int> list = new List<int>();var q = src.AsParallel() .Select(x => { list.Add(x); return x; }) .Where(x => true) .Take(100);

PLINQ

Page 19: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Doesn’t use SSE instructions

• Doesn’t use the GPU

• Isn’t using the CPU at 100%

What it can’t do

Page 20: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)
Page 21: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Data Parallelism

• Task Parallelism

• Asynchronous programming

• Agents/Actors

• Dataflows

Scenaria

Page 22: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Break the problem into steps

• Convert each step to a function

• Combine steps with Continuations

• TPL assigns tasks to threads as needed

• I DON’T have to define number of Tasks/Threads!

• Cancellation of the entire task chain

Task Parellelism – Recipe

Page 23: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Tasks wherever code blocks

• Cancellation

• Lazy Initialization

• Progress Reporting

• Synchronization Contexts

The Improvements

Page 24: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Problem: How do you cancel multiple tasks without leaving trash behind?

• Solution: Everyone monitors a CancellationToken TPL cancels subsequent Tasks or Parallel operations Created by a CancellationTokenSource Can execute code when Cancel is called

Cancellation

Page 25: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Problem: How do you update the UI from inside a task?

• Solution: Using an IProgress<T> object Out-of-the-Box Progress<T> updates the current Synch Context Any type can be a message Replace with our own implementation

Progress Reporting

Page 26: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Calculate a value only when needed

• Lazy<T>(Func<T> …)

• Synchronous or Asynchronous calculation Lazy.Value Lazy.GetValueAsync<T>()

Lazy Initialization

Page 27: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Since .NET 2.0!

• Hides Winforms, WPF, ASP.NET SynchronizationContext.Post/Send instead of Dispatcher.Invoke

etc Synchronous and Asynchronous version

• Automatically created by the environment

SynchronizationContext.Current

• Can create our own E.g. For a Command Line aplication

Synchronization Context

Page 28: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Data Parallelism

• Task Parallelism

• Asynchronous programming

• Agents/Actors

• Dataflows

Scenaria

Page 29: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Support at the language leve

• Debugging support

• Exception Handling

• After await return to original “thread” Beware of servers and libraries

• Dos NOT always execute asynchronously Only when a task is encountered or the thread yields Task.Yield

Async/Await

Page 30: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

private static async Task<T> Retry<T>(Func<T> func, int retryCount) { while (true) { try { var result = await Task.Run(func); return result; } catch { If (retryCount == 0) throw; retryCount--; } } }

Asynchronous Retry

Page 31: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)
Page 32: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Highly concurrent• Thread-safe• Not only for TPL/PLINQ• Producer/Consumer scenaria

More Goodies - Collections

Page 33: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• ConcurrentQueue

• ConcurrentStack

• ConcurrentDictionary

Concurrent Collections - 2

Page 34: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Duplicates allowed

• List per Thread

• Reduced collisions for each tread’s Add/Take

• BAD for Producer/Consumer

The Odd one - ConcurrentBag

Page 35: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• NOT faster than plain collections in low concurrency scenarios

• DO NOT consume less memory

• DO NOT provide thread safe enumeration

• DO NOT ensure atomic operations on content

• DO NOT fix unsafe code

Concurrent Collections - Gotchas

Page 36: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Visual Studio 2012

• Async Targeting package

• System.Net.HttpClient package

Also in .NET 4

Page 37: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• F# async

• C++ Parallel Patterns Library

• C++ Concurrency Runtime

• C++ Agents

• C++ AMP

Other Technologies

Page 38: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Object storage similar to Amazon S3/Azure Blob storage

• A Service of Synnefo – IaaS by GRNet

• Written in Python

• Clients for Web, Windows, iOS, Android, Linux

• Versioning, Permissions, Sharing

Page 39: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

Synnefo

Page 40: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• REST API base on CloudFiles by Rackspace Compatible with CyberDuck etc

• Block storage

• Uploads only using blocks

• Uses Merkle Hashing

Pithos API

Page 41: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Multiple accounts per machine

• Synchronize local folder to a Pithos account

• Detect local changes and upload

• Detect server changes and download

• Calculate Merkle Hash for each file

Pithos Client for Windows

Page 42: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

The Architecture

UI

WPF

MVVM

Caliburn Micro

Core

File Agent

Poll Agent

Network Agent

Status Agent

Networking

CloudFiles

HttpClient

Storage

SQLite

SQL Server Compact

Page 43: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• .ΝΕΤ 4, due to Windows XP compatibility

• Visual Studio 2012 + Async Targeting Pack

• UI - Caliburn.Micro

• Concurrency - TPL, Parallel, Dataflow

• Network – HttpClient

• Hashing - OpenSSL – Faster than native provider for hashing

• Storage - NHibernate, SQLite/SQL Server Compact

• Logging - log4net

Technologies

Page 44: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Handle potentially hundrends of file events

• Hashing of many/large files

• Multiple slow calls to the server

• Unreliable network

• And yet it shouldn’t hang

• Update the UI with enough information

The challenges

Page 45: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Use producer/consumer pattern

• Store events in ConcurrentQueue

• Process ONLY after idle timeout

Events Handling

Page 46: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Why I hate Game of Thrones

• Asynchronous reading of blocks

• Parallel Hashing of each block

• Use of OpenSSL for its SSE support

• Concurrency Throttling

• Beware of memory consumption!

Merkle Hashing

Page 47: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Each call a task

• Concurrent REST calls per account and share

• Task.WhenAll to process results

Multiple slow calls

Page 48: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Use System.Net.Http.HttpClient

• Store blocks in a cache folder

• Check and reuse orphans

• Asynchronous Retry of calls

Unreliable network

Page 49: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Use Transactional NTFS if available Thanks MS for killing it!

• Update a copy and File.Replace otherwise

Resilience to crashes

Page 50: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Use of independent agents

• Asynchronous operations wherever possible

Should not hang

Page 51: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Use WPF, MVVM

• Use Progress to update the UI

Provide Sufficient user feedback

Page 52: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Create Windows 8 Dekstop and WinRT client

• Use Reactive Framework

Next Steps

ΖΗΤΟΥΝΤΑΙ ΕΘΕΛΟΝΤΕΣ

Page 53: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)
Page 54: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Avoid Side Effects

• Use Functional Style

• Clean Coding

• THE BIG SECRET: Use existing, tested algorithms

• IEEE, ACM Journals and libraries

Clever Tricks

Page 55: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Simplify asynchronous or parallel code

• Use out-of-the-box libraries

• Scenarios that SUIT Task or Data Parallelism

YES TPL

Page 56: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• To accelerate “bad” algorithms

• To “accelerate” database access Use proper SQL and Indexes! Avoid Cursors

• Reporting DBs, Data Warehouse, OLAP Cubes

NO TPL

Page 57: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Functional languages like F#, Scala

• Distributed Frameworks like Hadoop, {m}brace

When TPL is not enough

Page 58: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• C# 5 in a Nutshell, O’Riley

• Parallel Programming with .NET, Microsoft

• Pro Parallel Programming with C#, Wiley

• Concurrent Programming on Windows, Pearson

• The Art of Concurrency, O’Reilly

Books

Page 59: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)

• Parallel FX Team: http://blogs.msdn.com/b/pfxteam/

• ΙΕΕΕ Computer Society http://www.computer.org

• ACM http://www.acm.org

Useful Links

Page 60: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)
Page 61: Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)