SlideShare a Scribd company logo
Parallel and Asynchronous
Programming
        Or how we built a Dropbox clone without a
        PhD in Astrophysics



             Panagiotis Kanavos
             DotNetZone Moderator
             pkanavos@gmail.com
Why
• Processors are getting smaller
• Networks are getting worse
• Operating Systems demand it
• Only a subset of the code can run in parallel
Processors are getting smaller
• Once, a single-thread process could use 100%
  of the CPU
• 16% ΜΑΧ ona Quad core LAPTOP with
  HyperThreading
• 8% ΜΑΧ on an 8 core server
What we used to have
• 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 ...
Why I stopped blogging
• Asynchronous Pipes with APM
The problem with threads
• Collisions
     Reduced throughput
     Deadlocks

• Solution: Limit the number of threads
     ThreadPools
     Extreme: Stackless Python
     Copy data instead of shared access
     Extreme: Immutable programming
Why should I care about
threads?
• How can I speed-up my algorithm?
• Which parts can run in parallel?
• How can I partition my data?
Example




          Revani
Synchronous Revani
•   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.
Parallel Revani
• Beat yolks                   • Beat Whites
           •   Add half mixture
           •   Mix semolina
           •   Add rest of meringue
           •   Mix
           •   Pour in cake pan

• Bake                          • Prepare syrup
               • Pour syrup
               • Sprinkle
What we have now
• Support for multiple concurrency scenarios
• Overall improvements in threading
• Highly Concurrent collections
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
Scenario Classification
• Data Parallelism
• Task Parallelism
• Asynchronous programming
• Agents/Actors
• Dataflows
Data Parallelism – Recipe
• 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 - Tools
• Parallel.For / Parallel.ForEach
• PLINQ
• Partitioners
Parallel class Methods
• Parallel execution of lambdas
• Blocking calls!
• We specify
     Cancellation Token
     Maximum number of Threads
     Task Scheduler
PLINQ
• LINQ Queries
• Potentially multiple threads
• Parallel operators
• Unordered results
• Beware of races
  List<int> list = new List<int>();
  var q = src.AsParallel()
    .Select(x => { list.Add(x); return x; })
    .Where(x => true) .Take(100);
What it can’t do
• Doesn’t use SSE instructions
• Doesn’t use the GPU
• Isn’t using the CPU at 100%
Scenaria
• Data Parallelism
• Task Parallelism
• Asynchronous programming
• Agents/Actors
• Dataflows
Task Parellelism – Recipe
• 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
The Improvements
• Tasks wherever code blocks
• Cancellation
• Lazy Initialization
• Progress Reporting
• Synchronization Contexts
Cancellation
• 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
Progress Reporting
• 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
Lazy Initialization
• Calculate a value only when needed
• Lazy<T>(Func<T> …)
• Synchronous or Asynchronous calculation
     Lazy.Value
     Lazy.GetValueAsync<T>()
Synchronization Context
• 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
Scenaria
• Data Parallelism
• Task Parallelism
• Asynchronous programming
• Agents/Actors
• Dataflows
Async/Await
• 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
Asynchronous Retry
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--;
} } }
More Goodies - Collections
•   Highly concurrent
•   Thread-safe
•   Not only for TPL/PLINQ
•   Producer/Consumer scenaria
Concurrent Collections - 2
• ConcurrentQueue
• ConcurrentStack
• ConcurrentDictionary
The Odd one - ConcurrentBag
• Duplicates allowed
• List per Thread
• Reduced collisions for each tread’s Add/Take
• BAD for Producer/Consumer
Concurrent Collections -
Gotchas
• 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
Also in .NET 4
• Visual Studio 2012
• Async Targeting package
• System.Net.HttpClient package
Other Technologies
• F# async
• C++ Parallel Patterns Library
• C++ Concurrency Runtime
• C++ Agents
• C++ AMP
• 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
Synnefo
Pithos API
• REST API base on CloudFiles by Rackspace
     Compatible with CyberDuck etc

