wrong results with parallel for?Hi, <br/> <br/> I was just trying the new Parallel extension with .NET 4.0 Beta in C#. <br/> Actually I want to compare time results between sequential and parallel for-loops but then I found something strange:<br/> When running a simple code which sums up array values I got wrong results with parallel execution. This seems to happen with iterations &gt; about 10 000. <br/> <br/> Here is the source Code:<br/> <br/> <pre lang="x-c#">using System; using System.Collections.Generic; using System.Threading; using System.Diagnostics; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int[] numbers = Enumerable.Range(0, 1000000).ToArray(); long total = 0; Stopwatch clock = new Stopwatch(); Console.WriteLine(&quot;With Parallelization:&quot;); clock.Start(); Parallel.For(0, numbers.Length, i =&gt; { total += numbers[i]; } ); clock.Stop(); Console.WriteLine(&quot;Result: &quot; + total); Console.WriteLine(&quot;Time: &quot; + clock.ElapsedMilliseconds); total = 0; clock.Reset(); Console.WriteLine(&quot;Without Parallelization:&quot;); clock.Start(); for (int i = 0; i &lt; numbers.Length; i++) { total += numbers[i]; } clock.Stop(); Console.WriteLine(&quot;Result: &quot; + total); Console.WriteLine(&quot;Time: &quot; + clock.ElapsedMilliseconds); clock.Reset(); Console.ReadLine(); } } } </pre> <br/> <br/> And here are the results:<br/> <br/> <pre>With Parallelization: Result: 224897535827 Time: 48 Without Parallelization: Result: 499999500000 Time: 6</pre> <br/> The strange thing is that the results of parallel execution seems to differ for each run:<br/> <br/> <pre>With Parallelization: Result: 209604538631 Time: 60 Without Parallelization: Result: 499999500000 Time: 6</pre> <br/> Am I doing something wrong? Or is this a bug?<br/> <br/> Regards, <br/> Alex<br/>© 2009 Microsoft Corporation. All rights reserved.Fri, 12 Jun 2009 20:30:06 Z76613b87-992a-4378-bf21-55d31c3352a5http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/76613b87-992a-4378-bf21-55d31c3352a5#76613b87-992a-4378-bf21-55d31c3352a5http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/76613b87-992a-4378-bf21-55d31c3352a5#76613b87-992a-4378-bf21-55d31c3352a5mistalanhttp://social.msdn.microsoft.com/Profile/en-US/?user=mistalanwrong results with parallel for?Hi, <br/> <br/> I was just trying the new Parallel extension with .NET 4.0 Beta in C#. <br/> Actually I want to compare time results between sequential and parallel for-loops but then I found something strange:<br/> When running a simple code which sums up array values I got wrong results with parallel execution. This seems to happen with iterations &gt; about 10 000. <br/> <br/> Here is the source Code:<br/> <br/> <pre lang="x-c#">using System; using System.Collections.Generic; using System.Threading; using System.Diagnostics; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int[] numbers = Enumerable.Range(0, 1000000).ToArray(); long total = 0; Stopwatch clock = new Stopwatch(); Console.WriteLine(&quot;With Parallelization:&quot;); clock.Start(); Parallel.For(0, numbers.Length, i =&gt; { total += numbers[i]; } ); clock.Stop(); Console.WriteLine(&quot;Result: &quot; + total); Console.WriteLine(&quot;Time: &quot; + clock.ElapsedMilliseconds); total = 0; clock.Reset(); Console.WriteLine(&quot;Without Parallelization:&quot;); clock.Start(); for (int i = 0; i &lt; numbers.Length; i++) { total += numbers[i]; } clock.Stop(); Console.WriteLine(&quot;Result: &quot; + total); Console.WriteLine(&quot;Time: &quot; + clock.ElapsedMilliseconds); clock.Reset(); Console.ReadLine(); } } } </pre> <br/> <br/> And here are the results:<br/> <br/> <pre>With Parallelization: Result: 224897535827 Time: 48 Without Parallelization: Result: 499999500000 Time: 6</pre> <br/> The strange thing is that the results of parallel execution seems to differ for each run:<br/> <br/> <pre>With Parallelization: Result: 209604538631 Time: 60 Without Parallelization: Result: 499999500000 Time: 6</pre> <br/> Am I doing something wrong? Or is this a bug?<br/> <br/> Regards, <br/> Alex<br/>Fri, 12 Jun 2009 20:00:53 Z2009-06-12T20:02:53Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/76613b87-992a-4378-bf21-55d31c3352a5#4c1ed7e8-a27f-4592-824e-f5edeb7a2518http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/76613b87-992a-4378-bf21-55d31c3352a5#4c1ed7e8-a27f-4592-824e-f5edeb7a2518Miha Markichttp://social.msdn.microsoft.com/Profile/en-US/?user=Miha%20Markicwrong results with parallel for?Hi Alex,<br/> <br/> Your parallel for is wrong because your aren't protecting the total against concurrent access - total += numbers[i] is called from withing multiple threads at the same time and this operation isn't thread safe.<br/> Here is a correct version using subtotals (each thread calculates its subtotal and all subtotals are safely added together by Interlocked.Add operation:<br/>   <pre lang="x-c#">Parallel.For&lt;long&gt;(0, numbers.Length, // init subtotal () =&gt; 0, (i, loop, subtotal) =&gt; { subtotal += numbers[i]; return subtotal; }, // add subtotal to total value (subtotal) =&gt; Interlocked.Add(ref total, subtotal) );</pre> See also this link - <a href="http://msdn.microsoft.com/en-us/library/dd460703(VS.100).aspx">How to: Write a Parallel.For Loop with Thread-Local Variables</a><hr class="sig">Miha Markic [MVP C#] http://blog.rthand.comFri, 12 Jun 2009 20:16:39 Z2009-06-12T20:16:39Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/76613b87-992a-4378-bf21-55d31c3352a5#28519cc6-f0df-4140-ba0e-88d6897ccd73http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/76613b87-992a-4378-bf21-55d31c3352a5#28519cc6-f0df-4140-ba0e-88d6897ccd73mistalanhttp://social.msdn.microsoft.com/Profile/en-US/?user=mistalanwrong results with parallel for?Thanks a lot Miha.<br/>Fri, 12 Jun 2009 20:30:06 Z2009-06-12T20:30:06Z