Introducing .net parallel programming
This is about the parallel programming features of .NET 4, specifically the Task Parallel Library (TPL), Parallel LINQ, and the legion of support classes that make writing parallel programs with C# simpler and easier than ever before. With the widespread use of multiprocessor and multicore computers, parallel programming has gone mainstream. Or it would have, if the tools and skills required had been easier to use and acquire. Microsoft has responded to the need for a better way to write parallel programs with the enhancements to the .NET framework .NET has had support for parallel programming since version 1.0, now referred to as classic threading, but it was hard to use and made you think too much about managing the parallel aspects of your program, which detracts from focusing on what needs to be done. The new .NET parallel programming features are built on top of the classic threading support. The difference between the TPL and classic threading becomes apparent when you consider the basic programming unit each uses. In the classic model, the programmer uses threads. Threads are the engineof execution, and you are responsible for creating them, assigning work to them, and managing their existence. In the classic approach, you create a little army to execute your program, give all the soldiers their orders, and keep an eye on them to make sure they do as they were told. By contrast, the basic unit of the TPL is the task, which describes something you want done. You create tasks for each activity you want performed, and the TPL takes care of creating threads and dealing with them as they undertake the work in your tasks. The TPL is task-oriented, while the classic threading model is worker-oriented. Tasks let you focus primarily on what problem you want to solve instead of on the mechanics of how it will get done. If you have tried parallel programming with classic threads and given up, you will find the new features have a refreshing and enabling approach. You can use the new features without having to know anything about the classic features. You’ll also find that the new features are much better thought out and easier to use. Understanding the benefits (and pitfalls) of parallel programming
Parallel computing is, at heart, a performance play. The work that a program performs is broken up into pieces, which are performed by multiple cores, processors, or computers. Some of those pieces of work will be performed at the same time, that is, in parallel, orconcurrently, which is where the two key terms for this kind of programming arise. Writing the code that breaks up and arranges for parallel computing is called parallel programming. If you have a multicore or multi-processor machine, spreading the pieces of work across them can reduce the amount of time to complete the work overall. The key phrase here is can reduce; there are some caveats that you should be aware. - ConsideringOverhead
- Coordinating Data
- Scaling Applications
1.Considering Overhead Parallel execution doesn’t come for free. There are overhead costs associated with setting up and managing parallel programming features. If you have only a small amount of work to perform, the overhead can outweigh the performance benefit. 2.Coordinating Data Applying coordination is not hard, but applying just the right amount is a trick that comes with forethought and experience. Too much coordination compromises the performance of your parallel program; too little gets you unexpected results. 3.Scaling Applications Adding a second core or CPU might increase the performance of your parallel program, but it is unlikely to double it. Likewise, a four-core machine is not going to execute your parallel program four times as quickly — in part because of the overhead and coordination described in the previous sections. However, the design of the computer hardware also limits its ability to scale. You can expect a significant improvement in performance, but it won’t be 100 percent per additional core, and there will almost certainly be a point at which adding additional cores or CPUs doesn’t improve the performance at all. Deciding when to go parallel
My advice for assessing if a problem can be parallelized successfully is to just give it a try and measure the results. If a problem is difficult to write a parallel solution for, you will find out pretty quickly. If the problem can be parallelized but is affected by one or more of the caveats in the previous section, you can make an informed decision as to whether to use the parallel version or stick with the sequential implementation. Either way, you’ll have increased your exposure to, and experience with, parallel programming. The key is measurement. Don’t just assume that a parallel solution will give you better performance and move on. Deciding when to stay sequential
It may seem odd to emphasize the value of sequential execution in a book about parallel programming, but effective parallel programmers know when to leave well enough alone. Some problems are inherently sequential in nature—there are no pieces of work that can be performed concurrently. Some problems require so much coordination that the overhead incurred by parallel execution cancels out the performance gains. Some problems come with a mass of legacy code that would require too much rewriting to integrate with parallel code. One of the most important times to consider sequential execution is when something is wrong with your parallel code and you can’t work out why. There are some new parallel features in the Visual Studio 2010 debugger that can be very helpful in tracking down bugs, but sometimes you need to go back to the basics to make sure that you are able to code a solution that works at all. Getting the example code
You should already know how to write C# code and use Visual Studio to create, compile, and run .NET applications in C#. You need Visual Studio 2010 and .NET 4 for this. You can get the source code for all of the examples. There is a different Visual Studio solution for each chapter and each listing is contained in a separate project. Shows you how this appears in Visual Studio 2010 The example code for Visual Studio 2010 
To run a listing, right-click the project in the Solution Explorer window, and select Set As Startup Project. Once you have selected the project you want, press Ctrl+F5 to compile and run the code. Selecting the startup project  |