• Block storage
• Uploads only using blocks
• Uses Merkle Hashing
Pithos Client for Windows
• 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
The Architecture

   UI         Core         Networking    Storage

             File Agent
  WPF
                            CloudFiles     SQLite
             Poll Agent
  MVVM
             Network
              Agent
                                         SQL Server
 Caliburn                   HttpClient
                                          Compact
  Micro     Status Agent
Technologies
• .ΝΕΤ 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
The challenges
• 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
Events Handling
• Use producer/consumer pattern
• Store events in ConcurrentQueue
• Process ONLY after idle timeout
Merkle Hashing
• 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!
Multiple slow calls

• Each call a task
• Concurrent REST calls per account and share
• Task.WhenAll to process results
Unreliable network

• Use System.Net.Http.HttpClient
• Store blocks in a cache folder
• Check and reuse orphans
• Asynchronous Retry of calls
Resilience to crashes
• Use Transactional NTFS if available
     Thanks MS for killing it!

• Update a copy and File.Replace otherwise
Should not hang
• Use of independent agents
• Asynchronous operations wherever possible
Provide Sufficient user feedback
• Use WPF, MVVM
• Use Progress to update the UI
Next Steps
• Create Windows 8 Dekstop and WinRT client
• Use Reactive Framework


    ΖΗΤΟΥΝΤΑΘ ΕΘΕΛΟΝΤΕΣ
Clever Tricks
• Avoid Side Effects
• Use Functional Style
• Clean Coding
• THE BIG SECRET:
     Use existing, tested algorithms

• IEEE, ACM Journals and libraries
YES TPL
• Simplify asynchronous or parallel code
• Use out-of-the-box libraries
• Scenarios that SUIT Task or Data Parallelism
NO TPL
• To accelerate “bad” algorithms
• To “accelerate” database access
     Use proper SQL and Indexes!
     Avoid Cursors

• Reporting DBs, Data Warehouse, OLAP Cubes
When TPL is not enough
• Functional languages like F#, Scala
• Distributed Frameworks like Hadoop, {m}brace
Books
• 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
Useful Links
• Parallel FX Team:
  http://blogs.msdn.com/b/pfxteam/
• ΙΕΕΕ Computer Society
  http://www.computer.org
• ACM http://www.acm.org
Parallel and Asynchronous Programming -  ITProDevConnections 2012 (Greek)
Parallel and Asynchronous Programming -  ITProDevConnections 2012 (Greek)

More Related Content

What's hot

JS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScript
JS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScriptJS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScript
JS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScript
JSFestUA
 
Cooking a rabbit pie
Cooking a rabbit pieCooking a rabbit pie
Cooking a rabbit pie
Tomas Doran
 
Combining the strength of erlang and Ruby
Combining the strength of erlang and RubyCombining the strength of erlang and Ruby
Combining the strength of erlang and Ruby
Martin Rehfeld
 
Scaling with Symfony - PHP UK
Scaling with Symfony - PHP UKScaling with Symfony - PHP UK
Scaling with Symfony - PHP UK
Ricard Clau
 
Erlang factory 2011 london
Erlang factory 2011 londonErlang factory 2011 london
Erlang factory 2011 london
Paolo Negri
 
Webinar: Queues with RabbitMQ - Lorna Mitchell
Webinar: Queues with RabbitMQ - Lorna MitchellWebinar: Queues with RabbitMQ - Lorna Mitchell
Webinar: Queues with RabbitMQ - Lorna Mitchell
Codemotion
 
Flink Forward SF 2017: Tzu-Li (Gordon) Tai - Joining the Scurry of Squirrels...
Flink Forward SF 2017: Tzu-Li (Gordon) Tai -  Joining the Scurry of Squirrels...Flink Forward SF 2017: Tzu-Li (Gordon) Tai -  Joining the Scurry of Squirrels...
Flink Forward SF 2017: Tzu-Li (Gordon) Tai - Joining the Scurry of Squirrels...
Flink Forward
 
Modern software architectures - PHP UK Conference 2015
Modern software architectures - PHP UK Conference 2015Modern software architectures - PHP UK Conference 2015
Modern software architectures - PHP UK Conference 2015
Ricard Clau
 
