SlideShare a Scribd company logo
PARALLEL
PROGRAMMING
Programming to leverage multicores or multiple processors is
called parallel programming. This is a subset of the broader
concept of multithreading. - http://www.albahari.com/
ANUJ KUMAR SAHU
AGENDA
• Introduction and Concept
• PLINQ
• TPL - Task Parallelism
• Parallel Class
• Demos
INTRODUCTION
• Many personal computers and workstations have two or
four cores (that is, CPUs) that enable multiple threads to
be executed simultaneously.
• To take advantage of the hardware of today and
tomorrow, you can parallelize your code to distribute
work across multiple processors.
• In the past, parallelization required low-level
manipulation of threads and locks.
• Visual Studio 2010 and the .NET Framework 4 enhance
support for parallel programming by providing a new
runtime, new class library types, and new diagnostic
tools.
• These APIs are collectively known (loosely) as PFX
(Parallel Framework).
Image Source - http://www.albahari.com/
CONCEPT
Two strategies for partitioning work among
threads: data parallelism and task parallelism
 Data parallelism - When a set of tasks must be performed on many data
values, we can parallelize by having each thread perform the (same) set
of tasks on a subset of values.
• PLINQ
• Parallel Class
 Task parallelism - partition the tasks i.e. each thread perform a different
task.
• is the lowest-level approach to parallelization with PFX.
• tuned for leveraging multicores.
• Classes - Task ; Task<TResult> ; TaskFactory etc…
PLINQ
• PLINQ automatically parallelizes local LINQ queries.
• To use PLINQ, simply call AsParallel() on the input
sequence and then continue the LINQ query as usual.
• PLINQ is only for local collections: it doesn’t work with
LINQ to SQL or Entity Framework because in those cases
the LINQ translates into SQL which then executes on a
database server.
• If you need order preservation, you can force it by
calling AsOrdered() after AsParallel(); however incurs a
performance hit with large numbers of elements.
• Does not works - Take, TakeWhile, Skip,
and SkipWhile; indexed versions of Select, SelectMany,
and ElementAt.
PLINQ - CODE
PARALLEL CLASS
• Three static methods in the Parallel class
o Parallel.Invoke - can execute several delegates in parallel. It takes array
of tasks (Actions) as parameters.
o Parallel.For - fromInclusive, toExclusive and step.
o Parallel.ForEach - is a multi-threaded implementation of a common loop
construct in C#, the foreach loop. It has numerous overloads; the most
commonly used has the following signature:
public static ParallelLoopResult ForEach<TSource>(
IEnumerable<TSource> source,
Action<TSource> body)
TPL – TASK PARALLEL LIBRARY
Class Purpose
Task For managing a unit for work
Task<TResult> For managing a unit for work with a
return value
TaskFactory For creating tasks
TaskFactory<TResult> For creating tasks and continuations
with the same return type
TaskScheduler For managing the scheduling of tasks
TaskCompletionSource For manually controlling a task’s
workflow
WHY TASK AND NOT THREADS?
• Reference
• The creation of a thread comes with a huge cost.
Creating a huge number of Threads within your
application also comes with an overhead of
Context Switching. In a single core environment, it
might lead to a bad performance as well, since we
have a single core which serves various threads.The
task on the other hand, dynamically calculates if it
needs to create different threads of execution or
not. It uses the ThreadPool under the hood, in order
to distribute the work, without going through the
overhead of Thread creation/or un-necessary
context switching if not required.
TPL Syntax
• Creating a task
o Task.Factory.StartNew (() => Console.WriteLine ("Hello from a task!"));
o var task = new Task (() => Console.Write ("Hello"));
... task.Start();
• Waiting on Tasks
o Wait
o WaitAny
o WaitAll
• Continuations
o ContinueWith
o ContinueWhenAll
• And many mores….
ADDITONAL POINTS
• Date Race condition
• Debugging //example was built by Microsoft.
NOT COVERED
• Error handling
• Cancellation
• New .NET 4.5 async and await Features
REFRENCES
• http://www.albahari.com/threading/part5.aspx
• MSDN
• Plural Sight
• http://www.blackwasp.co.uk/ParallelProgramming.
aspx
• Code Project
• Samples

More Related Content

What's hot (20)

ODP
Building Complex Data Workflows with Cascading on Hadoop
Gagan Agrawal
 
PDF
Apache Spark MLlib 2.0 Preview: Data Science and Production
Databricks
 
PDF
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Thomas Lockney
 
PPTX
Multi-threading in the modern era: Vertx Akka and Quasar
Gal Marder
 
PDF
Flink Forward SF 2017: James Malone - Make The Cloud Work For You
Flink Forward
 
