Slides from Phil Pennington\'s talk on Using Parallel Computing with Visual Studio 2010 and .NET 4.0, originally presented at the North Houston .NET Users Group (facebook.com/nhdnug).
The 7 Things I Know About Cyber Security After 25 Years | April 2024
Using Parallel Computing Platform - NHDNUG
1. Visual Studio 2010 Using the Parallel Computing Platform Phil Pennington philpenn@microsoft.com
2. Agenda 2 What’s new with Windows? Parallel Computing Tools in Visual Studio Using .NET Parallel Extensions
3. First, An ExampleMonte Carlo Approximation of Pi S = 4*r*r C = Pi*r*r Pi = 4*(C/S) For each Point (P), d(P) = SQRT((x * x) + (y * y)) if (d < r) thenP(x,y) is in C
4. Windows and Maximum Processors Before Win7/R2, the maximum number of Logical Processors (LPs) was dictated by processor integral word size LP state (e.g. idle, affinity) represented in word-sized bitmask 32-bit Windows: 32 LPs 64-bit Windows: 64 LPs 32-bit Idle Processor Mask 31 0 16 Busy Idle
5. Processor GroupsNew with Windows7 and Windows Server R2 5 GROUP NUMA NODE Socket Socket Core Core LP LP LP LP Core Core NUMA NODE
10. User Mode SchedulingArchitectural Perspective UMS Scheduler’s Ready List Your Scheduler Logic Wait Reason: Yield Reason: Yield Reason: Blocked Reason: Created CPU 1 CPU 2 UMS Completion List W1 W2 W3 W4 S1 S2 Application Kernel Blocked Worker Threads Scheduler Threads
11. Task Scheduling with a UMS SchedulerMaximize Quantum, Minimize Blocking Affects Tasks are run by worker threads, which the scheduler controls Dead Zone WT0 WT1 WT2 WT3 Without UMS (signal-and-wait) WT0 WT1 WT2 WT3 With UMS (UMS yield)
12. Load-Balancing, Work Stealing Scheduler DynamicScheduling Static Scheduling CPU0 CPU1 CPU2 CPU3 CPU0 CPU1 CPU2 CPU3 Dynamic scheduling improves performance by distributing work efficiently at runtime.
16. Thread-Pool Scheduler in .NET 4.0 Thread 1 Dispatch Loop Thread 2 Dispatch Loop Thread N Dispatch Loop Enqueue Dequeue T1 T2 T3 T4 Global Queue (FIFO) Dequeue Enqueue T5 Global Q is shared by legacy ThreadPool API and TPL Local work queues and work stealing scheduler (TPL only) T6 T7 T8 Steal Steal Steal Thread 1 Local Queue (LIFO) Thread 2 Local Queue (LIFO) Thread N Local Queue (LIFO)
29. Threads Measure time for interesting segments Hide uninteresting threads Zoom in and out Detailed thread analysis (one channel per thread) Active Legend Usage Hints Call Stacks
30. Cores Each logical core in a swim lane One color per thread Migration visualization Cross-core migration details
32. Agenda 27 What’s new with Windows? Parallel Computing Tools in Visual Studio Using .NET Parallel Extensions
33. Thinking Parallel - “Task” vs. “Data” Parallelism Task Parallelism Parallel.Invoke( () => { Console.WriteLine("Begin first task..."); }, () => { Console.WriteLine("Begin second task..."); }, () => { Console.WriteLine("Begin third task..."); } ); Data Parallelism IEnumerable<int> numbers = Enumerable.Range(2, 100-3); varmyQuery = from n in numbers.AsParallel() where Enumerable.Range(2, (int)Math.Sqrt(n)).All(i => n % i > 0) select n; int[] primes = myQuery.ToArray();
34. Thinking Parallel – How to Partition Work? Several partitioning schemes built-in Chunk Works with any IEnumerable<T> Single enumerator shared; chunks handed out on-demand Range Works only with IList<T> Input divided into contiguous regions, one per partition Stripe Works only with IList<T> Elements handed out round-robin to each partition Hash Works with any IEnumerable<T> Elements assigned to partition based on hash code Custom partitioning available through Partitioner<T> Partitioner.Create available for tighter control over built-in partitioning schemes
38. Resources NativeAPIs/runtimes (Visual C++ 10) Tasks, loops, collections, and Agents http://msdn.microsoft.com/en-us/library/dd504870(VS.100).aspx Tools (in the VS2010 IDE) Debugger and profiler http://msdn.microsoft.com/en-us/library/dd460685(VS.100).aspx Managed APIs/runtimes (.NET 4) Tasks, loops, collections, and PLINQ http://msdn.microsoft.com/en-us/library/dd460693(VS.100).aspx General VS2010 Parallel Computing Developer Center http://msdn.microsoft.com/en-us/concurrency/default.aspx
Notas do Editor
Let’s use this slide for an “Architectural Perspective” of UMS.<CLICK>S1 and S2 are the first threads created within a UMS solution. These are “Scheduler Threads” or “Primary Threads”. These threads represent “core” or physical CPU’s from a Scheduler perspective. These are normal threads to begin with, but you would typically first establish processor affinity using the new CreateRemoteThreadEx API and the use a new API, EnterUmsSchedulingMode, to specify that the new thread is a Scheduler thread.You pass in a callback, i.e. UMSSchedulerProc, function pointer to begin executing instructions on the Scheduler thread.A UMS worker thread is created by calling CreateRemoteThreadEx with the PROC_THREAD_ATTRIBUTE_UMS_THREAD attribute and specifying a UMS thread context and a completion list. The OS places these threads into the Completion List and your Scheduler logic takes over typically placing the new threads onto the Scheduler’s Ready List.<CLICK>The first thing that a Scheduler should do is move it’s associated Worker threads onto the Scheduler’s Ready List. Then, it can began executing your customer scheduler logic.<CLICK>Each of the Scheduler threads should then pop a Worker thread off of the Ready List and run it on the associated Core. When this occurs, the Scheduler thread context is essentially lost forever… the Worker thread now owns the core and is executing. The Scheduler thread will not regain the core until a processor Yield event occurs.<CLICK>The first thing that could happen is that this thread could yield. Yield is again a Scheduler callback mechanism and perhaps the single most important function of UMS. It’s within the Yield that you will implement your own synchronization primitives and scheduling logic.Ideally, the yielding thread provides some contextual information to the scheduler (maybe it wants to wait on some specific application domain event to occur). Your Scheduler would look at this Yield request and associated context and make a scheduling decision.<CLICK>Maybe the Scheduler places the Worker thread within a Wait list for that specific event or event type.Now your Scheduler has to decide what to run next. <CLICK>Maybe the next Worker thread from the Ready List, for instance… and we’re back running again. Note, that no kernel scheduling context switch was necessary. Maybe that wait event handling took 200 cycles in user-mode. It may have cost 10 times that with a kernel context switch.<CLICK>Let’s now assume that this worker performs a system call… At this point, we switch the worker thread to it’s kernel-mode context and the thread continues to run within the kernel. If it does not block (in other words, if it doesn’t use one of the kernel synchronization primitives, then it just continues to run. If the thread never blocks in the kernel, then it just returns to user-mode and continues to run and do work.<CLICK>Let’s assume that the thread does block. Maybe a page fault occurred, for instance. Now our Scheduler thread regains control of the processor via a callback from the kernel. Now, the kernel is telling your Scheduler that a worker thread is blocked and the reason for that block. This is the point where we integrate kernel synchronization with user synchronization. But now, you get to decide what to run next.<CLICK>The Scheduler looks at the state of it’s affairs and perhaps decides to run the next Worker thread from the Ready List, for example.Let’s assume that later in time Worker 3 unblocks. <CLICK>The kernel will now place this unblocked Worker thread into the UMS Completion List.<CLICK>At the next Yield event, for instance, we get another Scheduler decision opportunity. Maybe this Yield contains information that affects the state of our Wait list.<CLICK>The first thing that the Scheduler should do, however, is manage the Completion List and move any unblocked threads to the Ready List.<CLICK>Next, our Scheduler must make a priority decision. Maybe our Waiting thread gets to run again and our Yielding thread gets placed upon the Ready List.And we’re done…
UMS is an enabler for:Finer-grained parallelismMore deterministic behaviorBetter cache localityUMS allows your Scheduler to boost performance in certain situations:Apps that have a lot of blocking, for example
Think Tasks not Threads.Threads represent execution flow, not workHard-coded; significant system overheadMinimal intrinsic parallel constructsQueueUserWorkItem() is handy for fire-and-forgetBut what about…WaitingCancelingContinuingComposingExceptionsDataflowIntegrationDebugging
NOW, LET’S FIRST CONSIDER THE TOOLS ARCHICTECTURE FROM A .NET DEVELOPER’S PERSPECTIVE.LET’S START WITH THE .NET Runtime AND THE .NET Parallel Extensions library. In a moment, we’ll look at how a developer uses the extensions within their application. The .NET PARALLEL EXTENSIONS provide the benefits of concurrent task scheduling without YOU having to build a custom scheduler that is appropriately reentrant, thread-safe, and non-blocking.<CLICK>The Parallel Extensions library contains a Task Scheduler and a Resource Manager component that integrates with the underlying .NET Runtime. The Resource Manager manages access to system resources like the collection of available CPU’s. <CLICK>The Scheduler leverages only thread pools for task scheduling. <CLICK>The Parallel Extensions also supports multiple Programming Models. <CLICK>The Task Parallel Library (TPL) is an easy and convenient way to express fine-grain parallelism within your applications. The TPL provides patterns for Task Execution, Synchronization, and Data Sharing.<CLICK>The PLINQ (or Parallel LINQ) enables parallel query execution not only on SQL Data but also on XML or Collections Data.<CLICK>The Parallel Extensions also includes Data Structures that are “scheduler aware” enabling you to optimally specify task scheduling requests and custom scheduler policies.<CLICK>Again, Visual Studio 2010 includes new tools for parallel application development and testing. These include:<CLICK>A new parallel debugger. And…<CLICK>A new parallel application profiler.Let’s take a brief look at a simple .NET parallel application along with the Visual Studio 2010 Debugger and Parallel Performance Analyzer.Pure .NET librariesFeature areasTask Parallel LibraryParallel LINQSynchronization primitives and thread-safe data structuresEnhanced ThreadPool