Parallel Extensions
Parallel Extensionswas the development name for amanagedconcurrencylibrarydeveloped by a collaboration betweenMicrosoft Researchand theCLRteam atMicrosoft.The library was released in version 4.0 of the.NET Framework.[1]It is composed of two parts:Parallel LINQ(PLINQ) andTask Parallel Library(TPL).[2][3]It also consists of a set ofcoordination data structures(CDS) – sets ofdata structuresused to synchronize and co-ordinate the execution of concurrent tasks.[4]
Parallel LINQ
[edit]PLINQ,orParallelLINQ,parallelizing the execution of queries on objects (LINQ to Objects) and XML data (LINQ to XML). PLINQ is intended for exposingdata parallelismby use of queries.[2]Any computation on objects that has been implemented as queries can be parallelized by PLINQ. However, the objects need to implement theIParallelEnumerable
interface, which is defined by PLINQ itself. Internally it usesTPLfor execution.[4][5]
Task Parallel Library
[edit]TheTask Parallel Library(TPL) is thetask parallelismcomponent of the Parallel Extensions to.NET.[6]It exposes parallel constructs like parallelFor
andForEach
loops, using regular method calls anddelegates,thus the constructs can be used from anyCLI languages.The job of spawning and terminatingthreads,as well as scaling the number of threads according to the number of available processors, is done by the library itself,[3]using awork stealingscheduler.[7]
TPL also includes other constructs likeTaskandFuture.ATaskis an action that can be executed independent of the rest of the program. In that sense, it is semantically equivalent to a thread, except that it is a more light-weight object and comes without the overhead of creating an OS thread. Tasks are queued by aTask Managerobject and are scheduled to run on multiple OS threads in athread poolwhen their turn comes.
Futureis a task that returns a result. The result is computed in a background thread encapsulated by theFutureobject, and the result is buffered until it is retrieved.[3]If an attempt is made to retrieve the result before it has been computed then the requesting thread will block until the result is available.[6]
The other construct of TPL isParallelclass. TPL provides a basic form of structured parallelism via three static methods in the Parallel class:
- Parallel.Invoke
- Executes an array of Action delegates in parallel, and then waits for them to complete
- Parallel.For
- Parallel equivalent of a C# for loop
- Parallel.ForEach
- Parallel equivalent of a C#foreach loop
Architecture
[edit]The main concept in the Parallel Extensions to.NET is aTask
,which is a small unit of code, usually represented as alambda function,that can be executed independently. Both PLINQ and the TPL API provides methods to create the Tasks – PLINQ divides a query into smaller Tasks, and theParallel.For
,Parallel.ForEach
andParallel.Invoke
methods divide a loop into Tasks.
PFX includes aTask Manager
object which schedules the Tasks for execution. A Task Manager contains a globalqueueof Tasks, which are then executed. It also encapsulates multiplethreadsonto which the Tasks are executed. By default, as many threads as there are processors (or processor cores) on the system are created, though this number may be manually modified. Each thread is associated with a thread-specific queue of Tasks. When idle, each thread picks up a batch of Tasks and puts them on its local queue, where they are then executed, one by one. If the global queue is empty, a thread will look for Tasks in the queues of its peers, and will take the Tasks which have been in the queue the longest (task stealing). When in execution, the Tasks will be executed independently, with the change in state of one Task independent of others. As a result, if they use a shared resource, they still need to be synchronized manually using locks or other constructs.
See also
[edit]- Concurrency and Coordination Runtime
- Joins
- Cilk/Cilk Plus– comparable technology for C and C++
- Grand Central Dispatch– comparable technology inMac OS X 10.6developed byApple.
- Java Concurrency– comparable technology inJava(also known asJSR 166).
- Threading Building Blocks(TBB) – comparable technology for C++ available for many systems created originally by Intel (also open source)
- Thread pool pattern
- Task parallelism
- ReactiveX(Reactive Extensions)
References
[edit]- ^"What's New in the.NET Framework 4".Retrieved21 September2011.
- ^ab"Programming in the Age of Concurrency: Concurrent Programming with PFX".Retrieved16 October2007.
- ^abc"MSDN Magazine: Task Parallel Library".Archived fromthe originalon 14 October 2007.Retrieved16 October2007.
- ^ab"June 2008 CTP – Parallel Extensions to the.NET FX".Retrieved6 August2008.
- ^"More powerful aggregations in PLINQ".Retrieved6 August2008.
- ^abDuffy, Joe (2009).Concurrent Programming on Windows.pp. 887–929.ISBN978-0321434821.
- ^Leijen, Daan; Schulte, Wolfram; Burckhardt, Sebastian (2009). "The Design of a Task Parallel Library".ACM SIGPLAN Notices.44(10): 227.CiteSeerX10.1.1.146.4197.doi:10.1145/1639949.1640106.
External links
[edit]![](https://upload.wikimedia.org/wikipedia/commons/thumb/d/df/Wikibooks-logo-en-noslogan.svg/40px-Wikibooks-logo-en-noslogan.svg.png)