Using the Parallel class

The System.Threading class has another class named Parallel. This class provides parallel implementations for For and ForEach loops. Their implementation is similar to the sequential loop. When you use ParallelFor or ParallelForEach, the system automatically splits the process into multiple tasks and acquires locks if required. All of this low-level work is handled by TPL.

A sequential loop may look as follows:

foreach (var item in sourceCollection) 
{
Process(item);
}

The same loop can be represented using Parallel as follows:

Parallel.ForEach(sourceCollection, item => Process(item)); 

TPL manages the data source and creates partitions so that the loop can operate on multiple parts in parallel. Each task will be partitioned by the task scheduler as per system resources and workload. Then, if the workload becomes unbalanced, the work will be redistributed into multiple threads and processes by the task scheduler.

Parallel programming can increase performance when you have a lot of work to be done in parallel. If this isn't the case, it can become a costly affair. 

It is important to understand how parallelism works in a scenario given. In the following example, we'll look at how we can use Parallel.For and make a time comparison between sequential and parallel loops.

Here, we are defining an array of integers and calculating the sum and product of each element of the array. In the main program, we invoke this method using sequential and parallel loops and calculate how much time each loop takes to complete the process:

static int[] _values = Enumerable.Range(0, 1000).ToArray();

private static void SumAndProduct(int x)
{
int sum = 0;
int product = 1;
foreach (var element in _values)
{
sum += element;
product *= element;
}
}

public static void CallSumAndProduct()
{
const int max = 10;
const int inner = 100000;
var s1 = Stopwatch.StartNew();
for (int i = 0; i < max; i++)
{
Parallel.For(0, inner, SumAndProduct);
}
s1.Stop();

Console.WriteLine("Elapsed time in seconds for ParallelLoop: " + s1.Elapsed.Seconds);

var s2 = Stopwatch.StartNew();
for (int i = 0; i < max; i++)
{
for (int z = 0; z < inner; z++)
{
SumAndProduct(z);
}
}
s2.Stop();

Console.WriteLine("Elapsed time in seconds for Sequential Loop: " + s2.Elapsed.Seconds );
}

In the preceding code, we executed two loops: one using a parallel loop and the other using a sequential loop. The results show the time each operation took:

System.Threading.Tasks.Parallel comes with multiple helper classes, such as ParallelLoopResult, ParallelLoopState, and ParallelOptions.

ParallelLoopResult provides the completion status of the parallel loop, as shown here:

ParallelLoopResult result = Parallel.For(int i, ParallelLoopState loopstate) =>{});

ParallelLoopState allows iterations of parallel loops to interact with other iterations. Finally, LoopState allows you to identify any exceptions in iterations, break from an iteration, stop an iteration, identify if any iteration has invoked break or stop, and break long-running iterations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.141.219