SlideShare una empresa de Scribd logo
1 de 17
Parallel Extensions
Chester Hartin
Code Review 11/30/12
History of parallelization
 Definition: a form of computation in which many
  calculations are carried out simultaneously,
  operating on the principle that large problems
  can be divided into smaller ones, which are then
  solved concurrently (“in parallel”)

 Developers have tried to improve performance
  by parallelizing problems, even before true
  multicore systems

 How is this different from multithreading?
  Multithreading is a type of parallelism
Real-life parallelization
 Consider that we have some eggs to boil (data
  to process)

 Before the early 2000s we only had 1 pot (core)
  and more eggs than we could boil at once
  (meaning we could boil >1 at a time in
  parallel, since you can fit >1 egg/pot). After the
  early 2000s we had 2 pots, thus could boil twice
  as many eggs at once.
Pots vs Boil time
Given: We have 10 eggs to boil and each egg requires 8
minutes in order to be ready to eat. Each pot holds up to
5 eggs.
Number of pots                Boil Time
1                             16 min
2                             8 min
3                             8 min
4                             8 min

Something interesting occurs… Adding more than 2 pots
does nothing to decrease the overall time.
Time efficiency




              Time to boil 100 eggs
What does this mean?
 It really doesn’t matter how many cores we use.
  This problem simply will not speed up by adding
  cores.
 Our equations are pretty simple:
  Pots Needed = Number of Eggs/Eggs Per Pot
    20 = 100/5
  Time = Ceiling(Pots Needed/Pots) x Boiling Time
    160 = Celing(20/1) x 8

 In Computer Terms:
  ExecutionTime = Ceiling(Amount of
   threads/Cores)xThreadExecutionTime
Caveat
 In the egg example we assume…
  Thread execution time is constant (never happens)
  Presume each core executes one thread at a time
   and does not continue w/ the next thread in the
   queue until it’s finished – ie given a quad core
   processor, it can execute 4 threads and give us the
   same result as the egg boiling w/ 4 pots
Short attention span LINQ
 Warning: I only really know basic LINQ (slowly
  integrating it into the Real Feeds Project where I
  can use it)

 LINQ = Language Integrated Query

 Something something query – gotcha. Looks like
  SQL in reverse (we know SQL, right?)

 Layman's terms – LINQ works against collections
  of data (any data really that has an enumerator)
  to get a all or subset of data
Simple LINQ
var ages =   new List<int>(){ 25, 21, 18, 65};

var agesInOrder = from age in ages

                  orderby age ascending

                  select   age;
Parallelization and LINQ
 Let’s vary our boil time
       Egg                   Batch   Boil Time (min)
       1                     1       8
       2                     1       4
       3                     1       8
       4                     1       8
       5                     1       4
       6                     2       4
       7                     2       4
       8                     2       8
       9                     2       6
Ex 2
 Take a collection of egg boil times

 Iterate over the collection and look at 5 items at
  a time
  Find the longest cooking time for the egg in the
   current patch

 Simulate the boiling time with Thread.Sleep

 Stop looking for eggs when there are <5 eggs in
  the current batch
Ex 2 (cont)
 First run – ~1600ms

 2nd run - ~1200ms why?
  Put 5 eggs in the pot
  After 4 min, remove 2nd from last egg
  After 8 min, remove remaining egg
  Add next batch that contains 1 egg
  After 4min remove the egg from the pot
LINQ to the rescue
 Any for / foreach look can potentially be
  converted into LINQ

 Compare Boil() code v1 & v2

 Note :
  Optimized (1600ms vs 1200ms)
  Nicer to read
Parallelize It!
 We have 2 options
  Parallel extensions
  Parallel LINQ
Parallel Extensions
 Introduced in .Net 4.0

 Has 2 important methods that we’ll focus on
    Parallel.For
      Parallel.For(0, eggs.Length, I => {});
    Parallel.ForEach