Using Apache Camel as AKKA
Using Apache Camel as AKKAUsing Apache Camel as AKKA
Using Apache Camel as AKKA
Johan Edstrom
 
Innovating faster with SBT, Continuous Delivery, and LXC
Innovating faster with SBT, Continuous Delivery, and LXCInnovating faster with SBT, Continuous Delivery, and LXC
Innovating faster with SBT, Continuous Delivery, and LXC
kscaldef
 
AppScale Talk at SBonRails
AppScale Talk at SBonRailsAppScale Talk at SBonRails
AppScale Talk at SBonRails
Chris Bunch
 
Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"
Paolo Negri
 
Building Asynchronous Applications
Building Asynchronous ApplicationsBuilding Asynchronous Applications
Building Asynchronous Applications
Johan Edstrom
 
Scaling High Traffic Web Applications
Scaling High Traffic Web ApplicationsScaling High Traffic Web Applications
Scaling High Traffic Web Applications
Achievers Tech
 
webservers
webserverswebservers
webservers
Ewere Diagboya
 
Profiling and Tuning a Web Application - The Dirty Details
Profiling and Tuning a Web Application - The Dirty DetailsProfiling and Tuning a Web Application - The Dirty Details
Profiling and Tuning a Web Application - The Dirty Details
Achievers Tech
 
Big Data! Great! Now What? #SymfonyCon 2014
Big Data! Great! Now What? #SymfonyCon 2014Big Data! Great! Now What? #SymfonyCon 2014
Big Data! Great! Now What? #SymfonyCon 2014
Ricard Clau
 

What's hot (17)

JS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScript
JS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScriptJS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScript
JS Fest 2019. Александр Хотемский. Способы распараллеливания тестов в JavaScript
 
Cooking a rabbit pie
Cooking a rabbit pieCooking a rabbit pie
Cooking a rabbit pie
 
Combining the strength of erlang and Ruby
Combining the strength of erlang and RubyCombining the strength of erlang and Ruby
Combining the strength of erlang and Ruby
 
Scaling with Symfony - PHP UK
Scaling with Symfony - PHP UKScaling with Symfony - PHP UK
Scaling with Symfony - PHP UK
 
Erlang factory 2011 london
Erlang factory 2011 londonErlang factory 2011 london
Erlang factory 2011 london
 
Webinar: Queues with RabbitMQ - Lorna Mitchell
Webinar: Queues with RabbitMQ - Lorna MitchellWebinar: Queues with RabbitMQ - Lorna Mitchell
Webinar: Queues with RabbitMQ - Lorna Mitchell
 
Flink Forward SF 2017: Tzu-Li (Gordon) Tai - Joining the Scurry of Squirrels...
Flink Forward SF 2017: Tzu-Li (Gordon) Tai -  Joining the Scurry of Squirrels...Flink Forward SF 2017: Tzu-Li (Gordon) Tai -  Joining the Scurry of Squirrels...
Flink Forward SF 2017: Tzu-Li (Gordon) Tai - Joining the Scurry of Squirrels...
 
Modern software architectures - PHP UK Conference 2015
Modern software architectures - PHP UK Conference 2015Modern software architectures - PHP UK Conference 2015
Modern software architectures - PHP UK Conference 2015
 
Using Apache Camel as AKKA
Using Apache Camel as AKKAUsing Apache Camel as AKKA
Using Apache Camel as AKKA
 
Innovating faster with SBT, Continuous Delivery, and LXC
Innovating faster with SBT, Continuous Delivery, and LXCInnovating faster with SBT, Continuous Delivery, and LXC
Innovating faster with SBT, Continuous Delivery, and LXC
 
AppScale Talk at SBonRails
AppScale Talk at SBonRailsAppScale Talk at SBonRails
AppScale Talk at SBonRails
 
Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"Erlang factory SF 2011 "Erlang and the big switch in social games"
Erlang factory SF 2011 "Erlang and the big switch in social games"
 
Building Asynchronous Applications
Building Asynchronous ApplicationsBuilding Asynchronous Applications
Building Asynchronous Applications
 
Scaling High Traffic Web Applications
Scaling High Traffic Web ApplicationsScaling High Traffic Web Applications
Scaling High Traffic Web Applications
 
webservers
webserverswebservers
webservers
 
Profiling and Tuning a Web Application - The Dirty Details
Profiling and Tuning a Web Application - The Dirty DetailsProfiling and Tuning a Web Application - The Dirty Details
Profiling and Tuning a Web Application - The Dirty Details
 
Big Data! Great! Now What? #SymfonyCon 2014
Big Data! Great! Now What? #SymfonyCon 2014Big Data! Great! Now What? #SymfonyCon 2014
Big Data! Great! Now What? #SymfonyCon 2014
 

Similar to Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)

Background processing with hangfire
Background processing with hangfireBackground processing with hangfire
Background processing with hangfire
Aleksandar Bozinovski
 
CakePHP 2.0 - PHP Matsuri 2011
CakePHP 2.0 - PHP Matsuri 2011CakePHP 2.0 - PHP Matsuri 2011
CakePHP 2.0 - PHP Matsuri 2011
Graham Weldon
 
Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640
Newlink
 
Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640
Newlink
 
Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640
Newlink
 
Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640
LLC NewLink
 
Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640
Newlink
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tiger
Elizabeth Smith
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tiger
Elizabeth Smith
 
Data Pipelines with Python - NWA TechFest 2017
Data Pipelines with Python - NWA TechFest 2017Data Pipelines with Python - NWA TechFest 2017
Data Pipelines with Python - NWA TechFest 2017
Casey Kinsey
 
SOA with PHP and Symfony
SOA with PHP and SymfonySOA with PHP and Symfony
SOA with PHP and Symfony
MichalSchroeder
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcached
Jurriaan Persyn
 
C# Async/Await Explained
C# Async/Await ExplainedC# Async/Await Explained
C# Async/Await Explained
Jeremy Likness
 
Scalable game-servers-tgc
Scalable game-servers-tgcScalable game-servers-tgc
Scalable game-servers-tgc
Ashkan Saeedi Mazdeh
 
High Performance Drupal
High Performance DrupalHigh Performance Drupal
High Performance Drupal
Chapter Three
 
Scaling tappsi
Scaling tappsiScaling tappsi
Scaling tappsi
Óscar Andrés López
 
Drupal performance
Drupal performanceDrupal performance
Drupal performance
Piyuesh Kumar
 
Realtime traffic analyser
Realtime traffic analyserRealtime traffic analyser
Realtime traffic analyser
Alex Moskvin
 
Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming Architectures
David Martínez Rego
 
Top ten-list
Top ten-listTop ten-list
Top ten-list
Brian DeShong
 

Similar to Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek) (20)

Background processing with hangfire
Background processing with hangfireBackground processing with hangfire
Background processing with hangfire
 
CakePHP 2.0 - PHP Matsuri 2011
CakePHP 2.0 - PHP Matsuri 2011CakePHP 2.0 - PHP Matsuri 2011
CakePHP 2.0 - PHP Matsuri 2011
 
Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640
 
Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640
 
Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640
 
Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640
 
Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640Xen and-the-art-of-rails-deployment2640
Xen and-the-art-of-rails-deployment2640
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tiger
 
Taming the resource tiger
Taming the resource tigerTaming the resource tiger
Taming the resource tiger
 
Data Pipelines with Python - NWA TechFest 2017
Data Pipelines with Python - NWA TechFest 2017Data Pipelines with Python - NWA TechFest 2017
Data Pipelines with Python - NWA TechFest 2017
 
SOA with PHP and Symfony
SOA with PHP and SymfonySOA with PHP and Symfony
SOA with PHP and Symfony
 
Introduction to memcached
Introduction to memcachedIntroduction to memcached
Introduction to memcached
 
C# Async/Await Explained
C# Async/Await ExplainedC# Async/Await Explained
C# Async/Await Explained
 
Scalable game-servers-tgc
Scalable game-servers-tgcScalable game-servers-tgc
Scalable game-servers-tgc
 
High Performance Drupal
High Performance DrupalHigh Performance Drupal
High Performance Drupal
 