PDF
Scaling Machine Learning To Billions Of Parameters
Jen Aman
 
PPTX
Training – Going Async
Betclic Everest Group Tech Team
 
PDF
Javantura v4 - Getting started with Apache Spark - Dinko Srkoč
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PDF
Developing Secure Scala Applications With Fortify For Scala
Lightbend
 
PDF
Javantura v4 - Java and lambdas and streams - are they better than for loops ...
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PDF
Spark Summit EU talk by Kent Buenaventura and Willaim Lau
Spark Summit
 
ODP
Introduction to Scala Macros
Knoldus Inc.
 
PDF
Functional programming in Scala
datamantra
 
PPTX
Mini training - Reactive Extensions (Rx)
Betclic Everest Group Tech Team
 
PDF
Spark Summit EU talk by Luca Canali
Spark Summit
 
PDF
Scaling Apache Spark MLlib to Billions of Parameters: Spark Summit East talk ...
Spark Summit
 
PDF
Correctness and Performance of Apache Spark SQL with Bogdan Ghit and Nicolas ...
Databricks
 
PPTX
Project Reactor By Example
Denny Abraham Cheriyan
 
PDF
面向引擎——编写高效率JS
ucarticle
 
PPTX
Reactive
Pranav E K
 
Building Complex Data Workflows with Cascading on Hadoop
Gagan Agrawal
 
Apache Spark MLlib 2.0 Preview: Data Science and Production
Databricks
 
Reactive programming using rx java & akka actors - pdx-scala - june 2014
Thomas Lockney
 
Multi-threading in the modern era: Vertx Akka and Quasar
Gal Marder
 
Flink Forward SF 2017: James Malone - Make The Cloud Work For You
Flink Forward
 
Scaling Machine Learning To Billions Of Parameters
Jen Aman
 
Training – Going Async
Betclic Everest Group Tech Team
 
Javantura v4 - Getting started with Apache Spark - Dinko Srkoč
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Developing Secure Scala Applications With Fortify For Scala
Lightbend
 
Javantura v4 - Java and lambdas and streams - are they better than for loops ...
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Spark Summit EU talk by Kent Buenaventura and Willaim Lau
Spark Summit
 
Introduction to Scala Macros
Knoldus Inc.
 
Functional programming in Scala
datamantra
 
Mini training - Reactive Extensions (Rx)
Betclic Everest Group Tech Team
 
Spark Summit EU talk by Luca Canali
Spark Summit
 
Scaling Apache Spark MLlib to Billions of Parameters: Spark Summit East talk ...
Spark Summit
 
Correctness and Performance of Apache Spark SQL with Bogdan Ghit and Nicolas ...
Databricks
 
Project Reactor By Example
Denny Abraham Cheriyan
 
面向引擎——编写高效率JS
ucarticle
 
Reactive
Pranav E K
 

Viewers also liked (8)

PPTX
Develop MS Office Plugins
Mindfire Solutions
 
PDF
Java FX Part2
Mindfire Solutions
 
ODP
JavaFX in Action Part I
Mohammad Hossein Rimaz
 
ODP
Presentation - Course about JavaFX
Tom Mix Petreca
 
PDF
Robotics: Modelling, Planning and Control
Cody Ray
 
PDF
Introduction to Supervised ML Concepts and Algorithms
NBER
 
PPTX
Introduction to Scala
Mohammad Hossein Rimaz
 
PDF
Ridge regression, lasso and elastic net
Vivian S. Zhang
 
Develop MS Office Plugins
Mindfire Solutions
 
Java FX Part2
Mindfire Solutions
 
JavaFX in Action Part I
Mohammad Hossein Rimaz
 
Presentation - Course about JavaFX
Tom Mix Petreca
 
Robotics: Modelling, Planning and Control
Cody Ray
 
Introduction to Supervised ML Concepts and Algorithms
NBER
 
Introduction to Scala
Mohammad Hossein Rimaz
 
Ridge regression, lasso and elastic net
Vivian S. Zhang
 
Ad

Similar to Parallel Programming (20)

PPTX
C# Parallel programming
Umeshwaran V
 
PPTX
Distributed Model Validation with Epsilon
Sina Madani
 
PPTX
.NET Multithreading/Multitasking
Sasha Kravchuk
 
PPTX
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Sachintha Gunasena
 
PDF
Distributed Tensorflow with Kubernetes - data2day - Jakob Karalus
Jakob Karalus
 
PPTX
Training And Serving ML Model Using Kubeflow by Jayesh Sharma
CodeOps Technologies LLP
 
