- •Introduction to the Parallel Extensions Library
- •Objectives
- •System Requirements
- •Exercises
- •Next Step:
- •Figure 2
- •Figure 4
- •Parallel.Invoke(delegate { WalkTree(node.Left); }, delegate { WalkTree(node.Right); });
- •Visual Basic
- •Parallel.Invoke(Sub() WalkTree(node.Left), Sub() WalkTree(node.Right))
- •Figure 6
- •Next Step:
- •Figure 7
- •Figure 11
- •Next Step:
- •Figure 12
- •Next Step:
- •Figure 13
- •Figure 14
- •Figure 16
- •Next Step:
Figure 14
Output from a parallelized LINQ query
Note: The operations are executed in parallel with as many operations occurring concurrently as the number of physical cores will allow.
Task 2 – Using the ParallelEnumerable Class’ Extension Methods to Parallelize LINQ
As mentioned earlier, a more succinct way to take advantage of the Enumerable and ParallelEnumerable classes’ static LINQ methods is to use them as Extension methods.
Converting a non-parallelized LINQ query implemented using extension methods to a PLINQ query is straight forward. Replace the PLINQ query in the Main() method to match the following LINQ query:
C#
static void Main(string[] args)
{
...
// Methods to call
Ex4Task2_Extensions();
...
}
Visual Basic
Sub Main(ByVal args() As String)
...
' Methods to call
Ex4Task2_Extensions()
...
End Sub
Add the Ex4Task2_Extensions() method to Program.cs (C#) or Module1.vb (Visual Basic):
(Code Snippet – Intro to Parallel Extensions Lab - Ex4 Ex4Task2_Extensions CSharp)
C#
private static void Ex4Task2_Extensions()
{
var q = employeeData.
Where(x => x.EmployeeID % 2 == 0).OrderBy(x => x.EmployeeID)
.Select(x => PayrollServices.GetEmployeeInfo(x))
.ToList();
foreach (var e in q)
{
Console.WriteLine(e);
}
}
(Code Snippet – Intro to Parallel Extensions Lab - Ex4 Ex4Task2_Extensions VB)
Visual Basic
Private Sub Ex4Task2_Extensions()
Dim q = employeeData _
.Where(Function(x) x.EmployeeID Mod 2 = 0) _
.OrderBy(Function(x) x.EmployeeID) _
.Select(Function(x) PayrollServices.GetEmployeeInfo(x)) _
.ToList()
For Each e In q
Console.WriteLine(e)
Next e
End Sub
Note: Again, the ToList() used to execute the LINQ query immediately rather than waiting for it to execute when the IEnumerable<T> returned by Select() is iterated over during the foreach that comes later. You are avoiding Delayed Execution.
Build and run the application.
You should observe that the LINQ query performs the operations in order of the Employee ID. Also observe the total amount of time required to complete the work (the exact time required will vary):
Figure 15
Output from non-parallelized LINQ query with extension methods
To parallelize this LINQ query just replace the current method call from Main(), with the following one.
C#
static void Main(string[] args)
{
...
// Methods to call
Ex4Task2_ConvertToParallelExtensions();
...
}
Visual Basic
Sub Main(ByVal args() As String)
...
' Methods to call
Ex4Task2_ConvertToParallelExtensions()
...
End Sub
Add the Ex4Task2_ConvertToParallelExtensions() method to Program.cs (C#) or Module1.vb (Visual Basic):
(Code Snippet – Intro to Parallel Extensions Lab - Ex4 Ex4Task2_ConvertToParallelExtensions CSharp)
C#
private static void Ex4Task2_ConvertToParallelExtensions()
{
var q = employeeData.AsParallel()
.Where(x => x.EmployeeID % 2 == 0).OrderBy(x => x.EmployeeID)
.Select(x => PayrollServices.GetEmployeeInfo(x))
.ToList();
foreach (var e in q)
{
Console.WriteLine(e);
}
}
(Code Snippet – Intro to Parallel Extensions Lab - Ex4 Ex4Task2_ConvertToParallelExtensions VB)
Visual Basic
Private Sub Ex4Task2_ConvertToParallelExtensions()
Dim q = employeeData.AsParallel() _
.Where(Function(x) x.EmployeeID Mod 2 = 0) _
.OrderBy(Function(x) x.EmployeeID) _
.Select(Function(x) PayrollServices.GetEmployeeInfo(x)) _
.ToList()
For Each e In q
Console.WriteLine(e)
Next e
End Sub
Build and run the application.
You should observe that like the LINQ query based on the ParallelEnumerable static class, the PLINQ query implemented as extension methods no longer performs operations in order by EmployeeID. Instead the operations are executed in parallel with as many operations occurring concurrently as the number of physical cores will allow. Also note that as in the previous parallelize LINQ example, the parallelized version completes in roughly half the time as the non-parallelized version.
