Entity Framework 4 In Microsoft Visual Studio 2010 - ericnel
Overview Of Parallel Development - Ericnel
-
Upload
ukdpe -
Category
Technology
-
view
1.878 -
download
0
description
Transcript of Overview Of Parallel Development - Ericnel
![Page 1: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/1.jpg)
1
Overview of Parallel DevelopmentVisual Studio 2010 + a little on Axum and Concurrent Basic
Eric [email protected] http://geekswithblogs.net/iupdateablehttp://blogs.msdn.com/goto100 http://twitter.com/ericnel
![Page 2: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/2.jpg)
htt
p:/
/msd
n.m
icro
soft
.com
/uk/
flash
Editor of the UK MSDN Flash
MSDN Flash eBook 13 of the “Best Technical Articles of 2008”http://bit.ly/flashebook1
MSDN Flash Podcast PilotFor feedbackhttp://bit.ly/flashpod1
Technical Authors wanted for the Flash – 400 to 500 words. Fancy it?
Microsoft UK MSDN Flash NewsletterEvery two weeks, pure joy enters your Inbox
Poll
TipTech
nical
Article
Editori
alFreshDiscoveries
UK Events
![Page 3: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/3.jpg)
Agenda
Overview of what we are up toDrill down into parallel programming for managed developersIf we have time, “heads up” on Axum and CB
![Page 4: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/4.jpg)
Things I learnt...We have a very large investment in parallel computing
We have “something for everyone”It is not all synced, it is sometimes overlapping
It is a big topicManaged vs native vs client vs server vs task vs data...
Even with the investment, design/code/test for parallel is far harder
Locking, Deadlocks, LivelocksIt is about getting ready for the future
Code today – run better tomorrow?VS2010 CTP – not a great place for parallel
Single core in guestUnsupported route to use Hyper-V
Easiest route to dabble – Microsoft Parallel Extensions June CTP for VS2008
![Page 5: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/5.jpg)
Buying a new Processor
£100 - £300
2-3GHz
2 cores or 4
64-bit
Core
Core
![Page 6: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/6.jpg)
Buying a new Processor
CoreCoreCoreCore£200 - £500
2-3GHz
4 cores with HT
64-bit
QuickPath Interconnect
Memory Controller
![Page 7: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/7.jpg)
Where will it all end?
![Page 8: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/8.jpg)
Was it a wise purchase?
Windows OS
App 1 App 2 ...
App 1
.NET CLR
.NET Framework
My Code
![Page 9: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/9.jpg)
Was it a wise purchase?
Some environments scale to take advantage of additional CPU cores (mostly server-side)
A lot of code does not (mostly client-side)This code will see little benefit from future hardware advances
ASP.NET Web Forms/Services WCF Services WF Engine ...
.NET ThreadPool or Custom Threading Strategy
![Page 10: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/10.jpg)
What happened to “The Free Lunch”?
Bad sequential code will run faster on a faster processor
1 2 4 8 16 320
0.5
1
1.5
2
2.5
3
Speedup
Speedup
Just using parallel code is not enoughBad parallel code WILL NOT run faster on more cores
![Page 11: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/11.jpg)
0
16
32
48
64
0 16 32 48 64
Cores
Par
alle
l S
pee
du
p
Production Fluid
Production Face
Production Cloth
Game Fluid
Game Rigid Body
Game Cloth
Marching Cubes
Sports Video Analysis
Video Cast Indexing
Home Video Editing
Text Indexing
Ray Tracing
Foreground Estimation
Human Body Tracker
Portifolio Management
Geometric Mean
Graphics Rendering – Physical Simulation -- Vision – Data Mining -- Analytics
Applications Can Scale Well
![Page 12: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/12.jpg)
Multithreaded programming is “hard” todayDoable by only a subgroup of senior specialistsParallel patterns are not prevalent, well known, nor easy to implementSo many potential problems
Races, deadlocks, livelocks, lock convoys, cache coherency overheads, lost event notifications, broken serializability, priority inversion, and so on…
Businesses have little desire to “go deep”Best developers should focus on business value, not concurrencyNeed simple ways to allow all developers to write concurrent code
What's The Problem?
![Page 13: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/13.jpg)
Example: Matrix Multiplication
void MatrixMult( int size, double** m1, double** m2, double** result){ for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { result[i][j] = 0; for (int k = 0; k < size; k++) { result[i][j] += m1[i][k] * m2[k][j]; } } }}
![Page 14: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/14.jpg)
Manual Parallel Solution
void MatrixMult( int size, double** m1, double** m2, double** result) { int N = size; int P = 2 * NUMPROCS; int Chunk = N / P; HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); long counter = P; for (int c = 0; c < P; c++) { std::thread t ([&,c] { for (int i = c * Chunk; i < (c + 1 == P ? N : (c + 1) * Chunk); i++) { for (int j = 0; j < size; j++) { result[i][j] = 0; for (int k = 0; k < size; k++) { result[i][j] += m1[i][k] * m2[k][j]; } } } if (InterlockedDecrement(counter) == 0) SetEvent(hEvent); }); } WaitForSingleObject(hEvent,INFINITE); CloseHandle(hEvent);}
Synchronization Knowledge
Error prone
Heavy synchronization
Static partitioning
Lack of thread reuse
Tricks
Lots of boilerplate
![Page 15: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/15.jpg)
Microsoft Parallel Computing Technologies
•Robotics-based manufacturing assembly line•Silverlight Olympics viewer
•Enterprise search, OLTP, collab•Animation / CGI rendering•Weather forecasting•Seismic monitoring•Oil exploration
•Automotive control system •Internet –based photo services
•Ultrasound imaging equipment •Media encode/decode•Image processing/ enhancement•Data visualization
Task Concurrency
Data Parallelism
Distributed/Cloud Computing
LocalComputin
g
CCR
Maestro aka AxumTPL / PPL
Cluster TPL
Cluster PLINQ
MPI / MPI.Net
WCF
Cluster SOA
WF
PLINQ
TPL / PPL
CDS
OpenMP
WF
Compute Shader
![Page 16: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/16.jpg)
Visual Studio 2010Tools / Programming Models / Runtimes
Parallel Pattern Library
Resource Manager
Task Scheduler
Task Parallel Library
PLINQ
Managed Library Native LibraryKey:
ThreadsOperating System
Concurrency Runtime
Programming Models
AgentsLibrary
ThreadPool
Task Scheduler
Resource Manager
Data Structures
Dat
a St
ruct
ures
Integrated Tooling
Tools
ParallelDebugger
Tool
Profiler Concurrency
Analysis
Programming Models
Concurrency Runtime
![Page 17: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/17.jpg)
17
Explicit Tasking Support
.NET 4.0 Task Parallel Library
Task, TaskFactoryParallel.ForParallel.ForeachParallel.InvokeConcurrent data structures
Visual Studio 2010 C++Parallel Pattern Library
task, task_groupparallel_forparallel_for_eachparallel_invokeConcurrent data structuresPrimitives for message passingUser-mode locks
![Page 18: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/18.jpg)
Task Parallel Library ( TPL )
![Page 19: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/19.jpg)
19
Task
No Threadingto Threadingto Tasks
demo
![Page 20: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/20.jpg)
Program Thread
CLR Thread Pool
User Mode Scheduler
GlobalQueue
Worker Thread 1
Worker Thread p
…
![Page 21: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/21.jpg)
CLR Thread Pool: Work-Stealing
Worker Thread 1
Worker Thread p
…
Program Thread
User Mode Scheduler For Tasks
GlobalQueue
LocalQueue
LocalQueue
…
Task 1Task 2Task 3
Task 5Task 4Task 6
![Page 22: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/22.jpg)
22
Tasks revisited
More on Tasks
demo
![Page 23: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/23.jpg)
Debugger Support
Support both managed and native1. Parallel Tasks2. Parallel Stacks
![Page 24: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/24.jpg)
Higher Level Constructs
Even with Task there are common patterns that build into higher level abstractions
The Parallel classInvoke, For, For<T>, Foreach
Care needs to be taken with state, ordering“This is not your Father’s for loop”
![Page 25: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/25.jpg)
25
Parallel
Parallel.ForEachParallel.Invoke
demo
![Page 26: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/26.jpg)
Declarative Data Parallelism
Parallel LINQ-to-Objects (PLINQ)Enables LINQ devs to leverage multiple coresFully supports all .NET standard query operatorsMinimal impact to existing LINQ model
var q = from p in people where p.Name == queryInfo.Name && p.State == queryInfo.State && p.Year >= yearStart && p.Year <= yearEnd orderby p.Year ascending select p;
.AsParallel()
![Page 27: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/27.jpg)
27
Parallel LINQ
demo
![Page 28: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/28.jpg)
Example: "Baby Names"
IEnumerable<BabyInfo> babies = ...;var results = new List<BabyInfo>();foreach(var baby in babies){ if (baby.Name == queryName && baby.State == queryState && baby.Year >= yearStart && baby.Year <= yearEnd) { results.Add(baby); }}results.Sort((b1, b2) => b1.Year.CompareTo(b2.Year));
![Page 29: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/29.jpg)
Manual Naïve Parallel Solution
IEnumerable<BabyInfo> babies = …;var results = new List<BabyInfo>();int partitionsCount = Environment.ProcessorCount;int remainingCount = partitionsCount;var enumerator = babies.GetEnumerator();try { using (var done = new ManualResetEvent(false)) { for(int i = 0; i < partitionsCount; i++) { ThreadPool.QueueUserWorkItem(delegate { while(true) { BabyInfo baby; lock (enumerator) { if (!enumerator.MoveNext()) break; baby = enumerator.Current; } if (baby.Name == queryName && baby.State == queryState && baby.Year >= yearStart && baby.Year <= yearEnd) { lock (results) results.Add(baby); } } if (Interlocked.Decrement(ref remainingCount) == 0) done.Set(); }); } done.WaitOne(); results.Sort((b1, b2) => b1.Year.CompareTo(b2.Year)); }}finally { if (enumerator is IDisposable) ((IDisposable)enumerator).Dispose(); }
![Page 30: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/30.jpg)
LINQ Solution
var results = from baby in babies where baby.Name == queryName && baby.State == queryState && baby.Year >= yearStart && baby.Year <= yearEnd orderby baby.Year ascending select baby;
.AsParallel()
![Page 31: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/31.jpg)
Coordination Data Structures
Thread-safe collectionsConcurrentStack<T>...
LocksSpinLock, SpinWait, SemaphoreSlim ...
Work ExchangeBlockingCollection<T> ...
Phased OperationCountdownEvent ...
![Page 32: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/32.jpg)
32
Coordination Data Structures
demo
![Page 33: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/33.jpg)
What Next?
http://geekswithblogs.net/iupdateable Slides and links http://blogs.msdn.com/pfxteam/ http://msdn.com/concurrency
Wait for the Beta of Visual Studio 2008 andOR for the most impatient
Download VS 2010 CTPRemember to set the clock back
OrDownload Parallel Extensions June 2008 CTP for VS2008
![Page 34: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/34.jpg)
34
Appendix
![Page 35: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/35.jpg)
Heads up: Axum
Previously called MaestroIncubation project!New programming languageLets you take advantage of parallelism without “thinking about it”Agent based programming vs Object based programming
Model agents and their interactions via messages No public methods, fields
![Page 36: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/36.jpg)
Axum “Hello World”
using System; agent Program :
Microsoft.Axum.ConsoleApplication { override int Run(String[] args) { Console.WriteLine("Hello, World!"); } }
![Page 37: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/37.jpg)
Channels and Agents
using System; using System.Concurrency; using Microsoft.Axum; channel Adder { input int Num1; input int Num2; output int Sum; } agent AdderAgent : channel Adder { public AdderAgent() { int result = receive(PrimaryChannel::Num1) + receive(PrimaryChannel::Num2); PrimaryChannel::Sum <-- result; } }
agent MainAgent : channel Microsoft.Axum.Application { public MainAgent() { var adder = AdderAgent.CreateInNewDomain(); adder::Num1 <-- 10; adder::Num2 <-- 20; // do something useful ... var sum = receive(adder::Sum); Console.WriteLine(sum); PrimaryChannel::ExitCode <-- 0; } }
![Page 38: Overview Of Parallel Development - Ericnel](https://reader038.fdocuments.net/reader038/viewer/2022110306/554f8943b4c905d25b8b4e21/html5/thumbnails/38.jpg)
Heads up: Concurrent Basic
Research Projecthttp://channel9.msdn.com/shows/Going+Deep/Claudio-Russo-and-Lucian-Wischik-Inside-Concurrent-Basic/
Added message passing primitives – channels
Module Buffer Public Asynchronous Put(ByVal s As String) Public Synchronous Take() As String Private Function CaseTakeAndPut(ByVal s As String) As String When Take, Put Return s End Function End Module
Thread1: Thread2:Put(“Hello”) result = Take()