How to combine Thread-local variables and speed up small loop bodies?Hi, <br/> <br/> I was trying to combine the two How-To´s from msdn:<br/> <a title="Speed Up Small Loop Bodies" href="http://msdn.microsoft.com/en-us/library/dd560853(VS.100).aspx" title="Speed Up Small Loop Bodies">Speed Up Small Loop Bodies</a> <br/> <a title="Write a Parallel.For Loop with Thread-Local Variables" href="http://msdn.microsoft.com/en-us/library/dd460703(VS.100).aspx" title="Write a Parallel.For Loop with Thread-Local Variables">Write a Parallel.For Loop with Thread-Local Variables</a> <br/> <br/> The example with the parallel thread-local variables was slower then the corresponding sequential implementation.<br/> So I want to create partitions based on the cpu count and parallelize just this (like the first example). <br/> <br/> Of course the following code isn´t correct because the variable &quot;total&quot; isn´t summed up thread-safe:<br/> <br/> <pre lang="x-c#"> private static IEnumerable&lt;Tuple&lt;int, int&gt;&gt; CreateRanges(int fromInclusive, int toExclusive) { // argument checking omitted int rangeSize = (toExclusive - fromInclusive) / System.Environment.ProcessorCount; for (int i = fromInclusive; i &lt; toExclusive; i += rangeSize) { int from = i, to = Math.Min(i + rangeSize, toExclusive); yield return Tuple.Create(from, to); } } static void Main() { int[] arr2 = Enumerable.Range(0, 1000000).ToArray(); long total = 0; Parallel.ForEach(CreateRanges(0, arr2.Length), range =&gt; { for (int i = range.Item1; i &lt; range.Item2; i++) { // Small body. total += arr2[i]; } }); Console.WriteLine(total); Console.ReadKey(); }</pre> But I am really stuck cause I don´t have a glue how to parallelize the partitions and get the correct results.<br/> Can anybode help me?<br/> <br/> Thanks, <br/> Alex<br/>© 2009 Microsoft Corporation. All rights reserved.Tue, 16 Jun 2009 02:12:17 Z08570fed-f719-42bc-b391-b7be9c83a5c5http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/08570fed-f719-42bc-b391-b7be9c83a5c5#08570fed-f719-42bc-b391-b7be9c83a5c5http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/08570fed-f719-42bc-b391-b7be9c83a5c5#08570fed-f719-42bc-b391-b7be9c83a5c5mistalanhttp://social.msdn.microsoft.com/Profile/en-US/?user=mistalanHow to combine Thread-local variables and speed up small loop bodies?Hi, <br/> <br/> I was trying to combine the two How-To´s from msdn:<br/> <a title="Speed Up Small Loop Bodies" href="http://msdn.microsoft.com/en-us/library/dd560853(VS.100).aspx" title="Speed Up Small Loop Bodies">Speed Up Small Loop Bodies</a> <br/> <a title="Write a Parallel.For Loop with Thread-Local Variables" href="http://msdn.microsoft.com/en-us/library/dd460703(VS.100).aspx" title="Write a Parallel.For Loop with Thread-Local Variables">Write a Parallel.For Loop with Thread-Local Variables</a> <br/> <br/> The example with the parallel thread-local variables was slower then the corresponding sequential implementation.<br/> So I want to create partitions based on the cpu count and parallelize just this (like the first example). <br/> <br/> Of course the following code isn´t correct because the variable &quot;total&quot; isn´t summed up thread-safe:<br/> <br/> <pre lang="x-c#"> private static IEnumerable&lt;Tuple&lt;int, int&gt;&gt; CreateRanges(int fromInclusive, int toExclusive) { // argument checking omitted int rangeSize = (toExclusive - fromInclusive) / System.Environment.ProcessorCount; for (int i = fromInclusive; i &lt; toExclusive; i += rangeSize) { int from = i, to = Math.Min(i + rangeSize, toExclusive); yield return Tuple.Create(from, to); } } static void Main() { int[] arr2 = Enumerable.Range(0, 1000000).ToArray(); long total = 0; Parallel.ForEach(CreateRanges(0, arr2.Length), range =&gt; { for (int i = range.Item1; i &lt; range.Item2; i++) { // Small body. total += arr2[i]; } }); Console.WriteLine(total); Console.ReadKey(); }</pre> But I am really stuck cause I don´t have a glue how to parallelize the partitions and get the correct results.<br/> Can anybode help me?<br/> <br/> Thanks, <br/> Alex<br/>Sun, 14 Jun 2009 17:16:17 Z2009-06-14T17:16:17Zhttp://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/08570fed-f719-42bc-b391-b7be9c83a5c5#6b7926cb-e42f-468e-8322-8e7a3a7c5962http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/08570fed-f719-42bc-b391-b7be9c83a5c5#6b7926cb-e42f-468e-8322-8e7a3a7c5962Miha Markichttp://social.msdn.microsoft.com/Profile/en-US/?user=Miha%20MarkicHow to combine Thread-local variables and speed up small loop bodies?ForEach like this might do the job:<br/> <br/> <pre lang="x-c#"> Parallel.ForEach &lt;Tuple&lt;int, int&gt;, long&gt;(CreateRanges(0, arr2.Length), () =&gt; 0, (range, loop, subtotal) =&gt; { for (int i = range.Item1; i &lt; range.Item2; i++) { // Small body. subtotal += arr2[i]; } return subtotal; }, subtotal =&gt; Interlocked.Add(ref total, subtotal));</pre> <br/><hr class="sig">Miha Markic [MVP C#] http://blog.rthand.comSun, 14 Jun 2009 20:09:52 Z2009-06-14T20:09:52Z