Microsoft Press - Microsoft Visual C# 2010 Step by Step.pdf

Plinq works by dividing a data set into partitions

This preview shows page 681 - 683 out of 781 pages.

PLINQ works by dividing a data set into partitions, and then using tasks to retrieve the data that matches the criteria specified by the query for each partition in parallel . The results retrieved for each partition are combined into a single enumerable result set when the tasks have completed . PLINQ is ideal for scenarios that involve data sets with large numbers of elements, or if the criteria specified for matching data involve complex, expensive operations . A primary aim of PLINQ is to be as nonintrusive as possible . If you have a lot of existing LINQ queries, you don’t want to have to modify your code to enable them to run with the latest build of the .NET Framework . To achieve this, the .NET Framework includes the extension method AsParallel that you can use with an enumerable object . The AsParallel method re- turns a ParallelQuery object that acts in a similar manner to the original enumerable object except that it provides parallel implementations of many of the LINQ operators, such as join and where These new implementations of the LINQ operators are based on the TPL and use various algorithms to try and run parts of your LINQ query in parallel wherever possible . As ever in the world of parallel computing, the AsParallel method is not magic . You cannot guarantee that your code will speed up; it all depends on the nature of your LINQ queries and whether the tasks they are performing lend themselves to parallelization . To understand how PLINQ works and the situations in which it is useful, it helps to see some examples . The exercises in the following sections demonstrate a pair of simple scenarios . Using PLINQ to Improve Performance While Iterating Through a Collection The first scenario is simple . Consider a LINQ query that iterates through a collection and retrieves elements from the collection based on a processor-intensive calculation . This form of query can benefit from parallel execution as long as the calculations are independent . The elements in the collection can be divided into a number of partitions; the exact number depends on the current load of the computer and the number of CPUs available . The ele- ments in each partition can be processed by a separate thread . When all the partitions have been processed, the results can be merged . Any collection that supports access to elements through an index, such as an array or a collection that implements the IList<T> interface, can be managed in this way .
Image of page 681

Subscribe to view the full document.

Chapter 28 Performing Parallel Data Access 651 Note If the calculations require access to shared data, you must synchronize the threads . This can impose an overhead and might negate the benefits of parallelizing the query . Parallelize a LINQ query over a simple collection 1 . Using Microsoft Visual Studio 2010, open the PLINQ solution, located in the \Microsoft Press\Visual CSharp Step By Step\Chapter 28\PLINQ folder in your Documents folder .
Image of page 682
Image of page 683
  • Fall '09

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern