Skip to main content

PLinq

Introduction

Most .NET developers today are familiar with LINQ, the technology that brought functional programming ideas into the object-oriented environment. Parallel LINQ, or ‘PLINQ’, takes LINQ to the next level by adding intuitive parallel capabilities onto an already powerful framework.
PLINQ is a query execution engine that accepts any LINQ-to-Objects or LINQ-to-XML query and automatically utilizes multiple processors or cores for execution when they are available. The change in programming model is tiny, meaning you don’t need to be a concurrency guru to use it.
Using PLINQ is almost exactly like using LINQ-to-Objects and LINQ-to-XML. You can use any of the operators available through C# 3.0 syntax or the System.Linq.Enumerable class, including OrderBy, Join, Select, Where, and so on.
LINQ-to-SQL and LINQ-to-Entities queries will still be executed by the respective databases and query providers, so PLINQ does not offer a way to parallelize those queries. If you wish to process the results of those queries in memory, including joining the output of many heterogeneous queries, then PLINQ can be quite useful.
Using AsParallel method :
The AsParallel method is the doorway to PLINQ. It converts data sequence into a ParallelQuery. The LINQ engine detects the use of a ParallelQuery as the source in a query and switches to PLINQ execution automatically. You are likely to use the AsParallel method every time you use PLINQ.
Sample code 1 : Sequential LINQ execution
01var customers = new[] {
02    new Customer { ID = 1,  FirstName = "Sandeep"  , LastName = "Ramani" },
03    new Customer { ID = 2,  FirstName = "Dharmik"  , LastName = "Chotaliya" },
04    new Customer { ID = 3,  FirstName = "Nisar"    ,  LastName = "Kalia" } ,
05    new Customer { ID = 4,  FirstName = "Ravi"     , LastName = "Mapara" } ,
06    new Customer { ID = 5,  FirstName = "Hardik"   , LastName = "Mistry" }
07    new Customer { ID = 6,  FirstName = "Sandy"    , LastName = "Ramani" },
08    new Customer { ID = 7,  FirstName = "Jigar"    , LastName = "Shah" },
09    new Customer { ID = 8,  FirstName = "Kaushal"  , LastName = "Parik" } ,
10    new Customer { ID = 9,  FirstName = "Abhishek" , LastName = "Swarnker" } ,
11    new Customer { ID = 10, FirstName = "Sanket"   , LastName = "Patel" }
12    new Customer { ID = 11, FirstName = "Dinesh"   , LastName = "Prajapati" },
13    new Customer { ID = 12, FirstName = "Jayesh"   , LastName = "Patel" },
14    new Customer { ID = 13, FirstName = "Nimesh"   , LastName = "Mishra" } ,
15    new Customer { ID = 14, FirstName = "Shiva"    , LastName = "Reddy" } ,
16    new Customer { ID = 15, FirstName = "Jasmin"   , LastName = "Malviya" }
17    new Customer { ID = 16, FirstName = "Haresh"   , LastName = "Bhanderi" },
18    new Customer { ID = 17, FirstName = "Ankit"    , LastName = "Ramani" },
19    new Customer { ID = 18, FirstName = "Sanket"   , LastName = "Shah" } ,
20    new Customer { ID = 19, FirstName = "Amit"     , LastName = "Shah" } ,
21    new Customer { ID = 20, FirstName = "Nilesh"   , LastName = "Soni" }       };
22 
23var results = from c in customers
24          where c.FirstName.StartsWith("San")
25          select c;
Sample code 2 : Parallel LINQ execution
01var customers = new[] {
02    new Customer { ID = 1,  FirstName = "Sandeep"  , LastName = "Ramani" },
03    new Customer { ID = 2,  FirstName = "Dharmik"  , LastName = "Chotaliya" },
04    new Customer { ID = 3,  FirstName = "Nisar"    ,  LastName = "Kalia" } ,
05    new Customer { ID = 4,  FirstName = "Ravi"     , LastName = "Mapara" } ,
06    new Customer { ID = 5,  FirstName = "Hardik"   , LastName = "Mistry" }
07    new Customer { ID = 6,  FirstName = "Sandy"    , LastName = "Ramani" },
08    new Customer { ID = 7,  FirstName = "Jigar"    , LastName = "Shah" },
09    new Customer { ID = 8,  FirstName = "Kaushal"  , LastName = "Parik" } ,
10    new Customer { ID = 9,  FirstName = "Abhishek" , LastName = "Swarnker" } ,
11    new Customer { ID = 10, FirstName = "Sanket"   , LastName = "Patel" }
12    new Customer { ID = 11, FirstName = "Dinesh"   , LastName = "Prajapati" },
13    new Customer { ID = 12, FirstName = "Jayesh"   , LastName = "Patel" },
14    new Customer { ID = 13, FirstName = "Nimesh"   , LastName = "Mishra" } ,
15    new Customer { ID = 14, FirstName = "Shiva"    , LastName = "Reddy" } ,
16    new Customer { ID = 15, FirstName = "Jasmin"   , LastName = "Malviya" }
17    new Customer { ID = 16, FirstName = "Haresh"   , LastName = "Bhanderi" },
18    new Customer { ID = 17, FirstName = "Ankit"    , LastName = "Ramani" },
19    new Customer { ID = 18, FirstName = "Sanket"   , LastName = "Shah" } ,
20    new Customer { ID = 19, FirstName = "Amit"     , LastName = "Shah" } ,
21    new Customer { ID = 20, FirstName = "Nilesh"   , LastName = "Soni" }       };
22 
23var results = from c in customers.AsParallel()
24          where c.FirstName.StartsWith("San")
25          select c;
With the simple addition of the AsParallel() extension method, the .NET runtime will automatically parallelize the operation across multiple cores. In fact, PLINQ will take full responsibility for partitioning your data into multiple chunks that can be processed in parallel.
PLINQ partitioning is out of the scope for this article, but if you’re curious about the inner workings of it, this blog post from Microsoft’s own Parallel Programming team does a great job of explaining the details.
When you will run the above sample queries , you might get same output but possibly in different order. Here Sample code 1 is an example of Sequential LINQ execution, while Sample code 2 is an example of Parallel LINQ execution.