Scaling tappsi
Scaling tappsiScaling tappsi
Scaling tappsi
 
Drupal performance
Drupal performanceDrupal performance
Drupal performance
 
Realtime traffic analyser
Realtime traffic analyserRealtime traffic analyser
Realtime traffic analyser
 
Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming Architectures
 
Top ten-list
Top ten-listTop ten-list
Top ten-list
 

More from Panagiotis Kanavos

77o dotNETZone Meetup: Pattern matching expressions. One small step for one l...
77o dotNETZone Meetup: Pattern matching expressions. One small step for one l...77o dotNETZone Meetup: Pattern matching expressions. One small step for one l...
77o dotNETZone Meetup: Pattern matching expressions. One small step for one l...
Panagiotis Kanavos
 
65ο DotNetZone event: Tpl data flow και railway oriented programming
65ο DotNetZone event: Tpl data flow και railway oriented programming65ο DotNetZone event: Tpl data flow και railway oriented programming
65ο DotNetZone event: Tpl data flow και railway oriented programming
Panagiotis Kanavos
 
Περατζάδα στο Azure Event Hub
Περατζάδα στο Azure Event HubΠερατζάδα στο Azure Event Hub
Περατζάδα στο Azure Event Hub
Panagiotis Kanavos
 
Ο βασιλιάς Git!
Ο βασιλιάς Git!Ο βασιλιάς Git!
Ο βασιλιάς Git!
Panagiotis Kanavos
 
Το Azure δεν είναι χορτοφάγο! - 59ο DotNetZone Event
Το Azure δεν είναι χορτοφάγο! - 59ο DotNetZone EventΤο Azure δεν είναι χορτοφάγο! - 59ο DotNetZone Event
Το Azure δεν είναι χορτοφάγο! - 59ο DotNetZone Event
Panagiotis Kanavos
 
The Server-SIde Story: Παράλληλος και ασύγχρονος προγραμματισμός στο .NET - I...
The Server-SIde Story: Παράλληλος και ασύγχρονος προγραμματισμός στο .NET - I...The Server-SIde Story: Παράλληλος και ασύγχρονος προγραμματισμός στο .NET - I...
The Server-SIde Story: Παράλληλος και ασύγχρονος προγραμματισμός στο .NET - I...
Panagiotis Kanavos
 
Pithos - Architecture and .NET Technologies
Pithos - Architecture and .NET TechnologiesPithos - Architecture and .NET Technologies
Pithos - Architecture and .NET Technologies
Panagiotis Kanavos
 
Πίθος - Αρχιτεκτονική και τεχνολογίες .NET
Πίθος - Αρχιτεκτονική και τεχνολογίες .NETΠίθος - Αρχιτεκτονική και τεχνολογίες .NET
Πίθος - Αρχιτεκτονική και τεχνολογίες .NETPanagiotis Kanavos
 
Real Life Task Parallel Library, ITProDevConnections 2011 (Greek)
Real Life Task Parallel Library, ITProDevConnections 2011 (Greek)Real Life Task Parallel Library, ITProDevConnections 2011 (Greek)
Real Life Task Parallel Library, ITProDevConnections 2011 (Greek)Panagiotis Kanavos
 

More from Panagiotis Kanavos (9)

77o dotNETZone Meetup: Pattern matching expressions. One small step for one l...
77o dotNETZone Meetup: Pattern matching expressions. One small step for one l...77o dotNETZone Meetup: Pattern matching expressions. One small step for one l...
77o dotNETZone Meetup: Pattern matching expressions. One small step for one l...
 
65ο DotNetZone event: Tpl data flow και railway oriented programming
65ο DotNetZone event: Tpl data flow και railway oriented programming65ο DotNetZone event: Tpl data flow και railway oriented programming
65ο DotNetZone event: Tpl data flow και railway oriented programming
 
Περατζάδα στο Azure Event Hub
Περατζάδα στο Azure Event HubΠερατζάδα στο Azure Event Hub
Περατζάδα στο Azure Event Hub
 
Ο βασιλιάς Git!
Ο βασιλιάς Git!Ο βασιλιάς Git!
Ο βασιλιάς Git!
 
