Friday 7 September 2012

Some Notes about Parallel Programming in .NET 4 (and 4.5)

- Try to avoid writing to shared memory object such as static variables or class properties. Using locks in parallelization will suffer the performance.

- Only use thread-safe methods. Calling to non thread-safe methods in parallel programming can cause exceptions and undetected data loss.

- Most of the time, any regular loop that fulfills both of the above requirements can be converted into a parallel loop.

- Keep it simple and avoid over-parallelization (i.e.; unnecessary nested parallelization). When using parallelization there is an overhead cost to partition the work and merge the result back.

- If the parallel work is used to populate data into a collection, use one of the thread-safe collection types.
http://msdn.microsoft.com/en-us/library/dd997305.aspx

- Avoid any ordering operation if possible. By default PLINQ does not preserve the ordering sequence in the source.
http://msdn.microsoft.com/en-us/library/dd460677.aspx

- When needed to do further operation of a PLINQ result, prefer to use ForAll() method instead of Parallel.ForEach().
For example; use this
source.AsParallel()
      .Where( i => i.SomePredicate() )
      .ForAll( i => i.DoSomething() );
instead of
var filteredItems = source.AsParallel().Where( i => i.SomePredicate() );

Parallel.ForEach(filteredItems, item =>
{
    item.DoSomething();
});

References:
Potential Pitfalls with PLINQ
Parallelism in .NET – Part 2, Simple Imperative Data Parallelism
Parallelism in .NET – Part 6, Declarative Data Parallelism
Parallelism in .NET – Part 8, PLINQ’s ForAll Method

No comments: