Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

Parallel LINQ CodeCamp Seattle Notes

Modified: 2008/01/28 00:10 by 66.192.186.101 - Categorized as: CodeCamp, PLINQ
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!

  • Side affects

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!


  • Concurrent Exceptions

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.


  • Output Ordering

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).


  • Performance Overhead

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;



If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or omissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain unintentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.