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.

using System.Threading;
using System.Threading.Tasks;

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

double[] nums = new double[10];

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

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.

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.

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) --

359 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) ? (AMP Version)

Leave a Reply