SlideShare a Scribd company logo
1 of 14
Introduction
Multicore processors have been around for many years. However,
many developers are doing what they’ve always done: creating
single-threaded programs. They’re not taking advantage of all the
extra processing power.
Example: Imagine you have many tasks to perform and many
people to perform them, but you are using only one person
because you don’t know how to ask for more. It’s inefficient. Users
are paying for extra power, but their software is not allowing them
to use it.
Free lunch is over
• Don’t expect your sequential program to run faster on new
processors.
• Today’s desktops typically have 4 cores. Latest Intel multi-core chip
has 48 cores. Expect 100s of cores in the near future.
• To take advantage of the hardware of today and tomorrow, we have
to parallelize our code to distribute work across multiple processor
cores.
• The .NET Framework provides several ways to use multiple threads of
execution to keep application responsive to our user while
maximizing the performance.
Parallel Programming in the .NET Framework
• In the past, parallelization required low-level manipulation of threads,
thread pools and locks.
• Visual Studio 2010 and the .NET Framework 4.0:
• new runtime
• new class library types
• new diagnostic tools
• Features simplify parallel development to write efficient, fine-grained, and
scalable parallel code.
Parallel programming architecture.NET 4:
High-level overview
Task Parallel Library (TPL)
• Is a set of public types and APIs in
the System.Threading and System.Threading.Tasks namespaces.
• The TPL scales the degree of concurrency dynamically to most efficiently
use all the processors that are available.
• TPL handles the partitioning of the work, the scheduling of threads on the
threadpool, cancellation support, state management, and other low-level
details.
• To remember:
• Not all code is suitable for parallelization; e.g. for small/cheap loops, overhead of
parallelization can cause the code to run more slowly.
• Parallelization adds complexity to program execution.
• Need understanding of threading concepts: locks, deadlocks, and race conditions, to
use TPL effectively.
Data Parallelism (TPL)
• Data parallelism refers to scenarios in which the same operation is
performed concurrently (that is, in parallel) on elements in a source
collection or array.
• Through System.Threading.Tasks.Parallel class.
• Imperative way:
• Describes how to create parallel for and foreach loops.
• When to use: Strategy can work well if you have either of these
• Lots of items
• Lots of work for each item
• DEMO
Data Parallelism...
• Declarative way: PLINQ (Parallel LINQ)
• PLINQ)is a parallel implementation of the LINQ pattern
• Partition the data source into segments, and then executing the
query on each segment on separate worker threads in parallel
on multiple cores.
• PLINQ can achieve significant performance improvements over
legacy code for many cases.
• However, parallelization complexity can actually slows down
certain queries, if not used wisely.
Task Parallelism (TPL)
• Based on the concept of a task, which represents an asynchronous
operation.
• A task resembles a thread or ThreadPool work item, but at a higher
level of abstraction.
• The term task parallelism refers to one or more independent tasks
running concurrently.
Task benefits…
• More programmatic control than is possible with a thread: waiting,
cancellation, continuations, robust exception handling, detailed
status, custom scheduling, and more.
• More efficient and scalable use of system resources: tasks are
queued to the ThreadPool, enhanced with algorithms that determine
and adjust to the number of threads and that provide load balancing
to maximize throughput. This makes tasks relatively lightweight.
• For above reasons: TPL is the preferred API for writing multi-
threaded, asynchronous, and parallel code.
Creating and Running Tasks
• Implicit:
• Parallel.Invoke method to run any number tasks concurrently.
• Action delegate is passed for each item of work (task).
• Syntax: Parallel.Invoke(() => DoSomeWork(), () =>
DoSomeOtherWork());
• Demo ( parallelizes the operations, not the data)
• Explicit:
• Create Task or Task<TResult> with user delegate (named delegate,
anonymous method, or lambda expression) that encapsulates the code task
will execute.
• Execute with task1.Start() with other functions (task1.Wait() etc)
• Task.Run() methods to create and start a task in one operation
• Preferred way when more control over the creation and scheduling of the task is not
needed.
Tasks...
• The tasks run asynchronously and may complete in any order. If
the Result property is accessed before the computation finishes,
the property blocks the calling thread until the value is available.
Options
• C# provides several mechanisms for parallel programming:
• Explicit threads:
• with synchronization via locks, critical regions etc.
• The user gets full control over the parallel code.
• BUT orchestrating the parallel threads is tricky and error prone (race conditions,
deadlocks etc)
• This technique requires a shared-memory model
• Explicit threads with a message-passing library:
• Threads communicate by explicitly sending messages, with data required/produced,
between workstations. Parallel code can run on a distributed-memory architecture,
eg. a network of workstations. The programmer has to write code for (un-)serializing
the data that is sent between machines. BUT threads are still explicit, and the
difficulties in orchestrating the threads are the same. A common configuration is
C+MPI.
Wait a minute... Quantum Computation on a
way!
• Calls modern day computing classical one.
• Uses quantum superposition principle on superconductors.
• Can do:
• Integer prime factorization (security of public key cryptographic systems)
• Quadratic speedup over NP-complete problems (Viz. TSP, Subset sum problem
etc.)
• Much more...
• Example: D-wave 2x system
Resources
• https://github.com/bsonnino/ParallelProgramming
• QC: Dwave, ScienceDaily, Wikipedia