Το Azure δεν είναι χορτοφάγο! - 59ο DotNetZone Event
Το Azure δεν είναι χορτοφάγο! - 59ο DotNetZone EventΤο Azure δεν είναι χορτοφάγο! - 59ο DotNetZone Event
Το Azure δεν είναι χορτοφάγο! - 59ο DotNetZone Event
 
The Server-SIde Story: Παράλληλος και ασύγχρονος προγραμματισμός στο .NET - I...
The Server-SIde Story: Παράλληλος και ασύγχρονος προγραμματισμός στο .NET - I...The Server-SIde Story: Παράλληλος και ασύγχρονος προγραμματισμός στο .NET - I...
The Server-SIde Story: Παράλληλος και ασύγχρονος προγραμματισμός στο .NET - I...
 
Pithos - Architecture and .NET Technologies
Pithos - Architecture and .NET TechnologiesPithos - Architecture and .NET Technologies
Pithos - Architecture and .NET Technologies
 
Πίθος - Αρχιτεκτονική και τεχνολογίες .NET
Πίθος - Αρχιτεκτονική και τεχνολογίες .NETΠίθος - Αρχιτεκτονική και τεχνολογίες .NET
Πίθος - Αρχιτεκτονική και τεχνολογίες .NET
 
Real Life Task Parallel Library, ITProDevConnections 2011 (Greek)
Real Life Task Parallel Library, ITProDevConnections 2011 (Greek)Real Life Task Parallel Library, ITProDevConnections 2011 (Greek)
Real Life Task Parallel Library, ITProDevConnections 2011 (Greek)
 

Recently uploaded

“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Zilliz
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 

Recently uploaded (20)

