ENTERPRISE

Parallel Programming : Introducing Parallel Computing

10/10/2010 3:44:35 PM
The .NET Framework 4.0 provides support for parallel computing through the Task Parallel Library (also referred to as TPL), which is a set of APIs offered by specific extensions of the System.Threading.dll assembly. The reference to this assembly is included by default when creating new projects, so you do not need to add one manually. The TPL is reachable via the System.Threading and System.Threading.Tasks, namespaces that provide objects for scaling work execution over multiple processors. Basically you write small units of work known as tasks. Tasks are scheduled for execution by the TPL’s Task Scheduler, which is responsible for executing tasks according to available threads. This is possible because the Task Scheduler is integrated with the .NET Thread Pool. The good news is that the .NET Framework can automatically take advantage of all available processors on the target machines without the need to recompile code.

Note

Parallel computing makes it to easier to scale applications over multiple processors, but it remains something complex in terms of concepts. This is because you will face again some threading concepts, such as synchronization locks, deadlocks, and so on. The suggestion is to have at least a basic knowledge of threading issues before writing parallel code. Another important consideration is when should you use parallel computing? The answer is not easy because you are the only one who knows how your applications consume resources. The general rule is that parallel computing gives the best results when you have intensive processing scenarios. In simpler elaborations, parallel computing is not necessarily the best choice and can cause performance loss. Use it when your applications require hard CPU loops.


Most of the parallel API are available through the System.Threading.Tasks.Task and System.Threading.Tasks.Parallel classes. The first one is described in detail later, whereas we now provide coverage of the most important classes for parallelism.

Introducing Parallel Classes

Parallelism in the .NET Framework 4.0 is possible due to a number of classes, some responsible for maintaining the architecture of the TPL and some for performing operations in a concurrent fashion. The following subsection provides a brief coverage of the most important classes, describing their purpose.

The Parallel Class

The System.Threading.Tasks.Parallel class is one of the most important classes in parallel computing, because it provides shared methods for running concurrent tasks and for executing parallel loops. In this chapter you can find several examples of usage of this class; for now you just need to know that it provides the Invoke, For, and ForEach shared methods. The first one enables running multiple tasks concurrently, whereas the other ones enable executing loops in parallel.

The TaskScheduler Class

The System.Threading.Tasks.TaskScheduler class is responsible for the low-level work of sending tasks to the thread queue. This means that when you start a new concurrent task, the task is sent to the scheduler that checks for thread availability in the .NET thread pool. If a thread is available, the task is pushed into the thread and executed. Generally you do not interact with the task scheduler. (The class exposes some members that you can use to understand the tasks state.) The first property is Current, which retrieves the instance of the running task scheduler. This is required to access information. For example, you can understand the concurrency level by reading the MaximumConcurrencyLevel property as follows:

Console.WriteLine("The maximum concurrency level is {0}",
TaskScheduler.Current.MaximumConcurrencyLevel)

There are also some protected methods that can be used to force tasks’ execution (such as QueueTask and TryDequeue) but these are accessible if you want to create your custom task scheduler, which is beyond of the scope in this chapter.

The TaskFactory Class

The System.Threading.Tasks.TaskFactory class provides support for generating and running new tasks and is generally exposed as a shared property of the TaskStartNew method, which enables creating a new task and automatically starting it. class, as explained in the next section about tasks. The most important member is the

The ParallelOptions Class

The System.Threading.Tasks.ParallelOptions class provides a way for setting options on tasks’ creation. Specifically it provides properties for setting tasks’ cancellation properties (CancellationToken), the instance of the scheduler (TaskScheduler), and the maximum number of threads that a task is split across (MaxDegreeOfParallelism).

Other  
 
Top 10
Stream And Watch Your Movies Anywhere
Epos Epic 5 – Class-Leader Floor-Standing Speaker
Share Photos From Your Android Phone
Google Nexus 7 8GB - Tablet
Android 4.0 For Your Samsung Galaxy SII
A Potentially Perfect Partnership
Tips & Tricks Of November 2012 (Part 4)
Tips & Tricks Of November 2012 (Part 3)
Tips & Tricks Of November 2012 (Part 2)
Tips & Tricks Of November 2012 (Part 1)
Most View
iPhone 3D Programming : Adding Textures to ModelViewer (part 3) - Enabling Textures with ES1::RenderingEngine
Buying Guide: CPU Cooling Equipment (Part 4) - Thermaltake BigWater 760 Plus,Thermaltake Frio OCK,Zalman CNPS20LQ
10 best products of the past 200 issues (part 2)
Visual Studio 2010 : Understanding Solutions and Projects (part 2)
Configuring A Child - Friendly iPad
Zyxel PLA4205 - Fastest Homeplug AV kit
iOS 6 Beta Review (Part 2)
Windows Server 2008 Server Core : Working with Scripts - Scripting Networking Solutions with the NetSH Utility
Tablets Comparison (Part 2) - Tablets – OS - Android 4.1 Jelly Bean, Apple iOS 6, Windows 8 RT
What we expect: Windows 8 Tablets
Mobile Commerce Applications, Part 2
Programming with DirectX : Shading and Surfaces - Types of Textures
Network Attached Storage Round-Up (Part 1) - The Benefits Of A NAS
New Memory Card XQD 2.0 Delivers 1,000MB/S Transfer Rate
Exchange Server 2007 : Configure the Client Access Server - Enable POP3 and IMAP4
Preparing Your Windows 8 PC : Refreshing or Resetting Your PC
Navigating Architecture Changes in Windows Vista
Buffer Restrictions
XNA Game Studio 4.0 : Gamer Services - Gamers and Profiles
Understanding the Basics of Collaboration in SharePoint 2010 : Working with Lists and Libraries (part 3) - Managing List Columns