More Related Content

What's hot

Netflix machine learning
Netflix machine learningNetflix machine learning
Netflix machine learningAmer Ather
 
More Data Science with Less Engineering: Machine Learning Infrastructure at N...
More Data Science with Less Engineering: Machine Learning Infrastructure at N...More Data Science with Less Engineering: Machine Learning Infrastructure at N...
More Data Science with Less Engineering: Machine Learning Infrastructure at N...Ville Tuulos
 
Parallel Programming in .NET
Parallel Programming in .NETParallel Programming in .NET
Parallel Programming in .NETSANKARSAN BOSE
 
running Tensorflow in Production
running Tensorflow in Productionrunning Tensorflow in Production
running Tensorflow in ProductionMatthias Feys
 
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLAsync and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLArie Leeuwesteijn
 
Julia language: inside the corporation
Julia language: inside the corporationJulia language: inside the corporation
Julia language: inside the corporationAndre Pemmelaar
 
Neural_Programmer_Interpreter
Neural_Programmer_InterpreterNeural_Programmer_Interpreter
Neural_Programmer_InterpreterKaty Lee
 
Concurrent/ parallel programming
Concurrent/ parallel programmingConcurrent/ parallel programming
Concurrent/ parallel programmingTausun Akhtary
 
Making neural programming architectures generalize via recursion
Making neural programming architectures generalize via recursionMaking neural programming architectures generalize via recursion
Making neural programming architectures generalize via recursionKaty Lee
 
Parallel Programming Primer
Parallel Programming PrimerParallel Programming Primer
Parallel Programming PrimerSri Prasanna
 
Ch22 parallel d_bs_cs561
Ch22 parallel d_bs_cs561Ch22 parallel d_bs_cs561
Ch22 parallel d_bs_cs561Shobhit Saxena
 
A Neural Network that Understands Handwriting
A Neural Network that Understands HandwritingA Neural Network that Understands Handwriting
A Neural Network that Understands HandwritingShivam Sawhney
 
overloading
overloadingoverloading
overloadingcpsivaku
 

What's hot (20)

Netflix machine learning
Netflix machine learningNetflix machine learning
Netflix machine learning
 
OpenMP
OpenMPOpenMP
OpenMP
 
More Data Science with Less Engineering: Machine Learning Infrastructure at N...
More Data Science with Less Engineering: Machine Learning Infrastructure at N...More Data Science with Less Engineering: Machine Learning Infrastructure at N...
More Data Science with Less Engineering: Machine Learning Infrastructure at N...
 
Parallel Programming in .NET
Parallel Programming in .NETParallel Programming in .NET
Parallel Programming in .NET
 
Apache Spark
Apache SparkApache Spark
Apache Spark
 
running Tensorflow in Production
running Tensorflow in Productionrunning Tensorflow in Production
running Tensorflow in Production
 
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLAsync and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NL
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
 
Julia language: inside the corporation
Julia language: inside the corporationJulia language: inside the corporation
Julia language: inside the corporation
 
Neural_Programmer_Interpreter
Neural_Programmer_InterpreterNeural_Programmer_Interpreter
Neural_Programmer_Interpreter
 
LINQ/PLINQ
LINQ/PLINQLINQ/PLINQ
LINQ/PLINQ
 
Concurrent/ parallel programming
Concurrent/ parallel programmingConcurrent/ parallel programming
Concurrent/ parallel programming
 
Openmp
OpenmpOpenmp
Openmp
 
Making neural programming architectures generalize via recursion
Making neural programming architectures generalize via recursionMaking neural programming architectures generalize via recursion
Making neural programming architectures generalize via recursion
 
Apache Hama 0.4
Apache Hama 0.4Apache Hama 0.4
Apache Hama 0.4
 
Parallel Programming Primer
Parallel Programming PrimerParallel Programming Primer
Parallel Programming Primer
 
Ch22 parallel d_bs_cs561
Ch22 parallel d_bs_cs561Ch22 parallel d_bs_cs561
Ch22 parallel d_bs_cs561
 
A Neural Network that Understands Handwriting
A Neural Network that Understands HandwritingA Neural Network that Understands Handwriting
A Neural Network that Understands Handwriting
 
Nbvtalkataitamimageprocessingconf
NbvtalkataitamimageprocessingconfNbvtalkataitamimageprocessingconf
Nbvtalkataitamimageprocessingconf
 
