Parallel Programming with .NET Parallel Extensions
- Posted: May 05, 2009 at 1:54 PM
- 4,339 Views
- 1 Comment
Download
How do I download the videos?
- To download, right click the file type you would like and pick “Save target as…” or “Save link as…”
Why should I download videos from Channel9?
- It's an easy way to save the videos you like locally.
- You can save the videos in order to watch them offline.
- If all you want is to hear the audio, you can download the MP3!
Which version should I choose?
- If you want to view the video on your PC, Xbox or Media Center, download the High Quality WMV file (this is the highest quality version we have available).
- If you'd like a lower bitrate version, to reduce the download time or cost, then choose the Medium Quality WMV file.
- If you have a Zune, WP7, iPhone, iPad, or iPod device, choose the low or medium MP4 file.
- If you just want to hear the audio of the video, choose the MP3 file.
Right click “Save as…”
- WMV (WMV Video)
This video demonstrates the use of Parallel Extensions to the .NET Framework. Parallel Extensions requires .NET 3.5 and the final version will be part of the .NET 4.0 Framework when released. These extensions introduce new classes and functionality into
the System.Threading namespace. It's now considerably easier to parallelize a code block without creating explicit threads.
Windows Server 2008 R2 and Windows 7 introduce new system-level features (e.g. User-Mode-Scheduling) that both the C++ Concurrency Runtime and the .NET Parallel Extensions extend. Developers may easily target current and legacy platforms using the new runtime
and extensions.
First, a project is shown that counts the number of primes up to a given upper bound. Each number is checked by seeing if it’s divisible by any number between 2 and its square root. Each prime number found increases the count. The main control is a standard for loop.
Next, the same code is converted to work with the new Parallel.For() method. This takes the lower and upper bound, and a delegate for the action to take. Using a Lamda expression, the original loop body is made to work using i => {} syntax. When this code is run, it is clear that a bug has surfaced in the form of a bad prime count. This is actually a common multithreading bug, stemming from the use of the increment operator, ++. Using the Interlocked.Increment() method fixes the problem.
The next demo starts with a LINQ query and converts to a Parallel LINQ, or PLINQ, query. After demonstrating the original loop which uses Enumerable.Range() to generate a range of numbers, it is shown that ParallelEnumerable.Range() creates an enumerator that is automatically multi-threaded.
Finally, to demonstrate a more general-usage solution, any IEnumerable
collection can easily be parallelized using the expression method AsParallel(). Thus, a collection
students of type List<Student> could be used in a LINQ as a parallel query by simply using it as
students.AsParallel().
Learn more about Parallel Computing on Windows at the
MSDN Concurrency Dev Center.
Comments Closed
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Wonderfull presentation, very helpful, covers the basics of parallel extension with detailed examples.
Remove this comment
Remove this thread
close