SlideShare a Scribd company logo
1 of 66
Download to read offline
SESSION CODE: ARC-300-3



Patterns of
Parallel Programming
with .NET 4
Ade Miller
(adem@microsoft.com )
Microsoft patterns & practices
Introduction

• Why you should care?

• Where to start?

• Patterns walkthrough

• Conclusions (and a quiz)
Why Care?

                            Xeon Nehalem-EX            Moore’s Law
          Intel CPU Trends
     (sources: Intel, Wikipedia, K. Olukotun)

                           Pentium 4


                Pentium
                                                       Clock Speed
         386
                                                       Power

                                                       ILP


                                                2029
The End of The Free Lunch

• Although driven by hardware changes,
  the parallel revolution is primarily a software
  revolution.
• Parallel hardware is not “more of the same.”
• Software is the gating factor.
• Software requires the most changes to regain
  the “free lunch.”
• Hardware parallelism is coming,
  more and sooner than most people yet believe.
Where Should I Start?

• “Avoid multithreaded code”
• “Parallel programming is hard”
• “It’s for the experts”

• How do we succeed in this new parallel
  world?

• Let’s look at an application and see…
An Example: Adatum-Dash

• A Financial application for portfolio risk analysis
• Look at large chunks of recent and historical
  data
• Compare models with market conditions




• Source code available:
  http://parallelpatterns.codeplex.com/
The Adatum Dash Scenario
DEMONSTRATION


Adatum Dash
Finding Potential Parallelism

• Tasks vs. Data

• Control Flow



• Control and
  Data Flow
Data Parallelism

• Data “chunk” size?
  • Too big – under utilization
  • Too small – thrashing
• Chunk layout?
  • Cache and cache line size
  • False cache sharing
• Data dependencies?
Task Parallelism

• Enough tasks?
  • Too many – thrashing
  • Too few – under utilization
• Work per task?
  • Small workloads
  • Variable workloads
• Dependencies between tasks?
  • Removable
  • Separable
  • Read only or read/write
Control and Data Flow

• Task constraints
   • Temporal: A → B
   • Simultaneous: A ↔ B
   • None: A B
• External constraints
   • I/O read or write order
   • Message or list output order
• Linear and irregular orderings
   • Pipeline
   • Futures
   • Dynamic Tasks
Solution Forces

• Flexibility:
   • Easy to modify for different scenarios
   • Runs on different types of hardware
• Efficiency:
   • Time spent managing the parallelism vs. time gained
     from utilizing more processors or cores
   • Performance improves as more cores or processors
     are added – Scaling
• Simplicity:
   • The code can be easily debugged and maintained
The Adatum Dash Scenario
The Futures Pattern
The Futures Pattern

            “Does the ordering of steps in
            your algorithm depend on data
            flow constraints?”

            • Directed Acyclic Graph
            • Dependencies between tasks
            • F4 depends on the result of F1
              & F3 etc.

            • Also called “Task Graph”
Task Size and Granularity

• Variable size tasks – harder to balance
• Small tasks – more overhead; management
  and communication
• Large tasks – less potential for utilization
• Hardware specific – more tasks than cores
Course Grained Partition
Fine Grained Partition
DEMONSTRATION


Futures
Finer Grained Partition
Data Parallelism Patterns
The Parallel Loop Pattern

“Do you have sequential loops where there's no
communication among the steps of each
iteration?”




• A very common problem!
The Parallel Aggregation Pattern

“Do you need to summarize data by applying some
kind of combination operator? Do you have loops
with steps that are not fully independent?”

• Calculate sub-problem
  result per task
• Merge results later
• Reduces need for locking

• “Reduction” or “map/reduce”
DEMONSTRATION


Parallel Loops and Aggregation
The Parallel Tasks Pattern
The Parallel Tasks Pattern

“Do you have specific units of work with
well-defined control dependencies?”
Partitioning

• How do we divide up the
  workload?
  • Fixed workloads
  • Variable workloads
• Workload size
  • Too large – hard to balance
  • Too small – communication may dominate