Limitations 

  1. PLINQ only works against local collections. This means that if you’re using LINQ providers over remote data, such as LINQ to SQL or ADO.NET Entity Framework, then you’re out of luck for this version.
  2. Since PLINQ chunks the collection into multiple partitions and executes them in parallel, the results that you would get from a PLINQ query may not be in the same order as the results that you would get from a serially executed LINQ query.
However, you can work around this by introducing the AsOrdered() method into your query, which will force a specific ordering into your results. Keep in mind, however, that the AsOrdered() method does incur a performance hit for large collections, which can erase many of the performance gains of parallelizing your query in the first place.
Sample code 3 : Preserving the Order of PLINQ Query Results Using the AsOrdered Method
1var results = from c in customers.AsParallel().AsOrdered()
2          where c.FirstName.StartsWith("San")
3          select c;

Controlling Parallelism

1) Forcing Parallel Execution :
In some cases, PLINQ may decide that your query is better dealt with sequentially. You can control this by using the WithExecutionMode extension method, which is applied to the ParallelQuery type. The WithExecutionMode method takes a value from the ParallelExecutionMode enumeration. There are two such values: the default (let PLINQ decide what to do) and ForceParallelism (use PLINQ even if the overhead of parallel execution is likely to outweigh the benefits).
Here is sample code which demonstrates the use of this method :
1var results = from c in customers.AsParallel().WithExecutionMode(ParallelExecutionMode.ForceParallelism)
2          where c.FirstName.StartsWith("San")
3          select c;
2) Limiting the Degree of Parallelism :
You can request that PLINQ limit the number of partitions that are processed simultaneously using the WithDegreeofParallelism extension method, which operates on the ParallelQuery type. This method takes an int argument that states the maximum number of partitions that should be processed at once; this is known as the degree of parallelism. Setting the degree of parallelism doesn’t force PLINQ to use that many. It just sets an upper limit. PLINQ may decide to use fewer than you have specified or, if you have not used the WithExecutionMode method, may decide to execute the query sequentially.
Here is sample code which demonstrates the use of this method :
1var results = from c in customers.AsParallel().WithDegreeOfParallelism(2)
2          where c.FirstName.StartsWith("San")
3          select c;
3) Generating and Using a Parallel Sequence :
1IEnumerable<int> evens
2    = ((ParallelQuery<int>) ParallelEnumerable.Range(0, 50000))
3        .Where(i => i % 2 == 0)
4        .Select(i => i);
Above code uses the Range method to create a sequence of 50,000 integers starting with the zero. The first argument to the method is the start index; the second is the number of values you require. Notice that we have cast the result from the Range method to a ParallelQuery. If we don’t do this, LINQ doesn’t recognize the sequence as supporting parallel execution and will execute the query sequentially.
4) Generating and Using a Repeating Sequence :
1int sum = ParallelEnumerable.Repeat(1, 50000)
2        .Select(i => i)
3        .Sum();
The static Repeat method takes an object and a count and creates a sequence where the object is repeated the specified number of times.

Comments

Popular posts from this blog

Top 10 ASP.NET Web API Interview Questions

What is ASP.NET Web API? ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework. Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc. with many other advantages including: Create resource-oriented services using the full features of HTTP Exposing services to a variety of clients easily like browsers or mobile devices, etc. What are the Advantages of Using ASP.NET Web API? Using ASP.NET Web API has a number of advantages, but core of the advantages are: It works the HTTP way using standard HTTP verbs like  GET ,  POST ,  PUT ,  DELETE , etc. for all CRUD operations Complete support for routing Response generated in JSON or XML format using  MediaTypeFormatter It has the ability to be hosted in IIS as well as self-host outside of IIS Supports Model binding and Validation Support for OD...

Extension methods in C#

Consider the class C# 1 2 3 4 5 6 7 8 9 10 11 12 13          namespace ExtensionMethod      {          public class testClass {              public string sayHello ( ) {              return "Hello" ;            }        }      }     Invoke the above from your form using C# 1 2 3 4 5 6          testClass test = new testClass ( ) ;      MessageBox . Show ( test . sayHello ( ) ) ;     This will show “Hello” in message box. Consider the scenario where you don...

What is cookie? Advantages and disadvantages of cookies?

What is cookie? A cookie is a small piece of text file stored on user's computer in the form of name-value pair. Cookies are used by websites to keep track of visitors e.g. to keep user information like username etc. If any web application using cookies, Server send cookies and client browser will store it. The browser then returns the cookie to the server at the next time the page is requested. The most common example of using a cookie is to store User information, User preferences, Password Remember Option etc.It is also one of the common and mostly asked interview questions. Some facts about Cookie Here are a few facts to know about cookies: · Cookies are domain specific i.e. a domain cannot read or write to a cookie created by another domain. This is done by the browser for security purpose. · Cookies are browser specific. Each browser stores the cookies in a different location. The cookies are browser specific and so a cookie created in one browser(e.g in Google Chrome...