“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
Introducing Milvus Lite: Easy-to-Install, Easy-to-Use vector database for you...
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 

Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)

  • 1.
  • 2. Parallel and Asynchronous Programming Or how we built a Dropbox clone without a PhD in Astrophysics Panagiotis Kanavos DotNetZone Moderator pkanavos@gmail.com
  • 3. Why • Processors are getting smaller • Networks are getting worse • Operating Systems demand it • Only a subset of the code can run in parallel
  • 4. Processors are getting smaller • Once, a single-thread process could use 100% of the CPU • 16% ΜΑΧ ona Quad core LAPTOP with HyperThreading • 8% ΜΑΧ on an 8 core server
  • 5. What we used to have • 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 ...
  • 6. Why I stopped blogging • Asynchronous Pipes with APM
  • 7. The problem with threads • Collisions  Reduced throughput  Deadlocks • Solution: Limit the number of threads  ThreadPools  Extreme: Stackless Python  Copy data instead of shared access  Extreme: Immutable programming
  • 8. Why should I care about threads? • How can I speed-up my algorithm? • Which parts can run in parallel? • How can I partition my data?
  • 9. Example Revani
  • 10. Synchronous Revani • 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.
  • 11. Parallel Revani • Beat yolks • Beat Whites • Add half mixture • Mix semolina • Add rest of meringue • Mix • Pour in cake pan • Bake • Prepare syrup • Pour syrup • Sprinkle
  • 12. What we have now • Support for multiple concurrency scenarios • Overall improvements in threading • Highly Concurrent collections
  • 13. 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
  • 14. Scenario Classification • Data Parallelism • Task Parallelism • Asynchronous programming • Agents/Actors • Dataflows
  • 15. Data Parallelism – Recipe • 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!
  • 16. Data Parallelism - Tools • Parallel.For / Parallel.ForEach • PLINQ • Partitioners
  • 17. Parallel class Methods • Parallel execution of lambdas • Blocking calls! • We specify  Cancellation Token  Maximum number of Threads  Task Scheduler
  • 18. PLINQ • LINQ Queries • Potentially multiple threads • Parallel operators • Unordered results • Beware of races List<int> list = new List<int>(); var q = src.AsParallel() .Select(x => { list.Add(x); return x; }) .Where(x => true) .Take(100);
  • 19. What it can’t do • Doesn’t use SSE instructions • Doesn’t use the GPU • Isn’t using the CPU at 100%
  • 20.
  • 21. Scenaria • Data Parallelism • Task Parallelism • Asynchronous programming • Agents/Actors • Dataflows
  • 22. Task Parellelism – Recipe • 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
  • 23. The Improvements • Tasks wherever code blocks • Cancellation • Lazy Initialization • Progress Reporting • Synchronization Contexts
  • 24. Cancellation • 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
  • 25. Progress Reporting • 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
  • 26. Lazy Initialization • Calculate a value only when needed • Lazy<T>(Func<T> …) • Synchronous or Asynchronous calculation  Lazy.Value  Lazy.GetValueAsync<T>()
  • 27. Synchronization Context • 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
  • 28. Scenaria • Data Parallelism • Task Parallelism • Asynchronous programming • Agents/Actors • Dataflows
  • 29. Async/Await • 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
  • 30. Asynchronous Retry 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--; } } }
  • 31.
  • 32. More Goodies - Collections • Highly concurrent • Thread-safe • Not only for TPL/PLINQ • Producer/Consumer scenaria
  • 33. Concurrent Collections - 2 • ConcurrentQueue • ConcurrentStack • ConcurrentDictionary
  • 34. The Odd one - ConcurrentBag • Duplicates allowed • List per Thread • Reduced collisions for each tread’s Add/Take • BAD for Producer/Consumer
  • 35. Concurrent Collections - Gotchas • 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
  • 36. Also in .NET 4 • Visual Studio 2012 • Async Targeting package • System.Net.HttpClient package
  • 37. Other Technologies • F# async • C++ Parallel Patterns Library • C++ Concurrency Runtime • C++ Agents • C++ AMP
  • 38. • 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
  • 40. Pithos API • REST API base on CloudFiles by Rackspace  Compatible with CyberDuck etc • Block storage • Uploads only using blocks • Uses Merkle Hashing
  • 41. Pithos Client for Windows • 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
  • 42. The Architecture UI Core Networking Storage File Agent WPF CloudFiles SQLite Poll Agent MVVM Network Agent SQL Server Caliburn HttpClient Compact Micro Status Agent
  • 43. Technologies • .ΝΕΤ 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
  • 44. The challenges • 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
  • 45. Events Handling • Use producer/consumer pattern • Store events in ConcurrentQueue • Process ONLY after idle timeout
  • 46. Merkle Hashing • 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!
  • 47. Multiple slow calls • Each call a task • Concurrent REST calls per account and share • Task.WhenAll to process results
  • 48. Unreliable network • Use System.Net.Http.HttpClient • Store blocks in a cache folder • Check and reuse orphans • Asynchronous Retry of calls
  • 49. Resilience to crashes • Use Transactional NTFS if available  Thanks MS for killing it! • Update a copy and File.Replace otherwise
  • 50. Should not hang • Use of independent agents • Asynchronous operations wherever possible
  • 51. Provide Sufficient user feedback • Use WPF, MVVM • Use Progress to update the UI
  • 52. Next Steps • Create Windows 8 Dekstop and WinRT client • Use Reactive Framework ΖΗΤΟΥΝΤΑΘ ΕΘΕΛΟΝΤΕΣ
  • 53.
  • 54. Clever Tricks • Avoid Side Effects • Use Functional Style • Clean Coding • THE BIG SECRET:  Use existing, tested algorithms • IEEE, ACM Journals and libraries
  • 55. YES TPL • Simplify asynchronous or parallel code • Use out-of-the-box libraries • Scenarios that SUIT Task or Data Parallelism
  • 56. NO TPL • To accelerate “bad” algorithms • To “accelerate” database access  Use proper SQL and Indexes!  Avoid Cursors • Reporting DBs, Data Warehouse, OLAP Cubes
  • 57. When TPL is not enough • Functional languages like F#, Scala • Distributed Frameworks like Hadoop, {m}brace
  • 58. Books • 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
  • 59. Useful Links • Parallel FX Team: http://blogs.msdn.com/b/pfxteam/ • ΙΕΕΕ Computer Society http://www.computer.org • ACM http://www.acm.org