Parallel LINQ
 Say we have a list of web requests that we need
  to do

 Each call takes a certain amount of time & we
  want to parallelize it

 In previous examples we’ve relied on an index,
  but say if we can’t
Example 3

Más contenido relacionado

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Parallel extensions

  • 2. History of parallelization  Definition: a form of computation in which many calculations are carried out simultaneously, operating on the principle that large problems can be divided into smaller ones, which are then solved concurrently (“in parallel”)  Developers have tried to improve performance by parallelizing problems, even before true multicore systems  How is this different from multithreading? Multithreading is a type of parallelism
  • 3. Real-life parallelization  Consider that we have some eggs to boil (data to process)  Before the early 2000s we only had 1 pot (core) and more eggs than we could boil at once (meaning we could boil >1 at a time in parallel, since you can fit >1 egg/pot). After the early 2000s we had 2 pots, thus could boil twice as many eggs at once.
  • 4. Pots vs Boil time Given: We have 10 eggs to boil and each egg requires 8 minutes in order to be ready to eat. Each pot holds up to 5 eggs. Number of pots Boil Time 1 16 min 2 8 min 3 8 min 4 8 min Something interesting occurs… Adding more than 2 pots does nothing to decrease the overall time.
  • 5. Time efficiency Time to boil 100 eggs
  • 6. What does this mean?  It really doesn’t matter how many cores we use. This problem simply will not speed up by adding cores.  Our equations are pretty simple:  Pots Needed = Number of Eggs/Eggs Per Pot  20 = 100/5  Time = Ceiling(Pots Needed/Pots) x Boiling Time  160 = Celing(20/1) x 8  In Computer Terms:  ExecutionTime = Ceiling(Amount of threads/Cores)xThreadExecutionTime
  • 7. Caveat  In the egg example we assume…  Thread execution time is constant (never happens)  Presume each core executes one thread at a time and does not continue w/ the next thread in the queue until it’s finished – ie given a quad core processor, it can execute 4 threads and give us the same result as the egg boiling w/ 4 pots
  • 8. Short attention span LINQ  Warning: I only really know basic LINQ (slowly integrating it into the Real Feeds Project where I can use it)  LINQ = Language Integrated Query  Something something query – gotcha. Looks like SQL in reverse (we know SQL, right?)  Layman's terms – LINQ works against collections of data (any data really that has an enumerator) to get a all or subset of data
  • 9. Simple LINQ var ages = new List<int>(){ 25, 21, 18, 65}; var agesInOrder = from age in ages orderby age ascending select age;
  • 10. Parallelization and LINQ  Let’s vary our boil time Egg Batch Boil Time (min) 1 1 8 2 1 4 3 1 8 4 1 8 5 1 4 6 2 4 7 2 4 8 2 8 9 2 6
  • 11. Ex 2  Take a collection of egg boil times  Iterate over the collection and look at 5 items at a time  Find the longest cooking time for the egg in the current patch  Simulate the boiling time with Thread.Sleep  Stop looking for eggs when there are <5 eggs in the current batch
  • 12. Ex 2 (cont)  First run – ~1600ms  2nd run - ~1200ms why?  Put 5 eggs in the pot  After 4 min, remove 2nd from last egg  After 8 min, remove remaining egg  Add next batch that contains 1 egg  After 4min remove the egg from the pot
  • 13. LINQ to the rescue  Any for / foreach look can potentially be converted into LINQ  Compare Boil() code v1 & v2  Note :  Optimized (1600ms vs 1200ms)  Nicer to read
  • 14. Parallelize It!  We have 2 options  Parallel extensions  Parallel LINQ
  • 15. Parallel Extensions  Introduced in .Net 4.0  Has 2 important methods that we’ll focus on  Parallel.For  Parallel.For(0, eggs.Length, I => {});  Parallel.ForEach
  • 16. Parallel LINQ  Say we have a list of web requests that we need to do  Each call takes a certain amount of time & we want to parallelize it  In previous examples we’ve relied on an index, but say if we can’t