<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Is it ok to use nested Parallel.For loops?</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/03/14/10283025.aspx</link><description>Every now and then, I get this question: “is it ok to use nested Parallel.For loops?” The short answer is “yes.”&amp;#160; As is often the case, the longer answer is, well, longer. Typically when folks ask this question, they’re concerned about one of two</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Is it ok to use nested Parallel.For loops?</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/03/14/10283025.aspx#10285241</link><pubDate>Tue, 20 Mar 2012 03:59:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10285241</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;Hi Valera-&lt;/p&gt;
&lt;p&gt;Parallel.For is a blocking call in the same way that a for loop is blocking: the caller won&amp;#39;t progress past the loop until the loop has finished its work.&lt;/p&gt;
&lt;p&gt;It&amp;#39;s true that the Parallel.For won&amp;#39;t return until the tasks it&amp;#39;s used for processing have completed, but as noted in this post, the loop will run the first task it creates synchronously on the current thread, it will only have a single additional task sitting around for an additional thread to pick up, and it&amp;#39;ll cancel any outstanding tasks that aren&amp;#39;t being used when all of the work for the loop has completed.&lt;/p&gt;
&lt;p&gt;Your example of 15 nested Parallel.Fors should not cause thread starvation or deadlock... give it a try.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10285241" width="1" height="1"&gt;</description></item><item><title>re: Is it ok to use nested Parallel.For loops?</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/03/14/10283025.aspx#10284349</link><pubDate>Fri, 16 Mar 2012 18:04:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10284349</guid><dc:creator>Valera Kolupaev</dc:creator><description>&lt;p&gt;Is&amp;#39;t Parallel.For is a locking call?&lt;/p&gt;
&lt;p&gt;I assume that it locks and waits till child tasks will complete it&amp;#39;s execution.&lt;/p&gt;
&lt;p&gt;Would&amp;#39;t 15 nested Parallel.For cause thread starvation on ThreadPool and deadlock it this case?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10284349" width="1" height="1"&gt;</description></item><item><title>re: Is it ok to use nested Parallel.For loops?</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/03/14/10283025.aspx#10283753</link><pubDate>Thu, 15 Mar 2012 16:04:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10283753</guid><dc:creator>Stephen Toub - MSFT</dc:creator><description>&lt;p&gt;Harry, if/when this occurs, typically it&amp;#39;s because either:&lt;/p&gt;
&lt;p&gt;1) You have a significant load imbalance in terms of how long it takes to process certain items. &amp;nbsp;It could be that the threads you see running for a while at the end are all processing their last item, which is just taking longer than some of the other items, and thus the other threads have all finished with no more work to be done.&lt;/p&gt;
&lt;p&gt;2) The parallel loop partitioned the data in chunks such that, based on the how the data was distributed, some of the threads got starved for data towards the end of the loop. &amp;nbsp;If that&amp;#39;s the case, you could control the partitioning in a manner that&amp;#39;s better for your data set. &amp;nbsp;The limit of this is ensuring that each thread never takes more than 1 item at a time... to achieve that, you could use a partitioner like the one described at &lt;a rel="nofollow" target="_new" href="http://drdobbs.com/go-parallel/article/224600406?pgno=3"&gt;drdobbs.com/.../224600406&lt;/a&gt;, or if you&amp;#39;re using .NET 4.5, you can use the built-in variant of this via Partitioner.Create(source, EnumerablePartitionerOptions.NoBuffering).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10283753" width="1" height="1"&gt;</description></item><item><title>re: Is it ok to use nested Parallel.For loops?</title><link>http://blogs.msdn.com/b/pfxteam/archive/2012/03/14/10283025.aspx#10283562</link><pubDate>Thu, 15 Mar 2012 09:51:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10283562</guid><dc:creator>Harry</dc:creator><description>&lt;p&gt;Could you also explain why Parallel.For does not always utilize all available hardware threads when running long running tasks. E.g. in our case we have 24 long running task we want to run in parallel 8 at a time on a 8 hardware thread machine, but typically at the end of the run less than 8 tasks are running, slowing the total run down considerably. It is as if tasks are not executed even though a thread should be available...&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10283562" width="1" height="1"&gt;</description></item></channel></rss>