Dr. Dobb's Journal - February 2009 - (Page 32) d02kim_p3db 12/12/08 10:22 AM Page 32 State of the Art by Paul Kimmel Parallel LINQ PLINQ lets you tap into extra power but with little extra work Multicore processors are a standard part of computing these days. For instance, the laptop I’m Paul is an applications architect for EDS and author of LINQ Unleashed for C#. He can be contacted at pkimmel@softconcepts.com. writing this article on has a Core 2 Duo Intel 64-bit processor and 3 GBs of RAM. That’s a lot of computing power for single-threaded code. Luckily, Parallel LINQ (PLINQ), which is part of the Parallel FX extensions for .NET, lets me use the basic LINQ keywords to tap into that extra power with little extra work on my part. The Parallel FX Library is a managed concurrency library that includes PLINQ and the Task Parallel Library (TPL). The basic use of PLINQ is to add a reference to the downloaded System.Threading.dll installed by default at C:\Program Files\Microsoft Parallel Extensions Jun08 CTP and call the IParallelEnumerable.AsParallel extension method on your collection. IParallelEnumerable inherits from IEnumerable and generally appears in a LINQ query at the end of the from range in collection clause. For instance, if the collection were an array of integers named numbers, then you would substitute collection with numbers.AsParallel(). Listing One uses the Sieve of Eratosthenes to determine if a number is a prime. A list of primes is built using the sieve technique, and a LINQ query runs through a bunch of numbers testing for primality. Listing One(a) is the sequential query and One(b) the parallel version. If this is the first time you’ve used LINQ queries, then the queries are on the lines starting with var and followed by = from p in candidates where select The LINQ queries look somewhat like inverted SQL queries. Again, the first LINQ query in Listing One is sequential and the second is parallel. The AsParallel extension method kicks off the background threads. At minimum, calling AsParallel is all you need to do to use PLINQ and multiple threads for your LINQ queries. By default, the original sequence order is not maintained when you invoke a PLINQ query. If you want the order of the sequence maintained, then call AsOrdered. However, maintaining order incurs some performance penalties. On my Dell Precision 470 workstation, the parallel version of this ran consistently slower than the sequential version. Let’s explore some reasons performance may actually degrade for parallel operations. Understanding Exclusion Scenarios for PLINQ PLINQ works with LINQ-for-Objects and LINQ-forXML. PLINQ isn’t intended for use with LINQ-toSQL or LINQ-to-Entities. Why? Because the IQueryProvider implementation for SQL Server basically translates LINQ queries to SQL queries for LINQ-toSQL and LINQ-to-Entities—queries that are processed by the SQL engine instead of in memory. CPU-bound queries that query large amounts of data, perform intensive computations, or a combination of both will yield better results than queries that are I/O bound, such as queries against the filesystem or SQL server. 32 Dr. Dobb’s Journal l www.ddj.com l February 2009 http://www.ddj.com
Table of Contents Feed for the Digital Edition of Dr. Dobb's Journal - February 2009 Dr. Dobb's Journal - February 2009 Contents Friday Night Fish Fry Alia Vox Developer Diaries Conversations Computing in the Clouds Software Development in the Cloud Videos and Oracle Forms 10g Parallel LINQ Decoupling C Header Files Effective Concurrency Disciplined Agility Swaine’s Flames Dr. Dobb's Journal - February 2009 Dr. Dobb's Journal - February 2009 - (Page BB1) Dr. Dobb's Journal - February 2009 - (Page BB2) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page Cover1) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page Cover2) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page 1) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page 2) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page 3) Dr. Dobb's Journal - February 2009 - Contents (Page 4) Dr. Dobb's Journal - February 2009 - Contents (Page 5) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 6) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 7) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 8) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 9) Dr. Dobb's Journal - February 2009 - Alia Vox (Page 10) Dr. Dobb's Journal - February 2009 - Alia Vox (Page 11) Dr. Dobb's Journal - February 2009 - Developer Diaries (Page 12) Dr. Dobb's Journal - February 2009 - Developer Diaries (Page 13) Dr. Dobb's Journal - February 2009 - Conversations (Page 14) Dr. Dobb's Journal - February 2009 - Conversations (Page 15) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 16) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 17) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 18) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 19) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 20) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 21) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 22) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 23) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 24) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 25) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 26) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 27) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 28) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 29) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 30) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 31) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 32) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 33) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 34) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 35) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 36) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 37) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 38) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 39) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 40) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 41) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 42) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 43) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 44) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 45) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 46) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 47) Dr. Dobb's Journal - February 2009 - Swaine’s Flames (Page 48) Dr. Dobb's Journal - February 2009 - Swaine’s Flames (Page Cover3) Dr. Dobb's Journal - February 2009 - Swaine’s Flames (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.