Workload Balancing

• Static allocation:
  • By blocks
  • By index (interleaved)
  • Guided
• Dynamic work allocation
  • known and unknown task sizes
  • Task queues
  • Work stealing
• The TPL does a lot of this work for you
Sharing State and Synchronization

•   Don’t share!
•   Read only data
•   Data isolation
•   Synchronization
DEMONSTRATION


Parallel Tasks
The Pipeline Pattern
The Pipeline Pattern

“Does your application perform a sequence of
operations repetitively? Does the input data
have streaming characteristics?”

A            B           C            D

    1            1           1            1

    2            2           2

    3            3
The Producer/Consumer Pattern

• Organize work by FIFO order

• Producers… produce!
  • Block when buffer full
• Consumers… consume!
  • Block when buffer empty


• Limits the work in progress.
Workload Balancing

• Pipeline length
  • Long – High throughput
  • Short – Low latency
• Stage workloads
  • Equal – linear pipeline
  • Unequal – nonlinear pipeline
Passing Data Down the Pipe

• Shared queue(s)
  • Large queue work items – under utilization
  • Small queue work items – locking overhead
DEMONSTRATION


Pipeline
Opportunities for Parallelism
                                Pipeline




           Parallel
            Loops

                      Futures
      Parallel
    Aggregation
                                Parallel
                                 Tasks
Key Take Aways…
Don’t Just Rewrite Your Loops

• Understand your application before you start
  • Profile
  • Measure
• What matters to your users?

• Architect your application for parallelism
“Architect for Parallelism”

• Find the potential parallelism
  • Understand your tasks and data
  • Understand their dependencies
• Maximize the potential parallelism
  • Less shared data (and locking)
  • More immutable state
• Look for patterns
  • Be wary when they don’t seem to fit
Watch Out For…

• Scheduling and schedulers
• Exception and error handling
Q&A
Now Read The Book!

Parallel Programming with Microsoft .NET:
Design Patterns for Decomposition and
Coordination on Multicore Architectures

Colin Campbell, Ralph Johnson,
Ade Miller and Stephen Toub.
Foreword by Tony Hey


http://parallelpatterns.codeplex.com/
Samples in C#, Visual Basic and F#
QUESTIONS?
What else do you want to know?
What About Recursive Problems?

• Many problems can be tackled using
  recursion:
  • Task based: Divide and Conquer
  • Data based: Recursive Data
Dynamic Task Parallelism Pattern
Dynamic Task Parallelism Pattern

“Does your algorithm divide the problem
domain dynamically during the run? Do you
operate on recursive data structures such as
graphs?”
Workload Balancing

• Deep trees – thrashing
  • Limit the tree depth
• Shallow trees – under utilization
• Unbalanced Trees – under utilization
Dynamic Task Parallelism Example

void Walk<T>(Tree<T> root, Action<T>
action) {
    if (root == null) return;
    var t1 = Task.Factory.StartNew(() =>
        action(root.Data));
    var t2 = Task.Factory.StartNew(() =>
        Walk(root.Left, action));
    var t3 = Task.Factory.StartNew(() =>
        Walk(root.Right, action));
    Task.WaitAll(t1, t2, t3);
}
Other Resources

        Books
        •   Patterns for Parallel Programming – Mattson, Sanders &
            Massingill
        •   Design Patterns – Gamma, Helm, Johnson & Vlissides
        •   Head First Design Patterns – Freeman & Freeman
        •   Patterns of Enterprise Application Architecture – Fowler


        Research
        •   A Pattern Language for Parallel Programming ver2.0
        •   ParaPLOP - Workshop on Parallel Programming Patterns

        •   My Blog: http://ademiller.com/tech/ (Decks etc.)
Divide &        Distributed
  Pipeline            Actors
                                                                          Conquer            Array
                                                                                                         Map
                     MPMD                                                                               Reduce
   Producer/
   Consumer


                        SPMD           Parallel
                                        Loops

                                                    Master/                                 Shared       Repository
                                                                  Fork/
                                                    Worker                                  Queue
                                                                   Join
                                                               Task
                                                              Graph




                                                                             SOA
                                                                            Facade



