Talk at CodeCamp Seattle 27 Jan 2008 by Igor ...
-
PFX Team Blog- More processors in CPU's means a lot of opportunity for parrelism coding techniques; However, this is currently "hard".
- CTP released in December 2007.
- AIM: Simplify parallel programming for .NET.
Simple example:
IEnumerable<int> query =
from x in array.AsParallel()
where x > 5
orderby x
select x;
- AsParallel extension method hookes the IQuery engine for PLINQ to the query calls. PLINQ will spread out the work on all available cores.
- Demo 1: Financial analysis; find elements in the input that optomize some function.
public static string[] FindStocksLINQ(int threshhold)
{
return
( from stockName in GetStockNames().AsParallel()
let score = ComputeStockScore(stockName)
where score >= scoreThreshold
orderby stockName
select stockName).ToArray();
}- Partitioning, Query Execution - Merging...
- Why isn't LINQ to Objects just parallel? Conclusion: Parallelization is opt-in per query!
int count = 0;
var query = from x in source
where count++ < 5
select x;
would fail in PLINQ but work in LINQ to Objects. PLINQ queries should be observationally pure, at least threadsafe!
If two possible exceptions occur, which one and what one should the user see? Its nor obvious which one! All ideas are arbitrary....The approach PLINQ took is to return an aggregate exception containing all exceptions that occured.
If multiple threads fill their output buffer, the assembly of the final result can be order dependent; However, if it is parallel - who knows? You can pass in a flag to the as parallel method: E.g. .AsParallel(ParallelQueryOptions.PreserveOrdering).
Parallelism takes overhead. You need to use parallesism only when it will help. Some other queries perform too little work, or too few elements to outweigh the cost of parallelization.
- Parallel Extensions for .NET. VS2008 necessary. Download from
The MSDN Concurrency Website- Common work-stealing scheduler
- Tasks and data parallel APIs (parallel loops, parallel blocks, tasks, futures, etc.)
- PLINQ
- Communications and synchronization primitives
- Futures:
Not final code, but as an example:
Future<int> future = Parallel.CreateFuture( () => Compute(); );
// this will either return the previously computed value, or block until it is computed.
int x = future.Value;