Using the Parallel Class in C# – Processing Data in Parallel across Multiple Processors or Cores

The new Task Parallel Library introduced with .NET 4, allows you to easily split code execution onto multiple processors without using threads or locks.

For a task which is easily split into independent sub-tasks, or data that can be partitioned and computed separately you can use the Parallel class in System.Threading to assign tasks to be automatically scheduled and then wait for the tasks to be completed. The Parallel class will automatically scale to the number of available processors or cores.

Process Data in Parallel across Multiple Processors or Cores

When you have a data set which can be split over multiple processors and then processed independently, you should use constructs such as Parallel.For(), in the below example this is demonstrated for computing prime numbers:

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace  PrimesDemo
{
class Program
{
static void Main(string[] args)
{
int maxPrimes = 1000000;
int maxNumber = 200000000;
long primesFound = 0;
Console.WriteLine(“Iterative”);
Stopwatch watchObj = new Stopwatch();
watchObj.Start();

for (UInt32 i = 0; i < maxNumber; ++i) { if (IsPrime(i)) { Interlocked.Increment(ref primesFound); if (primesFound > maxPrimes)
{
Console.WriteLine(“Last prime found was: {0:N0}”,
i);
break;
}
}
}
watchObj.Stop();
Console.WriteLine(“Found {0:N0} prime numbers in {1}”,
primesFound, watchObj.Elapsed);
watchObj.Reset();
primesFound = 0;
Console.WriteLine(“Parallel”);
watchObj.Start();
// to stop the loop, there is an overload that takes Action
Parallel.For(0, maxNumber, (i, loopState) =>
{
if (IsPrime((UInt32)i))
{
Interlocked.Increment(ref primesFound);
if (primesFound > maxPrimes)
{
Console.WriteLine(“Last prime found was: {0:N0}”,
i);
loopState.Stop();
}
}
});
watchObj.Stop();
Console.WriteLine(“Found {0:N0} prime numbers in {1}”,
primesFound, watchObj.Elapsed);
Console.ReadKey();
}
public static bool IsPrime(UInt32 number)
{

//check for evenness
if (number % 2 == 0)
{
if (number == 2)
return true;
return false;
}

UInt32 max = (UInt32)Math.Sqrt(number);
for (UInt32 i = 3; i <= max; i += 2)
{
if ((number % i) == 0)
{
return false;
}
}
return true;
}
}
}

When you run this note the drastically reduced time using Parallel, also note that the results are not necessarily in order and that the parallel evaluation of the data does not arrive at the same results as the iterative evaluation. As opposed to running sequentially from 1 to 200,000,000 until one million primes are found, the input space is instead divided up and therefore it is possible to get different results in some circumstances.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!