Thursday, 20 March 2014

Parallel Computing in .NET Framework 4.0 - Part 3

PLINQ (Parallel - LINQ)

LINQ is Microsoft's new baby (new technology), introduced with ASP.NET 3.5. The LINQ stand for (L) Language-(IN) Integrated (Q) Query or LINQ (pronounced "link"). LINQ is designed to fill the gap between .NET languages and query languages such as SQL, with syntax specifically designed for query operations. With the introduction of LINQ into .NET, query becomes a first class concept in .NET, whether object, XML, or data queries. 

Microsoft has given a native syntax to developers in the form LINQ to C# and VB.Net for accessing data from any repository. The repository could be in memory object, database (still MS SQL Server only) and XML files.
The parallel LINQ approach is declarative rather than imperative in the same way as the sequential version of LINQ. This approach to parallelism is of a higher level that that provided by the TPL. It allows the use of the standard query operators, which you should be familiar with, whilst automatically assigning work to be carried out simultaneously by the available processors.
To get complete detail visit the MSDN link:
http://msdn.microsoft.com/en-us/library/dd460688(v=vs.100).aspx
The nature of many queries means that they can be easily dividing the task to take the advantage of Parallel approach. Most queries perform the same group of actions for each item in a collection. If all of those actions are independent, with no side effects caused due to the order in which they appear, you can often achieve a large performance increase by dividing the work between several processor cores. To support these scenarios, the .NET framework version 4.0 introduced Parallel LINQ (PLINQ).
PLINQ provides the same standard query operators and query expression syntax as LINQ. The key difference is that the source data can be broken into several groups using data decomposition.
LINQ works with sequences that implement the IEnumerable<T> interface. To signify that we wish to use PLINQ, we must ensure that the source sequence supports parallelism. To do so, we can use the static AsParallel method of the ParallelEnumerable class. This is an extension method of IEnumerable<T>, so can be applied to any sequence that supports LINQ operations. It returns an object of the type ParallelQuery<T>.
Handling Exceptions with PLINQ: When you execute a sequential query using LINQ, any one of the data items processed may lead to an exception. When an exception is thrown, the query stops executing immediately. With PLINQ, it is possible for the processing of multiple items to be executing concurrently. If one of these throws an exception, all of the other threads will stop but only after any scheduled operations have completed. 
This may mean that there is a delay between the exceptional event and the PLINQ query halting if the query operations are slow. It also means that any of the other parallel operations may also throw an exception.
To deal with the possibility of a query causing multiple exceptions, all exceptions from a PLINQ query are combined in a single AggregateException, which is thrown when all of the threads of execution stop. As when using parallel loops and tasks, you can capture this and examine its InnerExceptions property to find all of the thrown exceptions.

No comments:

Post a Comment