15.01.2013 Views

Free-ebooks-library - Bahar Ali Khan

Free-ebooks-library - Bahar Ali Khan

Free-ebooks-library - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 22-4. Chunk versus range partitioning<br />

calculator might perform poorly with range partitioning. An example of when range<br />

partitioning would do well is in calculating the sum of the square roots of the first<br />

10 million integers:<br />

ParallelEnumerable.Range (1, 10000000).Sum (i => Math.Sqrt (i))<br />

ParallelEnumerable.Range returns a ParallelQuery, so you don’t need to subsequently<br />

call AsParallel.<br />

Range partitioning doesn’t necessarily allocate element ranges<br />

in contiguous blocks—it might instead choose a “striping” strategy.<br />

For instance, if there are two workers, one worker might<br />

process odd-numbered elements while the other processes<br />

even-numbered elements. The TakeWhile operator is almost certain<br />

to trigger a striping strategy to avoid unnecessarily processing<br />

elements later in the sequence.<br />

Optimizing custom aggregations<br />

PLINQ parallelizes the Sum, Average, Min, and Max operators efficiently without additional<br />

intervention. The Aggregate operator, though, presents special challenges<br />

for PLINQ. As described in Chapter 9, Aggregate performs custom aggregations. For<br />

example, the following sums a sequence of numbers, mimicking the Sum operator:<br />

PLINQ | 889<br />

Parallel<br />

Programming

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!