Parallel Programming with .NET Parallel Extensions
- Posted: May 05, 2009 at 1:54 PM
- 5,073 Views
- 1 Comment
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
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 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
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
Wonderfull presentation, very helpful, covers the basics of parallel extension with detailed examples.
Remove this comment
Remove this thread
close