1

I have a question about Parallel.For method.

I want to make comparision between using 1 to 4 threads in image processing algorithm. Im setting up my Parallel.For like this:

        ParallelOptions po = new ParallelOptions();
        po.MaxDegreeOfParallelism = 4;

        Parallel.For(0, height - 1, po, y =>
        {
            int offset = y * stride; //row
            for (int x = 0; x < width - 1; x++)
            {
                //do stuff here
            }
         });

Time is counted by Stopwatch class.

var stopwatch = Stopwatch.StartNew();
MethodWithParallelFor.Execute();
stopwatch.Stop();

My question is, why when i set up MaxDegreeOfParallelism to any value (my CPU have 8 thread) i get exactly same times? When i set Degree to 1 or 4 i get same executing times.

So, how to debug Parallel.For to get information, how many threads are running in my loop? How to force program to use that many threads that i want to? If needed, i can share with my full code

Im implemented my test program in c# 7.3 and WPF 4.6.2

michasaucer
  • 4,562
  • 9
  • 40
  • 91
  • 2
    How large are your images? How much time is being reported? – Cameron Mar 26 '19 at 16:10
  • My image is `200x300px 8bpp` and i get `15-20ms` – michasaucer Mar 26 '19 at 16:11
  • 2
    Your image is likely too small for the parallelization to kick in. [`MaxDegreeOfParallelism`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.paralleloptions.maxdegreeofparallelism?view=netframework-4.7.2#System_Threading_Tasks_ParallelOptions_MaxDegreeOfParallelism) only controls the maximum; it's up to the thread pool to spin up new threads as it sees fit. – Cameron Mar 26 '19 at 16:15
  • 2
    You can't "force" it to use x number of threads. `MaxDegreeOfParallelism` is used to *restrict* the number of concurrent tasks. It doesn't meant that a certain number of threads will be used.The actual number of threads being used is calculated based on an algorithm and may differ from time to time. Please refer to [this](https://stackoverflow.com/questions/1114317/does-parallel-foreach-limit-the-number-of-active-threads) thread for more information. If you know that you always want to use x number of threads, you could create threads explicitly. – mm8 Mar 26 '19 at 16:17
  • Is there option to get information, how many threads was used in my loops? – michasaucer Mar 26 '19 at 16:26
  • 2
    It sounds for me like doing a micro-benchmarking. Don't do such things manually (there are lot of pitfalls), instead use battle-tested tools for this, personally I'd suggest to use [BenchmarkDotNet](https://github.com/dotnet/BenchmarkDotNet). – Dmytro Mukalov Mar 26 '19 at 16:57

1 Answers1

2

If all what you want to know is how many threads are concurrently running in your Parallel.For at any given time, you can use Interlocked.Increment and Interlocked.Decrement (at the beginning and the end of the Loop), check if the value in the variable is bigger than before and output it after the Parallel.For is completed, something like this:

public void TestParallelism() {
    int maxThreads = 0;
    int currentThreads = 0;

    Parallel.For(0, 100, (i) => {

        var max = Interlocked.Increment(ref currentThreads);

        if (maxThreads < max) {
            maxThreads = max; 
        }

        // your code here...

        Interlocked.Decrement(ref currentThreads);
    });

    Console.WriteLine($ "Max Parallel Threads: {maxThreads}");
}
Jesus Salas
  • 652
  • 3
  • 14