Source: More Patterns for Parallel Application Programs, Berna L. Massingill, Timothy G. Mattson and Beverly A. Sanders
Supporting Patterns

• “Gang of Four” Patterns
  • Façade
  • Decorator
  • Repository
• Shared Data Patterns
  • Shared Queue
The Façade Pattern

  • Hide parallelism
  • Optimize call granularity




Source: Design Patterns – Gamma, Helm, Johnson & Vlissides
        Patterns of Enterprise Application Architecture - Fowler
The Decorator Pattern

  • Encapsulate parallelism
         • Calling code is parallel agnostic
  • Add parallelism to an existing (base) class

                                 ConcurrentFoo

                                       IFoo            Foo




Source: Design Patterns – Gamma, Helm, Johnson & Vlissides
The Repository Pattern
                                                                    Shared hash or queue
                                                                    Database
                                                                    Distributed cache




Source: Patterns of Enterprise Application Architecture - Fowler
The Shared Queue Pattern
                or… Task Queue

 • A decorator to Queue
        • Hides the locking which protects the underlying
          queue
 • Facilitates Producer/Consumer pattern
        • Producers call:
            theQueue.Enqueue()
        • Consumers call:
            theQueue.Dequeue()

Source: Patterns for Parallel Programming – Mattson, Sanders & Massingill
Shared Queue Example

var results =
    new ConcurrentQueue<Result>();

Parallel.For(0, 1000, (i) =>
{
    Result result =
      ComputeResult(i);
    results.Enqueue(result);
});
Parallel With PLINQ

var accountRatings =
    accounts.AsParallel()
        .Select(item =>
            CreditRating(item))
        .ToList();
Loop Parallel Examples

Parallel.For (0, acc.Length, i =>
{
    acc[i].UpdateCreditRating();
});

#pragma omp parallel for
for (int i = 0; i < len; i++)
{
    acc[i].UpdateCreditRating();
}
Parallel Tasks Example

Parallel.Invoke(
    () => ComputeMean(),
    () => ComputeMedian()
);

parallel_invoke(
    [&] { ComputeMean(); },
    [&] { ComputeMedian(); },
);
Pipeline Examples
var inp = new BlockingCollection<string>();

var readLines = Task.Factory.StartNew(() =>
{
    try {
        foreach(var line in File.ReadAllLines(@"input.txt"))
            inp.Add(line);
    }
    finally { input.CompleteAdding(); }
});

