How to Speed Up Parallel Processing using Parallel For, Foreach in C# (.NET 4.0 or above) ?


At .NET 4.0 or .NET 4.5, you can write simple Parallel For Loop, which is useful if you have SIMD (Single Instruction Multiple Data). Instead of hard-coded threads, putting them in parallel, waiting for threads to finish (synchronization), you can just use the Parallel.For or Parallel.ForEach.

First of all, you would need these two units.

1
2
using System.Threading;
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;

Then, for demonstration purpose, we create an array of ten doubles.

1
double[] nums = new double[10];
double[] nums = new double[10];

Instead of the traditional way to initialise them with value equals to their index position:

1
2
3
for (int i = 0; i < nums.length; i ++) {
    nums[i] = i;
}
for (int i = 0; i < nums.length; i ++) {
    nums[i] = i;
}

We can use Parallel.For; the first parameter is the lower bound of index, the second parameter is the upper bound of index, and the third parameter is the action for index.

1
2
3
4
5
6
Parallel.For(0, nums.Length - 1, 
    i =>
    {
        nums[i] = i;
    }
); // helloacm.com
Parallel.For(0, nums.Length - 1, 
    i =>
    {
        nums[i] = i;
    }
); // helloacm.com

Similarly, we can use Parallel.ForEach to view these values. It only takes two parameters. The first specifies the data target. The second specifies the action.

1
2
3
4
5
6
Parallel.ForEach(
    nums, num =>
    {
        Console.WriteLine("Number {0:R} on thread {1}", num, Thread.CurrentThread.ManagedThreadId);
    }
); // helloacm.com
Parallel.ForEach(
    nums, num =>
    {
        Console.WriteLine("Number {0:R} on thread {1}", num, Thread.CurrentThread.ManagedThreadId);
    }
); // helloacm.com

The data parallelism is easily enabled. However, with ForEach, the variable num as defined in above example is a separate copy. Altering this variable does not change the values in the array.

Do not always assume the parallel-for is faster, in some cases, e.g. when sizes of array is not large, sequential access is faster due to cache/prefetching.

Combining above two snippets, we have outputs like this:

Number 0 on thread 9
Number 0 on thread 9
Number 7 on thread 15
Number 5 on thread 14
Number 4 on thread 12
Number 8 on thread 16
Number 1 on thread 10
Number 2 on thread 11
Number 3 on thread 6
Number 6 on thread 13

--EOF (The Ultimate Computing & Technology Blog) --

GD Star Rating
loading...
376 words
Last Post: How to Disable Onboard Graphics Card?
Next Post: How to Suppress Ads when WordPress Users are Logged in?

The Permanent URL is: How to Speed Up Parallel Processing using Parallel For, Foreach in C# (.NET 4.0 or above) ?

Leave a Reply