Download presentation
Presentation is loading. Please wait.
1
to Task Parallel Library (TPL)
Ted Neward Neward & Associates |
2
Credentials Who is this guy? Principal, Neward & Associates
Director -- Developer Relations, Smartsheet.com Microsoft MVP (F#, C#, Architect); Java Expert (JSRs 175, 277) Author Professional F# 2.0 (w/Erickson, et al; Wrox, 2010) Effective Enterprise Java (Addison-Wesley, 2004) SSCLI Essentials (w/Stutz, et al; OReilly, 2003) Server-Based Java Programming (Manning, 2000) Blog: Writing: For more, see
3
Objectives We're going to cover
basic concepts of concurrency and synch high-level review of .NET threads and synch dive into the Task Parallel Library (TPL) Task Data parallelism Task parallelism Dataflow
4
How well do you remember...?
Review How well do you remember...?
5
Concepts of Concurrency
Multitasking, multithreading, oh my!
6
Concepts Why go concurrent?
7
Concepts Concurrency carries with it some costs
most systems have two characteristics: small # of CPUs large # of concurrent tasks to run so we "task-switch" task-switching carries overhead for any collection of work "w", dividing it into "n" parts and running them simultaneously takes longer due to the task-switch time "q" ("quantum")
8
Concepts So why do it? "wait time" "cancellation"/responsiveness
while the CPU is waiting for a result, let other tasks run "cancellation"/responsiveness while a task is waiting for a result, keep the UI (or other foreground activities) responsive segregation of work some problems lend themselves naturally to being seen as separate-yet-connected sequences "massively parallel" problems shifts in hardware/CPU design
9
Concurrency Terminology
A glossary of common concurrency terms
10
Glossary Terminology Concurrency Cooperative vs non-cooperative
Multitasking Threads and Multithreading Parallel Processing Asynchronous Programming Reactive Programming
11
Glossary Concurrency "A perception of doing more than one thing at a time" Computers are concurrent even when executing code serially hardware interrupts, for example This covers a lot of ground threads, coroutines, interrupts, and more
12
Glossary "Cooperative" in a cooperative concurrent system, entities must explicitly work together to achieve Concurrency effectively, each one "takes turns" a poorly-written entity can monopolize the system in a non-cooperative concurrent system, the system retains control of who executes next a poorly-written entity cannot monopolize the system more difficult to get correct harder to synchronize/coordinate actions across entities
13
Glossary Multitasking/multiprocessing
Generally applies to processes running concurrently Windows 3.x was a "cooperative multitasking" system MacOS up through 9 was also "cooperative multitasking" Linux up to 2.x was a "non-cooperative multitasking" system; threads could be approximated using "lightweight processes" Most legacy "job share" mainframes are multitasking
14
Glossary Multithreading
a thread is an operating system concept (not a hardware one) a thread represents a sequential series of actions (code) most of the time, this means an execution stack threads can be started/stopped/paused/resumed threads can have priority of some form
15
Glossary Parallel processing
Work is partitioned up into parallel execution paths typically threads Typically this term also implies multiple execution paths executing at exactly the same moment which would be impossible on a single-CPU/single-core device
16
Glossary Asynchronous Programming
programming such that execution happens non-sequentially typically without having to manage threads by hand in some cases, without threads at all! characterized by "I start something, and eventually it will complete"
17
Glossary Reactive Programming
programming such that the application responds to events not always directly concurrent but concurrent code can often be better-managed using reactive approaches
18
Walking and chewing gum at the same time
.NET Threading Walking and chewing gum at the same time
19
Threading System.Threading.Thread: .NET thread object
may or may not be backed by "real" thread fairly simple API model
20
System.Threading.Thread
Creating a Thread construct Thread object with a delegate parameter either a void ThreadProc(void) or a void ParameterizedThreadProc(Object) Starting the Thread either way, call Start() to begin the Thread's processing no guarantees regarding scheduling
21
System.Threading.Thread
Foreground vs. background CLR will not die until all foreground threads terminate to set Thread to background status via IsBackground property must be done before call to Start()
22
System.Threading.Thread
Thread priority determines scheduling higher-priority thread gets first crack at CPU Thread priorities range from Lowest to Highest Lowest, BelowNormal, Normal, AboveNormal, Highest Threads inherit priority of parent Thread Underlying OS not required to respect Thread priority
23
System.Threading.Thread
Thread lifecycle Thread is either runnable or not-runnable in other words, is Thread being scheduled? runnable does not imply running Thread could be blocked or waiting not-runnable does not imply "dead" Thread could be waiting to be started... or paused... but once dead, Thread remains dead Thread.ThreadState's full list of values: Unstarted, Running, WaitSleepJoin, SuspendRequested, Suspended, Aborted, Stopped
24
System.Threading.Thread
From the outside of the Thread... ... wait for it to finish using Join() ... get name, state, priority, etc.
25
System.Threading.Thread
Threads complete in one of several ways simple completion ThreadProc completes voluntary suicide watch track a variable for signal to complete interruption via Thread.Interrupt() & ThreadInterruptedException abort via Thread.Abort() & ThreadAbortException ThreadAbortException is never cleared when caught
26
System.Threading.ThreadPool
ThreadPool.QueueUserWorkItem passed Action executes on the thread pool but only when there is an available thread to execute ThreadPool consists solely of static methods only one ThreadPool per process
27
Keeping the cookie jar free of too many hands at once
Synchronization Keeping the cookie jar free of too many hands at once
28
Synchronization A concurrent joke: Why did the multithreaded chicken cross the road?
29
Synchronization A concurrent joke: Why did the multithreaded chicken cross the road? To side get to other the !
30
Synchronization A concurrent joke: Why did the multithreaded chicken cross the road? To side get to other the ! get side ! To to other the
31
Synchronization A concurrent joke: Why did the multithreaded chicken cross the road? To side get to other the ! get side ! To to other the eo gTts or ttthde ohei e!
32
Synchronization A concurrent joke: Why did the multithreaded chicken cross the road? To side get to other the ! get side ! To to other the eo gTts or ttthde ohei e! this joke never ends
33
Synchronization Concurrent activities usually require some degree of synchronization cars at an intersection employees working on a project aircraft landing at an airport deposits and withdrawals at a bank processes reading/writing from the same file threads executing within the same process
34
Synchronization Multiple paths of execution create multiple interleaving permutations of execution this means we must protect variables being accessed even for simple statements like x = x + 1 32-bit CPUs guarantees 32-bit read/write atomicity meaning 64-bit values (longs, doubles) cannot be read or written atomically!
35
Synchronization Concurrent synchronization is a balancing act
Safety: "Nothing bad ever happens" Mutual exclusion: no more than one process is ever present in a critical region No deadlock: no process is ever delayed awaiting an event that cannot occur Partial correctness: if a program terminates, the output is what is required Liveness: "Something will eventually happen" Fairness (weak): a process that can execute will be executed Reliable communication: a message sent by one process to another will be received Total correctness: a program terminates and the output is what is required
36
Patterns Patterns for safely representing/managing state:
Immutability: change nothing Locking: enforce access/mutual exclusion State dependence: define policies for actions that might affect state policies can include: blind action; inaction; balking; guarding; trying; retrying; timeout; planning Containment: structurally guarantee exclusive access by encapsulation Splitting: isolate independent aspects of state into parts
37
Synchronization Terminology
Working definitions for some synchronization-related terms
38
Synchronization Terminology
Atomic execution an operation defined to be atomic for purposes of concurrent scheduling smallest unit of execution available typically CPUs will guarantee some atomic operations store, read operations some manipulation operations: increment, decrement, etc up to word-sized values on the CPU other systems must document details
39
Synchronization Terminology
Data collision when two execution agents non-atomically access datum simultaneously typically the results are "undefined" agent A sets one half, agent B sets the other half with no guarantees as to which halves or agent A "wins", or agent B "wins" generally to be avoided!
40
Synchronization Terminology
Reentrancy when two execution agents can call into the same body of code no guarantees/description of safety; just that it can be reentrant code is not always concurrent code interesting recursion scenarios can cause reentrancy
41
Synchronization Terminology
Deadlock liveness violation: one or more execution agents cease execution ceased agents will never begin execution again easiest example: agent A holds lock "1" and attempts to acquire lock "2" agent B holds lock "2" and attempts to acquire lock "1"
42
Synchronization Terminology
Race condition two entities each seeking access no guarantees as to order effects will vary
43
Synchronization Terminology
Starvation an execution agent never gets an opportunity to run usually due to a failure to schedule for time example: agent has low priority ... and other agents always have higher priority ... and thus this agent never gets a chance to execute
44
Synchronization Terminology
Livelock liveness violation: an execution agent can never proceed with execution example: an agent is waiting on a lock but the lock is never available for acquisition
45
Synchronization Terminology
Some synchronization control terms note that these will differ in definition according to author, speaker, language, etc not a lot of variety between them in concept, however always check with the source when using a term
46
Synchronization Terminology
Critical section a.k.a. Mutex, Lock only one execution agent permitted into the guarded section of code
47
Synchronization Terminology
Semaphore typically a synch control that permits up to "N" execution agents simultaneous access usually a semaphore is a CritSec/Mutex with an associated count
48
Synchronization Terminology
Future a.k.a. Promise a placeholder for a value that is being computed asynchronously usually blocks until value is available/ready typically built around a single value that value can be a collection if needed/desirable, though
49
Making sure A happens before B
.NET Synchronization Making sure A happens before B
50
.NET Synchronization A sample concurrency problem:
write a thread that "pings" every five seconds, until asked to quit by the user
51
.NET Memory Models The CLR memory model
lists requirements regarding memory optimization allows threads to have per-thread value caches avoids having to check main heap memory also means multiple copies…
52
.NET Memory Models Volatile access
CIL includes a prefix ("volatile.") to guarantee cross-thread access System.Threading.Interlocked class provides basic numeric operations add, increment, subtract, decrement, compare, and so on System.Threading.Volatile has Read/Write methods System.Thread.MemoryBarrier also provides memory barrier semantics
53
Synchronization A sample concurrency problem
multiple threads access a shared data structure problem: individual put()s and get()s may be in the middle of bookkeeping when thread-switch occurs nodes could get lost nodes could get double-inserted existing list could get horribly corrupted
54
Mutual exclusion Monitors are a built-in CLR thread synch
every object in VM has an associated monitor lock block tells executing thread to attempt acquisition of indicated object's monitor thread blocks while waiting calling Monitor.Enter/Monitor.Exit causes thread to attempt to acquire monitor before continuing monitors also force synchronization of main heap and cached thread values
55
Mutual exclusion Monitor ownership
anyone can attempt to acquire that object's monitor at any time; in other words, concurrency behavior is not encapsulated alternative solution is to create private "lock" Object, and synchronize on that, instead be careful: simple synchronization of methods does not eliminate all concurrency bugs
56
Mutual exclusion Locking constructs
Mutex: mutual exclusion lock primitive Semaphore: permits a limited number of threads simultaneous access SemaphoreSlim: lighterweight version of Semaphore ReaderWriterLock: single writers/multiple readers ReaderWriterLockSlim: lighterweight version of Semaphore SpinLock: thread spins continuously, seeking to acquire the lock
57
Synchronization A sample concurrency problem
producer thread creates data for processing consumer thread processes created data only one data item can be ready at a time problem: how does producer not overwrite data? problem: how does consumer not process twice? problem: how do we have multiple producers and/or consumers?
58
Signalling Threads sometimes need to signal each other
AutoResetEvent: event is signalled when Set(), then resets ManualResetEvent: event is signalled when Set(), remains signalled until reset by hand CountdownEvent: event is signalled when countdown reaches zero
59
Collections CLR 4.0 introduced System.Collections.Concurrent
ConcurrentBag ConcurrentDictionary ConcurrentQueue ConcurrentStack Use these as often as possible! But if the above don't give you what you need... BlockingCollection: provides blocking/bounding capabilities for an IProducerConsumerCollection IProducerConsumerCollection: interface for a collection that is geared towards producer/consumer scenarios
60
Task Parallel Library (TPL)
What is it?
61
TPL Overview What is Task Parallel Library?
"simplifying the process of adding parallelism and concurrency to applications" provides a foundation for thinking about concurrency but without having to think about threads explicitly offers: handling the partitioning of the work scheduling of the threads on the ThreadPool cancellation support state management and other low-level details
62
TPL Overview Requirements .NET Framework 4+
basic understanding of threading and synchronization concepts thread execution locks, deadlocks, race conditions
63
TPL Overview TPL Contents Data parallelism Task parallelism
Pipeline parallelism/Dataflow
64
Taking lots of data and breaking it up into manageable chunks
Data Parallelism Taking lots of data and breaking it up into manageable chunks
65
Data Parallelism Imagine we have a million bank accounts...
... and we need to calculate interest for each one ... or we need to verify that each one has a positive balance ... or we need to find the checking accounts out of the lot
66
Data Parallelism This is "data parallelism"
essentially, the same kind of work applied over a large amount of data since each is effectively independent of the other (no shared results), it's trivial to parallelize data can be either treated individually or in smaller subsets partitioning the data source can be the biggest challenge key is no dependencies between the tasks or at least minimizing them; the more dependencies, the less parallelism
67
Data Parallelism Map-reduce Classic data-parallel operation
"map": transform the source data in some fashion Filtering out unwanted values Person object to a first-name/last-name tuple Database row into a subset of columns Floating-point number into an integer round-off "reduce": collapse multiple values into a single summation average count
68
Data Parallelism Count appearance of each word in a set of documents
emit (word, sum)
69
Data Parallelism Components to data parallelism Source data
Decomposition: how do we divide the data up into right-sized chunks (tasks) static: decompose based on data-agnostic criteria (number of processors, etc) dynamic: partition work "on-demand" or using some moment-sensitive criteria Assignment: assign tasks to execution units Orchestration: data access, communication and synchronization of execution units Mapping: execution primitives are bound to execution units
70
TPL Data Parallelism
71
Data Parallelism Let's imagine we have a million bank accounts...
... and we need to apply interest to each one
72
Data Parallelism The Bank class Bank {
public IList<Account> Accounts = new List<Account>(); public Bank(int numAccounts) var amountGenerator = new Random(); for (int i = 0; i < numAccounts; i++) Accounts.Add(new Account(amountGenerator.Next(50000))); } public override string ToString() var result = "Bank Ledger:\n"; foreach (var acct in Accounts) result += String.Format("\tAccount {0}: {1}\n", acct.ID, acct.Balance); return result; var bankOfTed = new Bank( ); // generate a Bank with a million accounts
73
Data Parallelism The Accounts class Account { static int count = 0;
public Account(int balance) ID = ++count; Balance = balance; } public int ID { get; private set; } public int Balance { get; set; } public override string ToString() return String.Format("Account {0}: {1}", ID, Balance);
74
Data Parallelism First attempt: no parallelism
foreach (var acct in bankOfTed.Accounts) { Console.WriteLine("Old balance is {0}", acct.Balance); acct.Balance += (int)(acct.Balance * 0.05); // 5% interest Console.WriteLine("New balance is {0}", acct.Balance); }
75
Data Parallelism Second attempt: Naively ThreadPooled (one account per block) CountdownEvent latch = new CountdownEvent(bankOfTed.Accounts.Count); foreach (var acct in bankOfTed.Accounts) { ThreadPool.QueueUserWorkItem(obj => Account account = (Account)obj; Console.WriteLine("Old balance is {0}", acct.Balance); account.Balance += (int)(account.Balance * 0.05); // 5% interest Console.WriteLine("New balance is {0}", acct.Balance); latch.Signal(); }, acct); }
76
Data Parallelism Third attempt: Statically-decomposed ThreadPooled
var procs = Environment.ProcessorCount; var chunkSize = (bankOfTed.Accounts.Count / procs) + 1; var segments = bankOfTed.Accounts.Split(chunkSize); CountdownEvent latch = new CountdownEvent(segments.Count); foreach (var seg in segments) { ThreadPool.QueueUserWorkItem(obj => IEnumerable<Account> segment = (IEnumerable<Account>)obj; foreach (var account in segment) Console.WriteLine("Old: {0}", account); account.Balance += (int)(account.Balance * 0.05); // 5% interest Console.WriteLine("New: {0}", account); } latch.Signal(); }, seg); latch.Wait();
77
Data Parallelism Parallel has static methods for mimicking common sequential operations Parallel.For mimics for loop using an index counter Parallel.ForEach mimics for-each loop over an IEnumerable each takes a range (start/end for For, IEnumerable for ForEach) and a function overloads offer different options/control ParallelLoopState: monitor/manipulate the loop ParallelOptions: loop option configuration thread-local data
78
Data Parallelism Simple data parallelism: Calculating directory size
long totalSize = 0; String[] files = Directory.GetFiles(dir); Parallel.For(0, files.Length, index => { FileInfo fi = new FileInfo(files[index]); long size = fi.Length; Interlocked.Add(ref totalSize, size); }); return new Tuple<int, long>(files.Length, totalSize);
79
Data Parallelism Calculating directory size with subdirectories
long totalSize = 0; String[] dirs = Directory.GetDirectories(dir); Parallel.For(0, dirs.Length, index => { DirectoryInfo di = new DirectoryInfo(dirs[index]); var result = CalculateDirectoryRecursive(dirs[index]); Interlocked.Add(ref totalSize, result.Item2); }); String[] files = Directory.GetFiles(dir); Parallel.For(0, files.Length, index => { FileInfo fi = new FileInfo(files[index]); long size = fi.Length; Interlocked.Add(ref totalSize, size); return new Tuple<int, long>(files.Length, totalSize);
80
Data Parallelism ParallelLoopState allows for monitoring and control
this isn't sequential looping can't break can't throw an exception ParallelLoopState is passed to looping function as parameter IsExceptional, IsStopped, ShouldExitCurrentIteration provide insight Break() instructs Parallel to cease execution of iterations beyond the current iteration at the earliest opportunity Stop() instructs Parallel to cease execution at the earliest opportunity
81
Data Parallelism What if we need more sophistication in decomposing the data source? perhaps it's not an indexable array or perhaps we don't have the complete data set yet Parallel.ForEach can take a function/lambda for doing the decomposition
82
You do the high road, I'll do the low road...
Task Parallelism You do the high road, I'll do the low road...
83
Task Parallelism Imagine we have a number of operations we want to run on some data... ... like averaging a bunch of numbers ... and finding the minimum of those numbers ... and the maximum of those numbers ... and plot a curve using those numbers
84
Task Parallelism This is "task parallelism"
also sometimes called "function parallelism" or "control parallelism" task parallelism uses multiple operations/tasks each with its own input no dependencies between the tasks, enabling each to run in parallel we can run these in parallel as a "fan-out" operation
85
Task Parallelsim One common approach is the "fork-join model"
combine results from subtasks
86
System.Threading.Tasks
Thinking concurrently without thinking about threads
87
System.Threading.Tasks
Tasks are not threads tasks will be executed by threads how tasks map to threads is abstracted away unless you really want to control it which most of the time you don't (or shouldn't) tasks wrap around methods/functions/lambdas
88
System.Threading.Tasks
Parallel.Invoke takes 1+ Action objects invokes each in parallel no return, just fire-and-forget Tasks are executed by the thread pool in other words, on a thread you do not create/control
89
System.Threading.Tasks
Parallel.Invoke static void ThreadedMessage(string msg, params object[] args) { Console.WriteLine("{0}({1}): {2}", Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId, String.Format(msg, args)); } Parallel.Invoke( () => ThreadedMessage("Hello, from a Task"), () => ThreadedMessage("Hello, from another task") );
90
System.Threading.Tasks
Explicit Task creation instantiate Task call Start() to set it up for scheduling/queueing call Wait() to wait for it to complete try to avoid waiting or use Task.WaitAll(Task[]) to wait for multiple tasks Task.Run() instantiates and Start()s all at once Task.Factory.StartNew() does the same
91
System.Threading.Tasks
Task creation Task t = new Task( () => ThreadedMessage("Hello") ); t.Start(); Task t2 = Task.Run( Task t3 = Task.Factory.StartNew( ThreadedMessage("Hello"); t.Wait(); t2.Wait(); t3.Wait(); // or Task.WaitAll(new Task[] { t, t2, t3 });
92
System.Threading.Tasks
Waiting for Tasks Wait() takes four overloads no-arg: wait indefinitely CancellationToken: wait until token is signaled int millis: wait until timeout TimeSpan: wait until timeout int millis, CancellationToken: wait until timeout or cancellation TIP: never "unbounded wait" See "Release It", by Nygard, for more details
93
System.Threading.Tasks
Task vs Task<TResult> Task returns void Task<TResult> returns a TResult (a Future/Promise) obtain result from Result property note that Result implicitly acts as a promise; if the result hasn't been computed yet, the calling thread blocks until a result is available to return TIP: never "unbounded wait"; Result can be an "unbounded wait", so Wait() then obtain Result
94
System.Threading.Tasks
Task<TResult> var t = Task.Run(() => { var random = new Random().NextDouble(); var ticks = new DateTime().Ticks; var result = ticks * random; ThreadedMessage("Calculated {0}", result); return result; }); Console.WriteLine("completed = {0}", t.IsCompleted); var res = t.Result; // this will block until t is completed Console.WriteLine("res = {0}, completed = {1}", res, t.IsCompleted);
95
System.Threading.Tasks
Data passed to a Task usually we rely on closure-capture rules but if those aren't desired (or understood)... ... Task can be passed an object parameter at creation this state can later be accessed through AsyncState property
96
System.Threading.Tasks
Task<TResult> string[] messages = { "One", "Two", "Three" }; Task[] tasks = new Task[3]; for (int i=0; i<messages.Length; i++) { var t = Task.Factory.StartNew( (Object obj) => Tuple<int, String> data = obj as Tuple<int, String>; ThreadedMessage("We are #{0} in line and our message is {1}", data.Item1, data.Item2); }, Tuple.Create(i, messages[i]) ); tasks[i] = t; } Task.WaitAll(tasks);
97
System.Threading.Tasks
Task Continuations "chain" Tasks: when one finishes, the next starts returned result of first task passed to next as Action parameter ContinueWith() can be called on antecedent Task to set up allows for a fluent style of code
98
System.Threading.Tasks
Task Continuations var generatePC = Task.Factory.StartNew( () => { Random rnd = new Random(); int[] dieRolls = new int[18]; for (int i = 0; i < dieRolls.Length; i++) dieRolls[i] = rnd.Next(1, 7); return dieRolls; }).ContinueWith( (task) => { int[] dieRolls = task.Result; var pc = new PlayerCharacter(); pc.Strength = dieRolls[0] + dieRolls[1] + dieRolls[2]; pc.Intelligence = dieRolls[3] + dieRolls[4] + dieRolls[5]; pc.Wisdom = dieRolls[6] + dieRolls[7] + dieRolls[8]; pc.Dexterity = dieRolls[9] + dieRolls[10] + dieRolls[11]; pc.Constitution = dieRolls[12] + dieRolls[13] + dieRolls[14]; pc.Charisma = dieRolls[15] + dieRolls[16] + dieRolls[17]; return pc; });
99
System.Threading.Tasks
Task Composition Task provides a few composing methods Task.WhenAll: Wait until all tasks passed have completed Task.WhenAny: Wait until any one of the tasks have completed Task.Delay: produces a Task object after a delay Task.FromResult: use a pre-computed task result all of these are syntactic sugar you could and/or should write them yourself (if you need different semantics)
100
System.Threading.Tasks
Canceling a Task construct a CancellationToken pass it in during the Task construction signal the CancellationToken to cancel Task will abort at earliest recognition
101
System.Threading.Tasks
Tasks and Exceptions exceptions will not propagate upwards outside of a Task remember, they're probably on separate threads; no shared stack Task will be in "Faulted" state if an exception wasn't handled exception object available via Exception property note that the exception thrown will be an AggregateException InnerExceptions will have the aggregated exception objects
102
System.Threading.Tasks
Child Tasks a Task that is created in the body of a Task's Action it can be of two forms: detached: no special relationship between parent and child attached: parent waits for child tasks to complete parent will propagate exceptions thrown by the child the parent's status depends on its children use TaskCreationOptions.AttachedToParent to attach a child Task
103
System.Threading.Tasks
TaskCreationOptions most Task-creating APIs overload to take a TaskCreationOptions parameter OR'ed values to combine possible values: PreferFairness: tasks created sooner are scheduled sooner, tasks created later scheduled later LongRunning: indicates task is "long-running, coarse-grained operation involving fewer, larger components than fine-grained systems" AttachedToParent, DenyChildAttach: whether task should be attached to a parent task HideScheduler: use the default scheduler, not the one associated with the Task
104
Crawl, walk, run, wait, run again, ...
Pipeline Parallelism Crawl, walk, run, wait, run again, ...
105
Pipeline Parallelism Imagine we have a bunch of bank accounts...
... positive-balance accounts need interest calculations ... negative-balance accounts need urgent s sent ... all accounts generate a marketing flyer ... and so on
106
Pipeline Parallelism This is called "pipeline parallelism"
sometimes also called "message parallelism" or "dataflow" each data element in the data source can be operated in parallel in essence, it's a combination of data and task parallelism tasks "chain" to one another dependent tasks do not run until fed data from the depending task
107
Pipeline Parallelism Pipeline parallelism vs Reactive Extensions (Rx)
many similarities reactive tends to be more "constant-stream" pipelines tend to be more "data-centric" however, signficant overlap between them means either is often a useful solution to a given problem
108
Data values moving through workflow in your code
TPL Dataflow Data values moving through workflow in your code
109
Dataflow Message-passing parallelism an actor-based programming model
uses in-process messaging coarse-grained dataflow and pipelining "useful for multiple operations that must communicate with one another asynchronously"" "... or when you want to process data as it becomes available"
110
Dataflow Programming model
CPU-intensive and I/O intensive apps w/high throughput/low latency set up "data flows" (hence the name) between processing agents rather than synchronizing access to data between functions this is not the same as programming objects and a whole different way of thinking about structuring programs
111
Take your LINQ across multiple threads
Parallel LINQ (PLINQ) Take your LINQ across multiple threads
112
PLINQ What if we added parallelism under the hood to LINQ?
essentially, add an "AsParallel" method this method sets up a new set of extension methods these new extension methods do all the same operations... Select() Where() OrderBy() ... and so on ... but using parallelism tactics This is PLINQ
113
Summary TPL is definitely not a child's toy
it provides some excellent parallelism tools maybe TOO easy to use keep in mind, parallel isn't always faster parallelize the obvious stuff ... data parallelism in particular ... but for the rest, measure measure measure back away from parallelism if performance suffers never use Thread directly again if you can possibly help it
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.