Dr. Dobb's Journal - February 2009 - (Page 33) d02kim_p3db 12/12/08 10:22 AM Page 33 You might reasonably wonder why a parallel capability couldn’t just automatically be added to LINQ behind the scenes. The answer is that programmer involvement is needed to handle data impurity, concurrency exceptions, thread affinity, ordering expectations, and poorer than expected performance problems in some scenarios. Adverse conditions for parallelism are referred to as “parallelism blockers” and include: • Mutable Data Structures and Impurity. Side effects due to accessing data that is not threadsafe, such as mutable structures like List or side effects due to file I/O, are not good candidates for PLINQ. For instance, copying the elements of one List to a second List using PLINQ may result in race conditions on the Add method that may only infrequently manifest. • Concurrency Exceptions. Running multiple threads may result in exceptions being thrown on several threads. Handling multiple concurrent exceptions requires some extra considerations. The way PLINQ handles concurrency exceptions is to put all of the exceptions on all threads into an AggregateException; each exception is accessible through the InnerExceptions collection. • Thread Affinity. Some libraries and technologies like COM interop and graphics packages require thread affinity. For example, Windows Forms and WPF require that code that interacts with the UI must run on the UI thread. If PLINQ were automatic, then errors would be introduced for technologies that require the single-threaded apartment model or for GUI elements in WinForms or WPF. • Ordering Expectations. PLINQ partitions data to run on multiple processors and cores. By default, this behavior does not guarantee that input ordering matches output ordering. You can elect to preserve ordering manually by invoking the AsOrdered extension method (after AsParallel), but doing so can reduce performance. • Decreased Performance. Obviously, the most desirable outcome of using multiple threads is to enhance performance. Sequential algorithms can be complex but are executed in a straightforward manner. Parallel algorithms impose extra overhead due to partitioning data and tasks, synchronization, cross-thread communication, and merging results. If the cost of this overhead reduces performance, then it is undesirable to use parallelism in that instance. Finally, optimal parallelism is affected by Amdahl’s law, which says roughly that performance speedup is limited by the amount of code that must be processed sequentially. That is, at some point after all of the code and data are partitioned, some of it must be processed sequentially, and the sequential processing that must occur and all of that synchronizing, partitioning, cross-thread communicating, and results-merging Listing One (a) using using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Diagnostics; System.Threading; namespace Primes { class Program { static void Main(string[] args) { const long upper = 1000000; SieveOfEratosthenes.BuildPrimes(upper); int[] candidates = new int[upper-2]; for (int i = 2; i Console.WriteLine(x)); Console.ReadLine(); } } public class SieveOfEratosthenes { private static List primes = new List (); public static List Primes { get { return primes; } set { primes = value; } } public static void BuildPrimes(long max) { if (max < 3) return; for (long i = 2; i <= max; i++) { if (IsPrime(i)) Primes.Add(i); } } /// /// Use the Sieve of Eratosthenes: no number is divisible /// by a number greater than its square root /// /// /// public static bool IsPrime(long v) { if (v == 2) return true; for (int i = 0; i = Math.Sqrt(v)) return true; } return true; } } } February 2009 l www.ddj.com l Dr. Dobb’s Journal 33 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.