PPTX
End to-end async and await
vfabro
 
PDF
Neptune @ SoCal
Chris Bunch
 
PPTX
Parallel Computing-Part-1.pptx
krnaween
 
PPTX
Distributed Performance testing by funkload
Akhil Singh
 
PPTX
Data Parallel and Object Oriented Model
Nikhil Sharma
 
PDF
Impala Architecture presentation
hadooparchbook
 
PDF
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
PROIDEA
 
PDF
Neural Networks from Scratch - TensorFlow 101
Gerold Bausch
 
PPTX
python_development.pptx
LemonReddy1
 
PDF
I see deadlocks : Matt Ellis - Techorama NL 2024
citizenmatt
 
PDF
Introduction to OpenMP (Performance)
Akhila Prabhakaran
 
PPTX
VTU 6th Sem Elective CSE - Module 3 cloud computing
Sachin Gowda
 
PPTX
Async and parallel patterns and application design - TechDays2013 NL
Arie Leeuwesteijn
 
PDF
Here comes the Loom - Ya!vaConf.pdf
Krystian Zybała
 
C# Parallel programming
Umeshwaran V
 
Distributed Model Validation with Epsilon
Sina Madani
 
.NET Multithreading/Multitasking
Sasha Kravchuk
 
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Sachintha Gunasena
 
Distributed Tensorflow with Kubernetes - data2day - Jakob Karalus
Jakob Karalus
 
Training And Serving ML Model Using Kubeflow by Jayesh Sharma
CodeOps Technologies LLP
 
End to-end async and await
vfabro
 
Neptune @ SoCal
Chris Bunch
 
Parallel Computing-Part-1.pptx
krnaween
 
Distributed Performance testing by funkload
Akhil Singh
 
Data Parallel and Object Oriented Model
Nikhil Sharma
 
Impala Architecture presentation
hadooparchbook
 
JDD 2016 - Grzegorz Rozniecki - Java 8 What Could Possibly Go Wrong
PROIDEA
 
Neural Networks from Scratch - TensorFlow 101
Gerold Bausch
 
python_development.pptx
LemonReddy1
 
I see deadlocks : Matt Ellis - Techorama NL 2024
citizenmatt
 
Introduction to OpenMP (Performance)
Akhila Prabhakaran
 
VTU 6th Sem Elective CSE - Module 3 cloud computing
Sachin Gowda
 
Async and parallel patterns and application design - TechDays2013 NL
Arie Leeuwesteijn
 
Here comes the Loom - Ya!vaConf.pdf
Krystian Zybała
 
Ad

More from Mindfire Solutions (20)

PDF
Physician Search and Review
Mindfire Solutions
 
PDF
diet management app
Mindfire Solutions
 
PDF
Business Technology Solution
Mindfire Solutions
 
PDF
Remote Health Monitoring
Mindfire Solutions
 
PDF
Influencer Marketing Solution
Mindfire Solutions
 
PPT
High Availability of Azure Applications
Mindfire Solutions
 
PPTX
IOT Hands On
Mindfire Solutions
 
PPTX
Glimpse of Loops Vs Set
Mindfire Solutions
 
ODP
Oracle Sql Developer-Getting Started
Mindfire Solutions
 
PPT
Adaptive Layout In iOS 8
Mindfire Solutions
 
PPT
Introduction to Auto-layout : iOS/Mac
Mindfire Solutions
 
PPT
LINQPad - utility Tool
Mindfire Solutions
 
PPT
Get started with watch kit development
Mindfire Solutions
 
PPTX
Swift vs Objective-C
Mindfire Solutions
 
ODP
Material Design in Android
Mindfire Solutions
 
ODP
Introduction to OData
Mindfire Solutions
 
PPT
Ext js Part 2- MVC
Mindfire Solutions
 
PPT
ExtJs Basic Part-1
Mindfire Solutions
 
PPT
Spring Security Introduction
Mindfire Solutions
 
Physician Search and Review
Mindfire Solutions
 
diet management app
Mindfire Solutions
 
Business Technology Solution
Mindfire Solutions
 
Remote Health Monitoring
Mindfire Solutions
 
Influencer Marketing Solution
Mindfire Solutions
 
High Availability of Azure Applications
Mindfire Solutions
 
IOT Hands On
Mindfire Solutions
 
Glimpse of Loops Vs Set
Mindfire Solutions
 
Oracle Sql Developer-Getting Started
Mindfire Solutions
 
Adaptive Layout In iOS 8
Mindfire Solutions
 
Introduction to Auto-layout : iOS/Mac
Mindfire Solutions
 
LINQPad - utility Tool
Mindfire Solutions
 