var writeLines = Task.Factory.StartNew(() =>
{
    File.WriteAllLines(@”out.txt", inp.GetConsumingEnumerator());
});
Task.WaitAll(readLines, writeLines);
Pipeline Examples

Get-ChildItem C: |
Where-Object {$_.Length -gt 2KB} |
    Sort-Object Length
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should
 not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS,
                                                                           IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
Arc 300-3 ade miller-en

More Related Content

Viewers also liked

1201 2012 us training catalog final
1201 2012 us training catalog final1201 2012 us training catalog final
1201 2012 us training catalog finalMartin Cowling
 
1201 2012 training catalogue final
1201 2012 training catalogue final1201 2012 training catalogue final
1201 2012 training catalogue finalMartin Cowling
 
构建可扩展的微博系统
构建可扩展的微博系统构建可扩展的微博系统
构建可扩展的微博系统lonegunman
 
How To Log Into The Volunteer Center
How To Log Into The Volunteer CenterHow To Log Into The Volunteer Center
How To Log Into The Volunteer CenterMichael Gilman
 

Viewers also liked (7)

Magnets attract
Magnets attractMagnets attract
Magnets attract
 
1201 2012 us training catalog final
1201 2012 us training catalog final1201 2012 us training catalog final
1201 2012 us training catalog final
 
1201 2012 training catalogue final
1201 2012 training catalogue final1201 2012 training catalogue final
1201 2012 training catalogue final
 
构建可扩展的微博系统
构建可扩展的微博系统构建可扩展的微博系统
构建可扩展的微博系统
 
Creating an exceptional member volunteer experience
Creating an exceptional member volunteer experienceCreating an exceptional member volunteer experience
Creating an exceptional member volunteer experience
 
How To Log Into The Volunteer Center
How To Log Into The Volunteer CenterHow To Log Into The Volunteer Center
How To Log Into The Volunteer Center
 
Volunteer magnet2nded
Volunteer magnet2ndedVolunteer magnet2nded
Volunteer magnet2nded
 

Similar to Arc 300-3 ade miller-en

Building a highly scalable and available cloud application
Building a highly scalable and available cloud applicationBuilding a highly scalable and available cloud application
Building a highly scalable and available cloud applicationNoam Sheffer
 
Spark Overview and Performance Issues
Spark Overview and Performance IssuesSpark Overview and Performance Issues
Spark Overview and Performance IssuesAntonios Katsarakis
 
Взгляд на облака с точки зрения HPC
Взгляд на облака с точки зрения HPCВзгляд на облака с точки зрения HPC
Взгляд на облака с точки зрения HPCOlga Lavrentieva
 
High Performance Computing - Cloud Point of View
High Performance Computing - Cloud Point of ViewHigh Performance Computing - Cloud Point of View
High Performance Computing - Cloud Point of Viewaragozin
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with EpsilonSina Madani
 
Intro to Big Data and NoSQL
Intro to Big Data and NoSQLIntro to Big Data and NoSQL
Intro to Big Data and NoSQLDon Demcsak
 
Alex mang patterns for scalability in microsoft azure application
Alex mang   patterns for scalability in microsoft azure applicationAlex mang   patterns for scalability in microsoft azure application
Alex mang patterns for scalability in microsoft azure applicationCodecamp Romania
 
VTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computingVTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computingSachin Gowda
 
Mining big data streams with APACHE SAMOA by Albert Bifet
Mining big data streams with APACHE SAMOA by Albert BifetMining big data streams with APACHE SAMOA by Albert Bifet
Mining big data streams with APACHE SAMOA by Albert BifetJ On The Beach
 
Distributed Processing of Stream Text Mining
Distributed Processing of Stream Text MiningDistributed Processing of Stream Text Mining
Distributed Processing of Stream Text MiningLi Miao
 
Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesDavid Martínez Rego
 
Distributed Data processing in a Cloud
Distributed Data processing in a CloudDistributed Data processing in a Cloud
Distributed Data processing in a Cloudelliando dias
 
World-class Data Engineering with Amazon Redshift
World-class Data Engineering with Amazon RedshiftWorld-class Data Engineering with Amazon Redshift
World-class Data Engineering with Amazon RedshiftLars Kamp
 
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...Jose Quesada (hiring)
 
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 2017Casey Kinsey
 
History of database processing module 1 (2)
History of database processing module 1 (2)History of database processing module 1 (2)
History of database processing module 1 (2)chottu89
 
Coding For Cores - C# Way
Coding For Cores - C# WayCoding For Cores - C# Way
Coding For Cores - C# WayBishnu Rawal
 
The Analytics Frontier of the Hadoop Eco-System
The Analytics Frontier of the Hadoop Eco-SystemThe Analytics Frontier of the Hadoop Eco-System
The Analytics Frontier of the Hadoop Eco-Systeminside-BigData.com
 

Similar to Arc 300-3 ade miller-en (20)

Building a highly scalable and available cloud application
Building a highly scalable and available cloud applicationBuilding a highly scalable and available cloud application
Building a highly scalable and available cloud application
 
Spark Overview and Performance Issues
Spark Overview and Performance IssuesSpark Overview and Performance Issues
Spark Overview and Performance Issues
 
Взгляд на облака с точки зрения HPC
Взгляд на облака с точки зрения HPCВзгляд на облака с точки зрения HPC
Взгляд на облака с точки зрения HPC
 
High Performance Computing - Cloud Point of View
High Performance Computing - Cloud Point of ViewHigh Performance Computing - Cloud Point of View
High Performance Computing - Cloud Point of View
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with Epsilon
 
Intro to Big Data and NoSQL
Intro to Big Data and NoSQLIntro to Big Data and NoSQL
Intro to Big Data and NoSQL
 
DataOps with Project Amaterasu
DataOps with Project AmaterasuDataOps with Project Amaterasu
DataOps with Project Amaterasu
 
Alex mang patterns for scalability in microsoft azure application
Alex mang   patterns for scalability in microsoft azure applicationAlex mang   patterns for scalability in microsoft azure application
Alex mang patterns for scalability in microsoft azure application
 
VTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computingVTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computing
 
Mining big data streams with APACHE SAMOA by Albert Bifet
Mining big data streams with APACHE SAMOA by Albert BifetMining big data streams with APACHE SAMOA by Albert Bifet
Mining big data streams with APACHE SAMOA by Albert Bifet
 
Distributed Processing of Stream Text Mining
Distributed Processing of Stream Text MiningDistributed Processing of Stream Text Mining
Distributed Processing of Stream Text Mining
 
Building Big Data Streaming Architectures
Building Big Data Streaming ArchitecturesBuilding Big Data Streaming Architectures
Building Big Data Streaming Architectures
 
Distributed Data processing in a Cloud
Distributed Data processing in a CloudDistributed Data processing in a Cloud
Distributed Data processing in a Cloud
 
World-class Data Engineering with Amazon Redshift
World-class Data Engineering with Amazon RedshiftWorld-class Data Engineering with Amazon Redshift
World-class Data Engineering with Amazon Redshift
 
try
trytry
try
 
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
A full Machine learning pipeline in Scikit-learn vs in scala-Spark: pros and ...
 
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
 
History of database processing module 1 (2)
History of database processing module 1 (2)History of database processing module 1 (2)
History of database processing module 1 (2)
 
Coding For Cores - C# Way
Coding For Cores - C# WayCoding For Cores - C# Way
Coding For Cores - C# Way
 
The Analytics Frontier of the Hadoop Eco-System
The Analytics Frontier of the Hadoop Eco-SystemThe Analytics Frontier of the Hadoop Eco-System
The Analytics Frontier of the Hadoop Eco-System
 

Recently uploaded

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Recently uploaded (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Arc 300-3 ade miller-en

  • 1.
  • 2. SESSION CODE: ARC-300-3 Patterns of Parallel Programming with .NET 4 Ade Miller (adem@microsoft.com ) Microsoft patterns & practices
  • 3. Introduction • Why you should care? • Where to start? • Patterns walkthrough • Conclusions (and a quiz)
  • 4. Why Care? Xeon Nehalem-EX Moore’s Law Intel CPU Trends (sources: Intel, Wikipedia, K. Olukotun) Pentium 4 Pentium Clock Speed 386 Power ILP 2029
  • 5. The End of The Free Lunch • Although driven by hardware changes, the parallel revolution is primarily a software revolution. • Parallel hardware is not “more of the same.” • Software is the gating factor. • Software requires the most changes to regain the “free lunch.” • Hardware parallelism is coming, more and sooner than most people yet believe.
  • 6. Where Should I Start? • “Avoid multithreaded code” • “Parallel programming is hard” • “It’s for the experts” • How do we succeed in this new parallel world? • Let’s look at an application and see…
  • 7. An Example: Adatum-Dash • A Financial application for portfolio risk analysis • Look at large chunks of recent and historical data • Compare models with market conditions • Source code available: http://parallelpatterns.codeplex.com/
  • 8. The Adatum Dash Scenario
  • 10. Finding Potential Parallelism • Tasks vs. Data • Control Flow • Control and Data Flow
  • 11. Data Parallelism • Data “chunk” size? • Too big – under utilization • Too small – thrashing • Chunk layout? • Cache and cache line size • False cache sharing • Data dependencies?
  • 12. Task Parallelism • Enough tasks? • Too many – thrashing • Too few – under utilization • Work per task? • Small workloads • Variable workloads • Dependencies between tasks? • Removable • Separable • Read only or read/write
  • 13. Control and Data Flow • Task constraints • Temporal: A → B • Simultaneous: A ↔ B • None: A B • External constraints • I/O read or write order • Message or list output order • Linear and irregular orderings • Pipeline • Futures • Dynamic Tasks
  • 14. Solution Forces • Flexibility: • Easy to modify for different scenarios • Runs on different types of hardware • Efficiency: • Time spent managing the parallelism vs. time gained from utilizing more processors or cores • Performance improves as more cores or processors are added – Scaling • Simplicity: • The code can be easily debugged and maintained
  • 15. The Adatum Dash Scenario
  • 17. The Futures Pattern “Does the ordering of steps in your algorithm depend on data flow constraints?” • Directed Acyclic Graph • Dependencies between tasks • F4 depends on the result of F1 & F3 etc. • Also called “Task Graph”
  • 18. Task Size and Granularity • Variable size tasks – harder to balance • Small tasks – more overhead; management and communication • Large tasks – less potential for utilization • Hardware specific – more tasks than cores
  • 24. The Parallel Loop Pattern “Do you have sequential loops where there's no communication among the steps of each iteration?” • A very common problem!
  • 25. The Parallel Aggregation Pattern “Do you need to summarize data by applying some kind of combination operator? Do you have loops with steps that are not fully independent?” • Calculate sub-problem result per task • Merge results later • Reduces need for locking • “Reduction” or “map/reduce”
  • 28. The Parallel Tasks Pattern “Do you have specific units of work with well-defined control dependencies?”
  • 29. Partitioning • How do we divide up the workload? • Fixed workloads • Variable workloads • Workload size • Too large – hard to balance • Too small – communication may dominate
  • 30. Workload Balancing • Static allocation: • By blocks • By index (interleaved) • Guided • Dynamic work allocation • known and unknown task sizes • Task queues • Work stealing • The TPL does a lot of this work for you
  • 31. Sharing State and Synchronization • Don’t share! • Read only data • Data isolation • Synchronization
  • 34. The Pipeline Pattern “Does your application perform a sequence of operations repetitively? Does the input data have streaming characteristics?” A B C D 1 1 1 1 2 2 2 3 3
  • 35. The Producer/Consumer Pattern • Organize work by FIFO order • Producers… produce! • Block when buffer full • Consumers… consume! • Block when buffer empty • Limits the work in progress.
  • 36. Workload Balancing • Pipeline length • Long – High throughput • Short – Low latency • Stage workloads • Equal – linear pipeline • Unequal – nonlinear pipeline
  • 37. Passing Data Down the Pipe • Shared queue(s) • Large queue work items – under utilization • Small queue work items – locking overhead
  • 39. Opportunities for Parallelism Pipeline Parallel Loops Futures Parallel Aggregation Parallel Tasks
  • 41. Don’t Just Rewrite Your Loops • Understand your application before you start • Profile • Measure • What matters to your users? • Architect your application for parallelism
  • 42. “Architect for Parallelism” • Find the potential parallelism • Understand your tasks and data • Understand their dependencies • Maximize the potential parallelism • Less shared data (and locking) • More immutable state • Look for patterns • Be wary when they don’t seem to fit
  • 43. Watch Out For… • Scheduling and schedulers • Exception and error handling
  • 44. Q&A
  • 45. Now Read The Book! Parallel Programming with Microsoft .NET: Design Patterns for Decomposition and Coordination on Multicore Architectures Colin Campbell, Ralph Johnson, Ade Miller and Stephen Toub. Foreword by Tony Hey http://parallelpatterns.codeplex.com/ Samples in C#, Visual Basic and F#
  • 46. QUESTIONS? What else do you want to know?
  • 47. What About Recursive Problems? • Many problems can be tackled using recursion: • Task based: Divide and Conquer • Data based: Recursive Data
  • 49. Dynamic Task Parallelism Pattern “Does your algorithm divide the problem domain dynamically during the run? Do you operate on recursive data structures such as graphs?”
  • 50. Workload Balancing • Deep trees – thrashing • Limit the tree depth • Shallow trees – under utilization • Unbalanced Trees – under utilization
  • 51. Dynamic Task Parallelism Example void Walk<T>(Tree<T> root, Action<T> action) { if (root == null) return; var t1 = Task.Factory.StartNew(() => action(root.Data)); var t2 = Task.Factory.StartNew(() => Walk(root.Left, action)); var t3 = Task.Factory.StartNew(() => Walk(root.Right, action)); Task.WaitAll(t1, t2, t3); }
  • 52. Other Resources Books • Patterns for Parallel Programming – Mattson, Sanders & Massingill • Design Patterns – Gamma, Helm, Johnson & Vlissides • Head First Design Patterns – Freeman & Freeman • Patterns of Enterprise Application Architecture – Fowler Research • A Pattern Language for Parallel Programming ver2.0 • ParaPLOP - Workshop on Parallel Programming Patterns • My Blog: http://ademiller.com/tech/ (Decks etc.)
  • 53. Divide & Distributed Pipeline Actors Conquer Array Map MPMD Reduce Producer/ Consumer SPMD Parallel Loops Master/ Shared Repository Fork/ Worker Queue Join Task Graph SOA Facade Source: More Patterns for Parallel Application Programs, Berna L. Massingill, Timothy G. Mattson and Beverly A. Sanders
  • 54. Supporting Patterns • “Gang of Four” Patterns • Façade • Decorator • Repository • Shared Data Patterns • Shared Queue
  • 55. The Façade Pattern • Hide parallelism • Optimize call granularity Source: Design Patterns – Gamma, Helm, Johnson & Vlissides Patterns of Enterprise Application Architecture - Fowler
  • 56. The Decorator Pattern • Encapsulate parallelism • Calling code is parallel agnostic • Add parallelism to an existing (base) class ConcurrentFoo IFoo Foo Source: Design Patterns – Gamma, Helm, Johnson & Vlissides
  • 57. The Repository Pattern  Shared hash or queue  Database  Distributed cache Source: Patterns of Enterprise Application Architecture - Fowler
  • 58. The Shared Queue Pattern or… Task Queue • A decorator to Queue • Hides the locking which protects the underlying queue • Facilitates Producer/Consumer pattern • Producers call: theQueue.Enqueue() • Consumers call: theQueue.Dequeue() Source: Patterns for Parallel Programming – Mattson, Sanders & Massingill
  • 59. Shared Queue Example var results = new ConcurrentQueue<Result>(); Parallel.For(0, 1000, (i) => { Result result = ComputeResult(i); results.Enqueue(result); });
  • 60. Parallel With PLINQ var accountRatings = accounts.AsParallel() .Select(item => CreditRating(item)) .ToList();
  • 61. Loop Parallel Examples Parallel.For (0, acc.Length, i => { acc[i].UpdateCreditRating(); }); #pragma omp parallel for for (int i = 0; i < len; i++) { acc[i].UpdateCreditRating(); }
  • 62. Parallel Tasks Example Parallel.Invoke( () => ComputeMean(), () => ComputeMedian() ); parallel_invoke( [&] { ComputeMean(); }, [&] { ComputeMedian(); }, );
  • 63. Pipeline Examples var inp = new BlockingCollection<string>(); var readLines = Task.Factory.StartNew(() => { try { foreach(var line in File.ReadAllLines(@"input.txt")) inp.Add(line); } finally { input.CompleteAdding(); } }); var writeLines = Task.Factory.StartNew(() => { File.WriteAllLines(@”out.txt", inp.GetConsumingEnumerator()); }); Task.WaitAll(readLines, writeLines);
  • 64. Pipeline Examples Get-ChildItem C: | Where-Object {$_.Length -gt 2KB} | Sort-Object Length
  • 65. © 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.