parallel computing in . net 4 and visual studio 2010
DESCRIPTION
James Kolpack, InRAD LLC popcyclical.com. Parallel Computing in . NET 4 and Visual Studio 2010. CodeStock is proudly partnered with:. RecruitWise and Staff with Excellence - www.recruitwise.jobs. Send instant feedback on this session via Twitter: - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/1.jpg)
James Kolpack, InRAD LLCpopcyclical.com
PARALLEL COMPUTINGIN
.NET 4AND
VISUAL STUDIO 2010
![Page 2: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/2.jpg)
CodeStock is proudly partnered with:
Send instant feedback on this session via Twitter: Send a direct message with the room number to @CodeStock d codestock 406 This session is great!
For more information on sending feedback using Twitter while at CodeStock, please see the “CodeStock README” in your CodeStock guide.
RecruitWise and Staff with Excellence - www.recruitwise.jobs
![Page 3: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/3.jpg)
![Page 4: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/4.jpg)
Parallel computing
Manycore machines…
…have arrived!
![Page 5: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/5.jpg)
“Free Lunch” is over - TANSTAAFL
Performance and
Scalability
![Page 6: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/6.jpg)
Outline• Parallel Extensions• Imperative Data Parallelization• Imperative Task Parallelization• Declarative Data Parallelization (PLINQ)• Thread-safe Data Structures
• Visual Studio 2010 Concurrency Visualizations
![Page 7: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/7.jpg)
Parallel Extensions• Concurrency .NET Library• Lightweight user-mode runtime
• Key benefits• Lightweight tasks, efficient scheduling• Programming model improvements• Unified exception models and scheduling
• Great for “Delightfully” Parallel Problems• Computation-intensive processes for large
data sets
![Page 8: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/8.jpg)
Decomposition• Breaking a problem into discrete parts• Data Decomposition• Iterations (for loops) over data• Simple!
• Task Decomposition• Separate operations that can run
independently of each other• …Or both!
![Page 9: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/9.jpg)
Data Decompositionint[,] pixelData = new int[rowLen, colLen];
for (int y = 0; y < rowLen; y++){
for (int x = 0; x < colLen; x++){
pixelData[y, x] = TraceRay(y, x);}
}
![Page 10: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/10.jpg)
Sequentialx1 x2 x3 x4
y1
y2
y3
y4
codeSample();
![Page 11: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/11.jpg)
Thread for each Rowx1 x2 x3 x4
y1
y2
y3
y4
codeSample();
![Page 12: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/12.jpg)
Threading with 2 Partitionsx1 x2 x3 x4
y1
y2
y3
y4
codeSample();
![Page 13: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/13.jpg)
Imbalanced Workloadx1 x2 x3 x4
y1
y2
y3
y4
![Page 14: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/14.jpg)
Dynamic Partitioningx1 x2 x3 x4
y1
y2
y3
y4
![Page 15: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/15.jpg)
Partitioning TradeoffsFu
lly S
tatic
Fully Dynamic
More Load-Balancing
Less Synchronization
![Page 16: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/16.jpg)
Partitioning StrategiesFully Static
Fully Dynamic
![Page 17: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/17.jpg)
Partitioning StrategiesDynamic Chunks
Load Balancing
![Page 18: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/18.jpg)
Sample Application• For Real Ray Tracer
Samples for Parallel Programming with .NET 4code.msdn.microsoft.com/ParExtSamples
![Page 19: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/19.jpg)
Parallel Extensions Architecture
Task Parallel Library (TPL)
Parallel Constructs
.NET ProgramImperative
Parallel Algorithms
CompilerC#
VB
F#
C++(any
other)
IL
Data Structures
Concurrent CollectionsSynchronization Types
Coordination Types
AlgorithmsThreads
Tasks and Tasks Scheduling
Proc 1 … Proc p
![Page 20: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/20.jpg)
Lambda Expressions(input parameters) => expression
() => SomeMethod()
(input parameters) => {statement;}
(int i, string s) => { Console.WriteLine( “str: {0} int: {1}”, s, i); }
bool MyFunc(int i, string s){ return s.Length > i;}
void MyAction(int i, string s){ Console.WriteLine( “str: {0} int: {1}”, s, i);}
(int i, string s) => s.Length > i
![Page 21: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/21.jpg)
Func<int, string, bool>
Generic DelegatesTResult Func<out TResult>()TResult Func<in T1, out TResult>(in T1)TResult Func<in T1, in T2, out TResult>(in T1, in T2)
Func<int, string, bool> f = (int i, string s) => s.Length > i;
Action<int, string> a = (int i, string s) => { Console.WriteLine( “str: {0} int: {1}”, s, i); }
Action<int, string> a = MyAction;
bool MyFunc(int i, string s){}
void MyAction(int i, string s){}
Func<int, string, bool> f = MyFunc;
void Action<>()void Action<in T1>(in T1)Void Action<in T1, in T2>(in T1, in T2)
![Page 22: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/22.jpg)
Imperative Data Parallelization• Parallel.For, Parallel.ForEach• Exception handling• Cancelling• Break and Stop
• Thread-local state• Nested parallelism• Dynamic thread
counts• Efficient load
balancing
Parallel.For(0, n, i =>{ // ...});
codeSample();
Parallel.ForEach(data, d =>{ // ...});
![Page 23: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/23.jpg)
Imperative Task Parallelization• Tasks - Lighter weight than raw
Threads• Intelligent scheduling using ThreadPool
• Rich API for fine grained control• Waiting, cancellation, continuations,
exceptions, etc
Parallel.Invoke(() => DoComputation(),() => DoAnotherCompuation()
);
codeSample();
![Page 24: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/24.jpg)
Concurrent Collections• Concurrent Collections
• Thread-safe!• Time-outs and waits• Throttling• Cancellation
• Classes• BlockingCollection<T>• ConcurrentDictionary<T>• ConcurrentQueue<T>• ConcurrentStack<T>• ConcurrentBag<T>
codeSample();
![Page 25: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/25.jpg)
Parallel Extensions Architecture
Task Parallel Library (TPL)
Parallel Constructs
.NET ProgramDeclarative
ParallelQueries
Imperative Parallel
Algorithms
PLINQ Execution EngineQuery Analysis
Data Partitioning
Operator Types Merging
CompilerC#
VB
F#
C++(any
other)
IL
Data Structures
Concurrent CollectionsSynchronization Types
Coordination Types
PLINQ
AlgorithmsThreads
Tasks and Tasks Scheduling
Proc 1 … Proc p
![Page 26: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/26.jpg)
Parallel LINQ (PLINQ)• Declarative Data
Parallelization • Describe what we want rather than
how to accomplish it• For LINQ to Object queries
• System.Linq.Parallel• .AsParallel()
codeSample();
![Page 27: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/27.jpg)
PLINQ ForAll
codeSample();
![Page 28: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/28.jpg)
PLINQ Performance Considerations• Computational cost of the overall work
• The form of query execution• With ToArray or ToList, all results must be
merged• The type of merge options• Buffered or Streaming
• The kind of partitioning• Sequential mode fall-back
var queryA = from num in numberList.AsParallel() select ExpensiveFunction(num); //good for
PLINQ
var queryB = from num in numberList.AsParallel() where num % 2 > 0 select num; //not so good for PLINQ
![Page 29: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/29.jpg)
Parallel API Review• Imperative Data Parallelization
• System.Threading.Tasks.Parallel
• Imperative Task Parallelization• System.Threading.Tasks
• Declarative Data Parallelization (PLINQ)• System.Linq.Parallel
• Thread-safe Data Structures• System.Collections.Concurrent
Parallel.For(0, n, i => {});Parallel.ForEach(data, i => {});
var t = Task<TResult> .Factory .StartNew(() => {});t.Wait();
from n in nums.AsParallel()select ExpressiveFunction(n);
new BlockingCollection<string>();
![Page 30: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/30.jpg)
Parallel Patterns• Fork/Join• Recursive Decomposition• Aggregations• Dependencies• Producer/Consumer• MapReduce• Fold and Scan• Shared State• Anti-Patterns
![Page 31: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/31.jpg)
Threads View
Concurrency Visualizers
CPU Utilization View
Cores View
![Page 32: Parallel Computing in . NET 4 and Visual Studio 2010](https://reader036.vdocuments.us/reader036/viewer/2022062323/56816456550346895dd624a1/html5/thumbnails/32.jpg)
LinksBlog series on Parallelism in .NET by Reed Copsey Jr.reedcopsey.com/category/algorithms/parallelismPatterns for Parallel Programming – Stephen Toubbit.ly/caxi9I
MSDN Parallel Computing Portalmsdn.com/concurrency
MSDN Parallel Programming in the .NET Frameworkbit.ly/bo73EI