[Mono-list] Unexpected behavior of Parallel.For in mono 3.12.1, for lower numbers of iterations

Jean-Michel.Perraud at csiro.au Jean-Michel.Perraud at csiro.au
Fri Apr 3 09:53:55 UTC 2015


While figuring out why an optimisation routine  (https://github.com/jmp75/metaheuristics/blob/master/CSIRO.Metaheuristics/Optimization/ShuffledComplexEvolution.cs)  was not using multi-threading on Linux as expected when it was doing so on Windows+MS.NET I found out a behavior that may be worth noticing.

I gather from the look of the mono codebase that this behavior may not persist for long given the move to the MS .NET reference source code. Still, in case someone scratches their head before that happens:

Parallel.For has a hard coded behavior where for an iteration over less then 5 times the maximum level of parallelism required, it will use less than that. If you are doing large granularity task parallelism with long-lived threads, this is in your way, with no way to work around it, apart from using Parallel.ForEach which does not exhibit this behavior.

A test program to illustrate this:

		public static void Main (string[] args)
		{
			if (args.Length < 2) {
				Console.WriteLine ("USAGE: ./TestTpl.exe [MaxDegreeOfParallelism]  [NumIter]");
				return;
			}
			int m = int.Parse(args[0]);
			ParallelOptions op = new ParallelOptions () { MaxDegreeOfParallelism = m };
			var ts = TaskScheduler.Default;
			Console.WriteLine ("ProcessorCount = " + Environment.ProcessorCount);
			Console.WriteLine ("TaskScheduler.MaximumConcurrencyLevel = " + ts.MaximumConcurrencyLevel);
			int numIter = int.Parse (args [1]);

			List<int> list = new List<int> ();
			for (int i = 0; i < numIter; i++) {
				list.Add (i);
			}
			Console.WriteLine ("Parallel.For");
			Parallel.For(0, numIter, op, i => { Thread.Sleep(1000); Console.WriteLine(i);});

			Console.WriteLine ("Parallel.ForEach");
			Parallel.ForEach(list, op, i => { Thread.Sleep(1000); Console.WriteLine(i);});		
		}


More information about the Mono-list mailing list