views:

179

answers:

2

In former versions of Parallel Extensions you could set the number of threads:

enumerable.AsParallel(numberOfThreads)

But now that overload is not available anymore. How to do it now?

+4  A: 

I really have no idea why it changed, so I can't answer the question, but it seems like if the developer specifies the number of threads, then the parallel runtime won't be able to perform the operation in the most optimal way based on the currently available hardware threads.

I don't want to specify number of threads. The beauty of PLINQ is that it just goes parallel without me having to figure out any threading logic.

Dennis Palmer
That and I think the runtime computes it based upon the resources at hand. IE an 8 core box does more threads and a 2 core box.
Jason Short
8 core vs 2 core is what I was thinking when I wrote "available hardware threads." I wasn't expecting to be the only answer on this question!
Dennis Palmer
In todays desktop applications, most of the time, you are not cpu bound; being able to manually set the numbers of parallel tasks would allow developers to write reasonably good parallel code without the need to deal with the complexities of APM.
Maghis
@Maghis, what does APM stand for? I'm not sure what complexities you're referring to.
Dennis Palmer
@Dennis Palmer APM stands for Asynchronous Programming Model and is a .net Pattern to get access to functionality like IO completition ports. Basically you can use them to wait for an external resource (ie network) without wasting a thread. (good intro: http://msmvps.com/blogs/luisabreu/archive/2009/06/07/multithreading-introducing-the-asynchronous-programming-model.aspx)
Maghis
@Maghis, thanks for the link! My understanding of PLINQ is that it abstracts away those complexities and the developer doesn't need to be concerned with how many parallel tasks there are. I suppose having that control might be nice, but I'd rather let the parallel runtime figure it out.
Dennis Palmer
+4  A: 

In the new version you can specify it with the extension method ".WithDegreeOfParallelism(int degreeOfParallelism)".

IE:

enumerable.AsParallel().WithDegreeOfParallelism(numberOfThreads)
Maghis