overloading
overloadingoverloading
overloading
 

Similar to Coding For Cores - C# Way

Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency ProgrammingConcurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency ProgrammingSachintha Gunasena
 
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
 
Multicore_Architecture Book.pdf
Multicore_Architecture Book.pdfMulticore_Architecture Book.pdf
Multicore_Architecture Book.pdfSwatantraPrakash5
 
Data Parallel and Object Oriented Model
Data Parallel and Object Oriented ModelData Parallel and Object Oriented Model
Data Parallel and Object Oriented ModelNikhil Sharma
 
Parallel Computing-Part-1.pptx
Parallel Computing-Part-1.pptxParallel Computing-Part-1.pptx
Parallel Computing-Part-1.pptxkrnaween
 
Multithreaded Programming Part- I.pdf
Multithreaded Programming Part- I.pdfMultithreaded Programming Part- I.pdf
Multithreaded Programming Part- I.pdfHarika Pudugosula
 
Lec 2 (parallel design and programming)
Lec 2 (parallel design and programming)Lec 2 (parallel design and programming)
Lec 2 (parallel design and programming)Sudarshan Mondal
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with EpsilonSina Madani
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threadsVaibhav Khanna
 
Distributed Tensorflow with Kubernetes - data2day - Jakob Karalus
Distributed Tensorflow with Kubernetes - data2day - Jakob KaralusDistributed Tensorflow with Kubernetes - data2day - Jakob Karalus
Distributed Tensorflow with Kubernetes - data2day - Jakob KaralusJakob Karalus
 
Week # 1.pdf
Week # 1.pdfWeek # 1.pdf
Week # 1.pdfgiddy5
 
Parallel Algorithms Advantages and Disadvantages
Parallel Algorithms Advantages and DisadvantagesParallel Algorithms Advantages and Disadvantages
Parallel Algorithms Advantages and DisadvantagesMurtadha Alsabbagh
 
12. Parallel Algorithms.pptx
12. Parallel Algorithms.pptx12. Parallel Algorithms.pptx
12. Parallel Algorithms.pptxMohAlyasin1
 

Similar to Coding For Cores - C# Way (20)

Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency ProgrammingConcurrency Programming in Java - 01 - Introduction to Concurrency Programming
Concurrency Programming in Java - 01 - Introduction to Concurrency Programming
 
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
 
Multicore_Architecture Book.pdf
Multicore_Architecture Book.pdfMulticore_Architecture Book.pdf
Multicore_Architecture Book.pdf
 
Data Parallel and Object Oriented Model
Data Parallel and Object Oriented ModelData Parallel and Object Oriented Model
Data Parallel and Object Oriented Model
 
Cc module 3.pptx
Cc module 3.pptxCc module 3.pptx
Cc module 3.pptx
 
Parallel Computing-Part-1.pptx
Parallel Computing-Part-1.pptxParallel Computing-Part-1.pptx
Parallel Computing-Part-1.pptx
 
Multithreaded Programming Part- I.pdf
Multithreaded Programming Part- I.pdfMultithreaded Programming Part- I.pdf
Multithreaded Programming Part- I.pdf
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
 
Lec 2 (parallel design and programming)
Lec 2 (parallel design and programming)Lec 2 (parallel design and programming)
Lec 2 (parallel design and programming)
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with Epsilon
 
Lecture1
Lecture1Lecture1
Lecture1
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threads
 
Async programming in c#
Async programming in c#Async programming in c#
Async programming in c#
 
Distributed Tensorflow with Kubernetes - data2day - Jakob Karalus
Distributed Tensorflow with Kubernetes - data2day - Jakob KaralusDistributed Tensorflow with Kubernetes - data2day - Jakob Karalus
Distributed Tensorflow with Kubernetes - data2day - Jakob Karalus
 
Pthread
PthreadPthread
Pthread
 
Week # 1.pdf
Week # 1.pdfWeek # 1.pdf
Week # 1.pdf
 
Deep Learning at Scale
Deep Learning at ScaleDeep Learning at Scale
Deep Learning at Scale
 
Parallel Algorithms Advantages and Disadvantages
Parallel Algorithms Advantages and DisadvantagesParallel Algorithms Advantages and Disadvantages
Parallel Algorithms Advantages and Disadvantages
 
12. Parallel Algorithms.pptx
12. Parallel Algorithms.pptx12. Parallel Algorithms.pptx
12. Parallel Algorithms.pptx
 
Concept of thread
Concept of threadConcept of thread
Concept of thread
 

Recently uploaded

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
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 