Get started with watch kit development
Mindfire Solutions
 
Swift vs Objective-C
Mindfire Solutions
 
Material Design in Android
Mindfire Solutions
 
Introduction to OData
Mindfire Solutions
 
Ext js Part 2- MVC
Mindfire Solutions
 
ExtJs Basic Part-1
Mindfire Solutions
 
Spring Security Introduction
Mindfire Solutions
 

Recently uploaded (20)

PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Import Data Form Excel to Tally Services
Tally xperts
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
Human Resources Information System (HRIS)
Amity University, Patna
 

Parallel Programming

  • 1. PARALLEL PROGRAMMING Programming to leverage multicores or multiple processors is called parallel programming. This is a subset of the broader concept of multithreading. - http://www.albahari.com/ ANUJ KUMAR SAHU
  • 2. AGENDA • Introduction and Concept • PLINQ • TPL - Task Parallelism • Parallel Class • Demos
  • 3. INTRODUCTION • Many personal computers and workstations have two or four cores (that is, CPUs) that enable multiple threads to be executed simultaneously. • To take advantage of the hardware of today and tomorrow, you can parallelize your code to distribute work across multiple processors. • In the past, parallelization required low-level manipulation of threads and locks. • Visual Studio 2010 and the .NET Framework 4 enhance support for parallel programming by providing a new runtime, new class library types, and new diagnostic tools. • These APIs are collectively known (loosely) as PFX (Parallel Framework).
  • 4. Image Source - http://www.albahari.com/
  • 5. CONCEPT Two strategies for partitioning work among threads: data parallelism and task parallelism  Data parallelism - When a set of tasks must be performed on many data values, we can parallelize by having each thread perform the (same) set of tasks on a subset of values. • PLINQ • Parallel Class  Task parallelism - partition the tasks i.e. each thread perform a different task. • is the lowest-level approach to parallelization with PFX. • tuned for leveraging multicores. • Classes - Task ; Task<TResult> ; TaskFactory etc…
  • 6. PLINQ • PLINQ automatically parallelizes local LINQ queries. • To use PLINQ, simply call AsParallel() on the input sequence and then continue the LINQ query as usual. • PLINQ is only for local collections: it doesn’t work with LINQ to SQL or Entity Framework because in those cases the LINQ translates into SQL which then executes on a database server. • If you need order preservation, you can force it by calling AsOrdered() after AsParallel(); however incurs a performance hit with large numbers of elements. • Does not works - Take, TakeWhile, Skip, and SkipWhile; indexed versions of Select, SelectMany, and ElementAt.
  • 8. PARALLEL CLASS • Three static methods in the Parallel class o Parallel.Invoke - can execute several delegates in parallel. It takes array of tasks (Actions) as parameters. o Parallel.For - fromInclusive, toExclusive and step. o Parallel.ForEach - is a multi-threaded implementation of a common loop construct in C#, the foreach loop. It has numerous overloads; the most commonly used has the following signature: public static ParallelLoopResult ForEach<TSource>( IEnumerable<TSource> source, Action<TSource> body)
  • 9. TPL – TASK PARALLEL LIBRARY Class Purpose Task For managing a unit for work Task<TResult> For managing a unit for work with a return value TaskFactory For creating tasks TaskFactory<TResult> For creating tasks and continuations with the same return type TaskScheduler For managing the scheduling of tasks TaskCompletionSource For manually controlling a task’s workflow
  • 10. WHY TASK AND NOT THREADS? • Reference • The creation of a thread comes with a huge cost. Creating a huge number of Threads within your application also comes with an overhead of Context Switching. In a single core environment, it might lead to a bad performance as well, since we have a single core which serves various threads.The task on the other hand, dynamically calculates if it needs to create different threads of execution or not. It uses the ThreadPool under the hood, in order to distribute the work, without going through the overhead of Thread creation/or un-necessary context switching if not required.
  • 11. TPL Syntax • Creating a task o Task.Factory.StartNew (() => Console.WriteLine ("Hello from a task!")); o var task = new Task (() => Console.Write ("Hello")); ... task.Start(); • Waiting on Tasks o Wait o WaitAny o WaitAll • Continuations o ContinueWith o ContinueWhenAll • And many mores….
  • 12. ADDITONAL POINTS • Date Race condition • Debugging //example was built by Microsoft.
  • 13. NOT COVERED • Error handling • Cancellation • New .NET 4.5 async and await Features
  • 14. REFRENCES • http://www.albahari.com/threading/part5.aspx • MSDN • Plural Sight • http://www.blackwasp.co.uk/ParallelProgramming. aspx • Code Project • Samples