2. Parallel Programming
Most of computers manufacture with multi cores
Server application will handle each request in separate
threads.
Parallel programming basically for Desktop applications.
Desktop implementation
Partitioning codes into small chunks
Execute those chunks in multi threads
Collate all the results from the threads, in a thread-safe and
perform ant manner.
Its awkward to handle with threads particularly
partitioning and collating.
Parallel programming is designed with set of API’s
specifically handle these scenario.
3. Parallel Programming
Programming to leverage multicores or multiple processors is
called parallel programming. This is a subset of the broader
concept of multithreading.
Parallel programming is the general discipline of doing multiple
computations in parallel, using multiple cores, each of which is
doing some sub computation of a larger single problem.
Where as Multithreading is the approach of using multiple
threads of execution to process different operations, e.g. if you
have two things to do, use one thread to do one and another
thread to do the other.
An operating system is able to take handle thread execution in
available cores.
New .net parallel API’s take maximum advantage of available
CPU resources.
7. Task Parallel Library (TPL)
TPL provides set of API to implement parallel
processing.
TPL is to make developers more productive by
simplifying the process of adding parallelism and
concurrency to applications.
The TPL scales the degree of concurrency dynamically
to most efficiently use all the processors that are
available.
the TPL handles the partitioning of the work, the
scheduling of threads on the ThreadPool, cancellation
support and state management.
8. PLINQ (Parallel LINQ)
PLINQ offers the richest functionality: it automates all the
steps of parallelization—including partitioning the work
into tasks, executing those tasks on threads, and collating
the results into a single output sequence
PLINQ is to speed up the execution of LINQ to Objects
queries by executing the query delegates in parallel on
multi-core computers.
PLINQ queries scale in the degree of concurrency based on
the capabilities of the host computer. Syntax-
from num in source.AsParallel()
where num % 10 == 0
select num;
9. Data Parallelism
The same operation is performed concurrently on
elements in a source collection or array.
The source collection is partitioned so that multiple
threads can operate on different segments concurrently.
Parallel – class available in System.Thread.Tasks namespace
For() - Performs the parallel equivalent of a C# for loop
ForEach() - Performs the parallel equivalent of a C# foreach loop
Invoke () - method provides a convenient way to run any number
of arbitrary statements concurrently. That mean, executes an array
of Action delegates in parallel, and then waits for them to complete
10. TaskParallelism
It refers to one or more independent tasks running concurrently.
A task represents an asynchronous operation, and in some ways it resembles the creation
of a new thread or ThreadPool work item,.
Benefits of taks.
Tune a task’s scheduling
Establish a parent/child relationship when one task is started from another
Implement cooperative cancellation
Wait on a set of tasks—without a signaling construct
Attach “continuation” task(s)
Schedule a continuation based on multiple antecedent tasks
Propagate exceptions to parents, continuations, and task consumers
Dispose the task, it will dispose associated object in the task.
The Parallel class and PLINQ are internally built on the task parallelism constructs. Task
parallelism is the lowest-level approach to parallelization.
The following way to create Task, class available in System.Threading.Tasks namespace
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