What is Plinq C# and what is it?

Parallel LINQ is a parallel implementation of the language-integrated query pattern.The full set of LINQ standard query operators are implemented as extension methods by PLINQ.The power of parallel programming is combined with the simplicity of LINQ.

If you're not familiar with LINQ, it has a unified model for querying any data source in a type-safe manner.The name for queries that are run against in-memory collections is linq to objects.It is assumed that you have a basic understanding of LINQ.See Language-Integrated Query for more information.

A PLINQ query is similar to a non-parallel query.PLINQ queries operate on any in-memory data source and have deferred execution, which means they do not begin executing until the query is enumerated.PLINQ tries to use all the processors on the system.Partitioning the data source into segments and then executing the query on each segment on separate worker threads on multiple processors is what it does.Parallel execution can mean that the query runs faster.

By adding the AsParallel query operation to the data source, PLINQ can achieve significant performance improvements over legacy code for certain kinds of queries.Not all query operations run faster in PLINQ because of parallelism.Parallelization slows down some queries.You should know how issues such as ordering affect parallel queries.Understanding Speedup can be found in PLINQ.

The documentation uses lambda expressions to define delegates.If you don't know what a lambda expression is, you can see it in PLINQ and TPL.

An overview of the main PLINQ classes is included in the remainder of this article.There are links to more detailed information in each section.

Almost all of PLINQ's function is exposed by the System.Linq.ParallelEnumerable class.The System.Core.dll assembly contains it and the rest.Both the assembly and the namespace are references in the default C# and Visual Basic projects.

ParallelEnumerable does not attempt to parallelize each query operator.If you don't know anything about LINQ, you can see the introduction to C# and visual basic.

The ParallelEnumerable class has methods that enable behaviors specific to parallel execution.The following table shows the PLINQ-specific methods.

The following example shows how to invoke the ParallelEnumerable.AsParallel extension method on the data source.

The System.Linq.ParallelEnumerable implementations are bound by the AsParallel extension method.

PLINQ is conservative.The overall structure of the query is analyzed at run time by the PLINQ infrastructure.PLINQ divides the source sequence into tasks that can be run concurrently if the query yields speedups.PLINQ just runs the query sequential if it is not safe to parallelize it.If PLINQ has a choice between a parallel or sequential algorithm, it chooses the sequential one.The WithExecutionMode method can be used to instruct PLINQ to use the parallel algorithm.When you know by testing and measurement that a query executes faster in parallel, this is useful.You can see how to specify the execution mode in PLINQ.

All of the processors on the host computer are used by PLINQ.The WithDegreeOfParallelism method can be used to instruct PLINQ to use no more than a specified number of processors.When you want to make sure that other processes on the computer receive a certain amount of time, this is useful.The query can only be used with a maximum of two processors.

It might be beneficial to specify a degree of parallelism greater than the number of cores on the machine in cases where a query is performing a significant amount of non-compute-bound work.

The ordering of the source sequence must be preserved in some queries.The operator for this purpose is provided by PLINQ.AsSequential is different from AsOrdered.The results of an AsOrdered sequence are sorted and buffered.An AsOrdered sequence might be processed more slowly than the default AsUnordered sequence.There are many factors that affect whether a particular ordered parallel operation is faster than a sequential version.

The source data should be delivered in a sequential manner.When it's necessary, the ParallelEnumerable query operators return to sequential mode.The AsSequential method is used for user defined query operators and user delegates.When you use AsSequential, all operators in the query are executed at the same time.You can see how to combine parallel and sequential linq queries.

When a PLINQ query executes in parallel, its results from each worker thread must be merged back onto the main thread for consumption by a foreach loop, or into a list or array.To begin producing results more quickly, it might be beneficial to specify a particular kind of merge operation.PLINQ supports the WithMergeOptions method.See the options in PLINQ for more information.

Execution is deferred until the query is enumerated in a foreach loop or by using a method such as ToList, ToArray, or ToDictionary.You can use foreach to execute the query in PLINQ.Since foreach does not run in parallel, the output from all parallel tasks have to be merged back into the thread on which the loop is running.You can use foreach whenever you need to preserve the final ordering of the query results and also when you are processing the results in a serial manner.When order preservation is not required, use the ForAll method to execute a PLINQ query.The final merge step is not performed by ForAll.The ForAll method is shown in the following code example.This is a place where multiple threads are added concurrently without attempting to remove any items.

The illustration shows the difference between ForAll and foreach.

The cancellation types in.NET are integrated with PLINQ.See Cancellation in Managed Threads for more information.PLINQ queries can be canceled.The WithCancellation operator can be used to create a cancelable PLINQ query.PLINQ will stop processing on all threads when the IsCancellationRequested property on the token is true.

A PLINQ query might continue to process elements after the cancellation token is set.

You can respond to cancellation requests in long-running user delegates.How to: Cancel a PLINQ query can be found here.

Multiple exceptions might be thrown from different threads at the same time.The code that threw the exception might be on a different thread.The AggregateException type is used by PLINQ to marshal the exceptions back to the calling thread.Only one try-catch block is required on the calling thread.You can catch any exceptions encapsulated in the AggregateException that you can safely recover from.Some exceptions may be thrown that are not wrapped in an AggregateException.

It is possible that a query may continue to process items after the exception is raised if exceptions are allowed to bubble up back to the joining thread.

Writing a custom partitioner that takes advantage of some characteristic of the source data can improve query performance.The object that is queried is the custom partitioner.

There is a fixed number of partitions that PLINQ supports.Dynamic partitioning means that the number of partition changes at run time.Partitioners for PLINQ and TPL are available.

The overhead of parallelizing a query outweighs the performance benefit.If a query does not perform much computation or if the data source is small, a PLINQ query may be slower than a sequential LINQ to Objects query.You can use the Parallel Performance Analyzer to compare the performance of various queries, to locate processing bottlenecks, and to determine whether your query is running in parallel or sequential.There are two ways to measure PLINQ query performance.

Related Posts:

  1. What is query language with example?
  2. Where is the power query editor?
  3. What type copper are fittings?
  4. How does a sequential gearbox work?