Here’s a great video with Stephen Toub and Jason Olson that gives a 15min overview of what’s new for managed developers doing parallel programming with .NET 4.0.

Video: Using the Parallel Extensions to the .NET Framework

Welcome back to another Visual Studio 2010 and .NET Framework 4.0 Week video. In this latest installment, we catch up with Stephen Toub, Senior Program Manager on the Parallel Computing Platform team. Stephen takes us through a whirlwind tour of many different Parallel Extensions features, showing them in action. Covered are features like Parallel LINQ, Parallel.For, the Task Parallel Library, and some of the new Coordination Data Structures.

image

I’ve included a VB version of the code below, in case you want to follow along in Visual Basic. I’ve also attached the solution at the end of this post. The solution runs on Visual Studio 2010 Beta 2.

Code Snippet
  1. Imports System.Threading
  2. Imports System.Threading.Tasks
  3. Imports System.Collections.Concurrent
  4. Module Module1
  5.     Sub Main()
  6.         While True
  7.             'Non-Parallel LINQ example
  8.             Console.WriteLine("Non-Parallel LINQ Example")
  9.             Console.WriteLine(Time(Sub()
  10.                                        Dim dict = (From i In Enumerable.Range(0, 2000000)
  11.                                                   Where IsPrime(i)
  12.                                                   Select i).ToDictionary(Function(x) x)
  13.                                    End Sub))
  14.             'Parallel LINQ example (PLINQ)
  15.             Console.WriteLine(vbCrLf & "Parallel LINQ Example")
  16.             Console.WriteLine(Time(Sub()
  17.                                        Dim dict = (From i In Enumerable.Range(0, 2000000).AsParallel
  18.                                                   Where IsPrime(i)
  19.                                                   Select i).ToDictionary(Function(x) x)
  20.                                    End Sub))
  21.             'Parallel, Ordered LINQ example
  22.             Console.WriteLine(vbCrLf & "Parallel & Ordered LINQ Example")
  23.             Console.WriteLine(Time(Sub()
  24.                                        Dim dict = (From i In Enumerable.Range(0, 2000000).AsParallel.AsOrdered
  25.                                                   Where IsPrime(i)
  26.                                                   Select i).ToDictionary(Function(x) x)
  27.                                    End Sub))
  28.             'Non-Parallel, Non-LINQ (uses a For loop)
  29.             Console.WriteLine(vbCrLf & "Non-Parallel & Non-LINQ Example (uses a For loop)")
  30.             Console.WriteLine(Time(Sub()
  31.                                        Dim primes = New Queue(Of Integer)
  32.                                        For i = 0 To 2000000
  33.                                            If IsPrime(i) Then primes.Enqueue(i)
  34.                                        Next
  35.                                    End Sub))
  36.             'Parallel, Non-LINQ (uses Parallel.For, which is part of the Parallel Task Library in System.Threading.Tasks).
  37.             Console.WriteLine(vbCrLf & "Parallel For Example")
  38.             Console.WriteLine(Time(Sub()
  39.                                        'Queue is not thread safe.
  40.                                        'Dim primes = New Queue(Of Integer)
  41.                                        'Instead use ConcurrentQueue, which is a new .NET 4.0 collection in System.Collections.Concurrent.
  42.                                        Dim primes = New ConcurrentQueue(Of Integer)
  43.                                        Parallel.For(0, 2000000, Sub(i)
  44.                                                                     If IsPrime(i) Then primes.Enqueue(i)
  45.                                                                 End Sub)
  46.                                    End Sub))
  47.             Console.ReadLine()
  48.             'BlockingCollection Example
  49.             Dim primes2 = New BlockingCollection(Of Integer)
  50.             Dim t As Task = Task.Factory.StartNew(Sub()
  51.                                                       For Each prime In primes2.GetConsumingEnumerable()
  52.                                                           Console.WriteLine(prime)
  53.                                                       Next
  54.                                                   End Sub)
  55.             For i = 0 To 2000000
  56.                 If IsPrime(i) Then primes2.Add(i)
  57.             Next
  58.             primes2.CompleteAdding()
  59.             t.Wait()
  60.             Console.ReadLine()
  61.         End While
  62.     End Sub
  63.     Function IsPrime(ByVal valueToTest As Integer)
  64.         If valueToTest < 2 Then Return False
  65.         Dim bound As Integer = Math.Sqrt(valueToTest)
  66.         For i = 2 To bound
  67.             If valueToTest Mod i = 0 Then Return False
  68.         Next
  69.         Return True
  70.     End Function
  71.     Function Time(ByVal a As Action) As TimeSpan
  72.         Dim sw As Stopwatch = Stopwatch.StartNew()
  73.         a()
  74.         Return sw.Elapsed()
  75.     End Function
  76. End Module