230 likes | 420 Views
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. NEAS.DK. Nordjysk Elhandel Pris aftaler el forbrug
E N D
PlinqPresentation 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
NEAS.DK • Nordjysk Elhandel • Pris aftaler el forbrug • Pris aftaler el produktion • Klima • Trading
Agenda • QuickLinq Intro • Plinq Intro • PlinqPitfalls • Task Parallel Library
QuickLinq Intro • Slides by Paul Litwin
PLinq • ParrallelLinq • Make it easy for the developer to takeadvantage of multiple cores. • Declarativeway of expressing parrallel computations
Thread Pool • A lot of codeneeded to manage the plumbinginvovled in this
Plinqthread Pool • Split the enumerable list intopartitions and let the threads run the tasks
AsParrallel • .AsParrallel • Extension to IEnumerable • publicstaticParallelQueryAsParallel( thisIEnumerable source ) • The Ienumerablesourceshouldrepresent the tasksthatshould run in parrallel
ParallelQuery • .WithDegreeOfParallelism() • The number of threads to beused • Example:
Partitioner • The Plinqruntimechoosehow to partition the source • The default for List<T> source is the range partitioner
Partitioner • ChunkPartitioning • StripedPartitioning • Hash Partitioning
Partitioner • Youcanwrite a customPartitionerthatinherits from Partitioner<T> • Thiscan tune the performance if the partitioning is a bottleneck • Example
ParallelQuery • WithMergeOptions() • FullyBufferedeachthreadprocess the part, thenmerge • Example
ParallelQuery • WithCancellation(CancellationToken) • Makes it possible to cancel the parrallelquery • AsOrdered() • Wait for all threads to finish and thenorders the result as a normal query
ParallelQuery • ForAll() • Do somework for all elements but returnsnothing
Exceptions • AggregateException • Collects all the exceptionsthrown by the threads • InnerExceptionsmemberreturns the list of exceptions
When to usePlinq • N elements - M threads - T time to process • Overhead • Split the enumerableinto parts O(N) • Start the threads O(M) • Merge the part resultsinto the completeresult O(N) • Gain O(N/M*T) • O(N/M*T) > (2*O(N)+ O(M))
Plinq and dbsources • Bewareif the source for the AsParrallel is a database source. • Example
Task Parallel Library • Parallel.Invoke Parallel.Invoke( () => MethodA(), () => MethodB(), () => MethodC()); • Parallel.For Parallel.For(start, end, (i) => DoSomeWork(i));
Task Parallel Library • Parallel.ForEach
Task<T> • ”Future” a proxy for an object not yetcomputed. • Taskt = Task.Factory.StartNew(() => DoAction());