Transcript
Page 2: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

?

• Datalog fra Århus• Speciale: Neural

Networks

• DDE / Ehuset• KMD• Bluetags• Statsbilioteket• Saxotech• Gatehouse• Nordjysk Elhandel

Page 3: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

NEAS.DK

• Nordjysk Elhandel• Pris aftaler el forbrug • Pris aftaler el produktion • Klima • Trading

Page 4: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Agenda

• Quick Linq Intro• Plinq Intro• Plinq Pitfalls• Task Parallel Library

Page 5: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Quick Linq Intro

• Slides by Paul Litwin

Page 6: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

PLinq

• Parrallel Linq• Make it easy for the developer to

take advantage of multiple cores.• Declarative way of expressing

parrallel computations

Page 7: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Thread Pool

• A lot of code needed to manage the plumbing invovled in this

Page 8: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Plinq thread Pool

• Split the enumerable list into partitions and let the threads run the tasks

Page 9: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

AsParrallel

• .AsParrallel• Extension to IEnumerable• public static ParallelQuery AsParallel( this IEnumerable source )

• The Ienumerable source should represent the tasks that should run in parrallel

Page 10: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

ParallelQuery

• .WithDegreeOfParallelism()• The number of threads to be used

• Example:

Page 11: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Partitioner

• The Plinq runtime choose how to partition the source

• The default for List<T> source is the range partitioner

Page 12: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Partitioner

• Chunk Partitioning

• Striped Partitioning

• Hash Partitioning

Page 13: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Partitioner

• You can write a custom Partitioner that inherits from Partitioner<T>• This can tune the performance if the

partitioning is a bottleneck• Example

Page 14: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

ParallelQuery

• WithMergeOptions()• FullyBuffered each thread process the

part, then merge • Example

Page 15: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

ParallelQuery

• WithCancellation(CancellationToken)• Makes it possible to cancel the

parrallel query• AsOrdered()

• Wait for all threads to finish and then orders the result as a normal query

Page 16: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

ParallelQuery

• ForAll()• Do some work for all elements but

returns nothing

Page 17: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Exceptions

• AggregateException• Collects all the exceptions thrown by

the threads • InnerExceptions member returns the

list of exceptions

Page 18: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

When to use Plinq

• N elements - M threads - T time to process • Overhead

• Split the enumerable into parts O(N) • Start the threads O(M)• Merge the part results into the complete

result O(N)• Gain O(N/M*T)• O(N/M*T) > (2*O(N)+ O(M))

Page 19: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Plinq and db sources

• Beware if the source for the AsParrallel is a database source.

• Example

Page 20: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Task Parallel Library

Page 21: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Task Parallel Library

• Parallel.InvokeParallel.Invoke( () => MethodA(),

() => MethodB(), () => MethodC());

• Parallel.ForEach• Parallel.ForEach(list, e => { DoWork(e) });

• Parallel.ForParallel.For(start, end, (i) => DoSomeWork(i));

Page 22: Plinq Presentation Steen L. Knudsen slk@neas.dk steen.l.knudsen@gmail.com

Task Parallel Library

• Task<T>• ”Future” a proxy for an object not yet

computed. • Task task = Task.Factory.StartNew(() => DoAction());

• Is the task done:• If(task.IsCompleted)

• Wait to the job to be done• task.Wait();


Recommended