Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MaxDegreeOfParallelism = Environment.ProcessorCount slows down execution time on my CPU

I have the following program (that I got from http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx) that splits a task up using Parallel.For loop

class Program
{
    static void Main(string[] args)
    {
        var watch = Stopwatch.StartNew();


        Parallel.For(2, 20, (i) =>
        {
            var result = SumRootN(i);
            Console.WriteLine("root {0} : {1} ", i, result);
        });

        Console.WriteLine(watch.ElapsedMilliseconds);
        Console.ReadLine();
    }

    public static double SumRootN(int root)
    {
        double result = 0;
        for (int i = 1; i < 10000000; i++)
        {
            result += Math.Exp(Math.Log(i) / root);
        }
        return result;
    }
}

When I run this test several times I get times of:

1992, 2140, 1783, 1863 ms etc etc.

My first question is, why are the times always different?? I am doing the exact same calculations each time yet the times vary each time.

Now when I add in the following code to make use of all the available processors on my CPU:

        var parallelOptions = new ParallelOptions
        {
            MaxDegreeOfParallelism = Environment.ProcessorCount    (On my CPU this is 8)
        };

        Parallel.For(2, 20, parallelOptions, (i) =>
        {
            var result = SumRootN(i);
            Console.WriteLine("root {0} : {1} ", i, result);
        });

I notice that the execution times actually increase!! The times are now:

2192, 3192, 2603, 2245 ms etc etc.

Why does this cause the times to increase? Am I using this wrong?

like image 239
Harry Boy Avatar asked Dec 27 '13 18:12

Harry Boy


1 Answers

From http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism(v=vs.110).aspx

By default, For and ForEach will utilize however many threads the underlying scheduler provides. Changing MaxDegreeOfParallelism from the default only limits how many concurrent tasks will be used.

This means that setting MaxDegreeOfParallelism to the number of processors will in fact limit the capacity of the Parallel.For loop to use the optimal amount of threads for the work load. For example, I have a migration job that uses close to 60 threads on around 600 iterations of long-running code, a lot more than the 1 thread per processor limit that you're trying to set.

MaxDegreeOfParallelism or ThreadPool.SetMaxThreads should only be used if you explicitly need to prevent more than a given number of threads from executing. For example, if using an Access database, I would set it to 64, because that's the maximum number of concurrent connections that can be handled by Access for a single process.

like image 75
Benjamin Beaulieu Avatar answered Nov 15 '22 18:11

Benjamin Beaulieu