Visual Basic Code for .NET Parallel Extensions Video
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.
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
Imports System.Threading
Imports System.Threading.Tasks
Imports System.Collections.Concurrent
Module Module1
Sub Main()
While True
'Non-Parallel LINQ example
Console.WriteLine("Non-Parallel LINQ Example")
Console.WriteLine(Time(Sub()
Dim dict = (From i In Enumerable.Range(0, 2000000)
Where IsPrime(i)
Select i).ToDictionary(Function(x) x)
End Sub))
'Parallel LINQ example (PLINQ)
Console.WriteLine(vbCrLf & "Parallel LINQ Example")
Console.WriteLine(Time(Sub()
Dim dict = (From i In Enumerable.Range(0, 2000000).AsParallel
Where IsPrime(i)
Select i).ToDictionary(Function(x) x)
End Sub))
'Parallel, Ordered LINQ example
Console.WriteLine(vbCrLf & "Parallel & Ordered LINQ Example")
Console.WriteLine(Time(Sub()
Dim dict = (From i In Enumerable.Range(0, 2000000).AsParallel.AsOrdered
Where IsPrime(i)
Select i).ToDictionary(Function(x) x)
End Sub))
'Non-Parallel, Non-LINQ (uses a For loop)
Console.WriteLine(vbCrLf & "Non-Parallel & Non-LINQ Example (uses a For loop)")
Console.WriteLine(Time(Sub()
Dim primes = New Queue(Of Integer)
For i = 0 To 2000000
If IsPrime(i) Then primes.Enqueue(i)
Next
End Sub))
'Parallel, Non-LINQ (uses Parallel.For, which is part of the Parallel Task Library in System.Threading.Tasks).
Console.WriteLine(vbCrLf & "Parallel For Example")
Console.WriteLine(Time(Sub()
'Queue is not thread safe.
'Dim primes = New Queue(Of Integer)
'Instead use ConcurrentQueue, which is a new .NET 4.0 collection in System.Collections.Concurrent.
Dim primes = New ConcurrentQueue(Of Integer)
Parallel.For(0, 2000000, Sub(i)
If IsPrime(i) Then primes.Enqueue(i)
End Sub)
End Sub))
Console.ReadLine()
'BlockingCollection Example
Dim primes2 = New BlockingCollection(Of Integer)
Dim t As Task = Task.Factory.StartNew(Sub()
For Each prime In primes2.GetConsumingEnumerable()
Console.WriteLine(prime)
Next
End Sub)
For i = 0 To 2000000
If IsPrime(i) Then primes2.Add(i)
Next
primes2.CompleteAdding()
t.Wait()
Console.ReadLine()
End While
End Sub
Function IsPrime(ByVal valueToTest As Integer)
If valueToTest < 2 Then Return False
Dim bound As Integer = Math.Sqrt(valueToTest)
For i = 2 To bound
If valueToTest Mod i = 0 Then Return False
Next
Return True
End Function
Function Time(ByVal a As Action) As TimeSpan
Dim sw As Stopwatch = Stopwatch.StartNew()
a()
Return sw.Elapsed()
End Function
End Module