Recently uploaded (20)

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
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
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...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Coding For Cores - C# Way

  • 1. Introduction Multicore processors have been around for many years. However, many developers are doing what they’ve always done: creating single-threaded programs. They’re not taking advantage of all the extra processing power. Example: Imagine you have many tasks to perform and many people to perform them, but you are using only one person because you don’t know how to ask for more. It’s inefficient. Users are paying for extra power, but their software is not allowing them to use it.
  • 2. Free lunch is over • Don’t expect your sequential program to run faster on new processors. • Today’s desktops typically have 4 cores. Latest Intel multi-core chip has 48 cores. Expect 100s of cores in the near future. • To take advantage of the hardware of today and tomorrow, we have to parallelize our code to distribute work across multiple processor cores. • The .NET Framework provides several ways to use multiple threads of execution to keep application responsive to our user while maximizing the performance.
  • 3. Parallel Programming in the .NET Framework • In the past, parallelization required low-level manipulation of threads, thread pools and locks. • Visual Studio 2010 and the .NET Framework 4.0: • new runtime • new class library types • new diagnostic tools • Features simplify parallel development to write efficient, fine-grained, and scalable parallel code.
  • 4. Parallel programming architecture.NET 4: High-level overview
  • 5. Task Parallel Library (TPL) • Is a set of public types and APIs in the System.Threading and System.Threading.Tasks namespaces. • The TPL scales the degree of concurrency dynamically to most efficiently use all the processors that are available. • TPL handles the partitioning of the work, the scheduling of threads on the threadpool, cancellation support, state management, and other low-level details. • To remember: • Not all code is suitable for parallelization; e.g. for small/cheap loops, overhead of parallelization can cause the code to run more slowly. • Parallelization adds complexity to program execution. • Need understanding of threading concepts: locks, deadlocks, and race conditions, to use TPL effectively.
  • 6. Data Parallelism (TPL) • Data parallelism refers to scenarios in which the same operation is performed concurrently (that is, in parallel) on elements in a source collection or array. • Through System.Threading.Tasks.Parallel class. • Imperative way: • Describes how to create parallel for and foreach loops. • When to use: Strategy can work well if you have either of these • Lots of items • Lots of work for each item • DEMO
  • 7. Data Parallelism... • Declarative way: PLINQ (Parallel LINQ) • PLINQ)is a parallel implementation of the LINQ pattern • Partition the data source into segments, and then executing the query on each segment on separate worker threads in parallel on multiple cores. • PLINQ can achieve significant performance improvements over legacy code for many cases. • However, parallelization complexity can actually slows down certain queries, if not used wisely.
  • 8. Task Parallelism (TPL) • Based on the concept of a task, which represents an asynchronous operation. • A task resembles a thread or ThreadPool work item, but at a higher level of abstraction. • The term task parallelism refers to one or more independent tasks running concurrently.
  • 9. Task benefits… • More programmatic control than is possible with a thread: waiting, cancellation, continuations, robust exception handling, detailed status, custom scheduling, and more. • More efficient and scalable use of system resources: tasks are queued to the ThreadPool, enhanced with algorithms that determine and adjust to the number of threads and that provide load balancing to maximize throughput. This makes tasks relatively lightweight. • For above reasons: TPL is the preferred API for writing multi- threaded, asynchronous, and parallel code.
  • 10. Creating and Running Tasks • Implicit: • Parallel.Invoke method to run any number tasks concurrently. • Action delegate is passed for each item of work (task). • Syntax: Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork()); • Demo ( parallelizes the operations, not the data) • Explicit: • Create Task or Task<TResult> with user delegate (named delegate, anonymous method, or lambda expression) that encapsulates the code task will execute. • Execute with task1.Start() with other functions (task1.Wait() etc) • Task.Run() methods to create and start a task in one operation • Preferred way when more control over the creation and scheduling of the task is not needed.
  • 11. Tasks... • The tasks run asynchronously and may complete in any order. If the Result property is accessed before the computation finishes, the property blocks the calling thread until the value is available.
  • 12. Options • C# provides several mechanisms for parallel programming: • Explicit threads: • with synchronization via locks, critical regions etc. • The user gets full control over the parallel code. • BUT orchestrating the parallel threads is tricky and error prone (race conditions, deadlocks etc) • This technique requires a shared-memory model • Explicit threads with a message-passing library: • Threads communicate by explicitly sending messages, with data required/produced, between workstations. Parallel code can run on a distributed-memory architecture, eg. a network of workstations. The programmer has to write code for (un-)serializing the data that is sent between machines. BUT threads are still explicit, and the difficulties in orchestrating the threads are the same. A common configuration is C+MPI.
  • 13. Wait a minute... Quantum Computation on a way! • Calls modern day computing classical one. • Uses quantum superposition principle on superconductors. • Can do: • Integer prime factorization (security of public key cryptographic systems) • Quadratic speedup over NP-complete problems (Viz. TSP, Subset sum problem etc.) • Much more... • Example: D-wave 2x system