View
226
Download
0
Category
Preview:
Citation preview
?
• 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
• Quick Linq Intro• Plinq Intro• Plinq Pitfalls• Task Parallel Library
Quick Linq Intro
• Slides by Paul Litwin
PLinq
• Parrallel Linq• Make it easy for the developer to
take advantage of multiple cores.• Declarative way of expressing
parrallel computations
Thread Pool
• A lot of code needed to manage the plumbing invovled in this
Plinq thread Pool
• Split the enumerable list into partitions and let the threads run the tasks
AsParrallel
• .AsParrallel• Extension to IEnumerable• public static ParallelQuery AsParallel( this IEnumerable source )
• The Ienumerable source should represent the tasks that should run in parrallel
ParallelQuery
• .WithDegreeOfParallelism()• The number of threads to be used
• Example:
Partitioner
• The Plinq runtime choose how to partition the source
• The default for List<T> source is the range partitioner
Partitioner
• Chunk Partitioning
• Striped Partitioning
• Hash Partitioning
Partitioner
• You can write a custom Partitioner that inherits from Partitioner<T>• This can tune the performance if the
partitioning is a bottleneck• Example
ParallelQuery
• WithMergeOptions()• FullyBuffered each thread process the
part, then merge • Example
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
ParallelQuery
• ForAll()• Do some work for all elements but
returns nothing
Exceptions
• AggregateException• Collects all the exceptions thrown by
the threads • InnerExceptions member returns the
list of exceptions
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))
Plinq and db sources
• Beware if the source for the AsParrallel is a database source.
• Example
Task Parallel Library
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));
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