Information Technology Reference
In-Depth Information
PLINQ tries to create the best implementation for the queries you write in
order to generate the results you need with the least amount of work, and
in the least amount of time. Sometimes that means PLINQ queries will
execute in a different manner than you would expect. Sometimes it will
act more like LINQ to Objects, where asking for the next item in the out-
put sequence executes the code that produces it. Sometimes it will behave
more like LINQ to SQL or Entity Framework in that asking for the first
item will produce all of them. Sometimes it will behave like a mixture of
the two. You should make sure that you don't introduce any side effects in
your LINQ queries. Those will be unreliable in a PLINQ execution model.
Yo u s h o u l d c o n s t r u c t y o u r q u e r i e s w i t h s o m e c a r e t o e n s u r e t h a t y o u g e t
the most out of the underlying technology. That requires you to under-
stand how they work differently.
Parallel algorithms are limited by Amdahl's law: The speedup of a pro-
gram using multiple processors is limited by the sequential fraction of the
program. The extension methods in ParallelEnumerable are no exception
to this rule. Many of these methods can operate in parallel, but some of
them will affect the degree of parallelism due to their nature. Obviously
OrderBy and ThenBy require some coordination between tasks. Skip,
SkipWhile, Take, and TakeWhile will affect the degree of parallelism. Par-
allel tasks running on different cores may finish in different orders. You
can use the AsOrdered() and AsUnordered() methods to instruct PLINQ
as to whether or not order matters in the result sequence.
Sometimes your own algorithm relies on side effects and cannot be paral-
lelized. You can force sequential execution using the ParallelEnumerable
.AsSequential() extension method to interpret a parallel sequence as an
IEnumerable and force sequential execution.
Finally, ParallelEnumerable contains methods that allow you to control
how PLINQ executes parallel queries. You can use WithExecutionMode()
to suggest parallel execution, even if that means selecting a high overhead
algorithm. By default, PLINQ will parallelize those constructs where it
expects parallelism to help. You can use WithDegreeOfParallelism() to sug-
gest the number of threads that may be used in your algorithm. Usually,
PLINQ will allocate threads based on the number of processors on the
current machine. You can also use the WithMergeOptions() to request a
change in how PLINQ controls buffering results during a query. Usually,
PLINQ will buffer some results from each thread before making them
 
Search WWH ::




Custom Search