Reputation: 583
I am currently working on an application that has an "embarrassingly Parallel" scenario. Is there any guideline/algorithm to determine ideal number of tasks to maximize CPU utilization.
Upvotes: 3
Views: 1450
Reputation: 244777
I think the best approach is to first let the framework deal with that and do something more complicated only when that isn't good enough.
In your case, it would probably mean using Parallel.ForEach()
to process some collection, instead of manually using n Task
s.
When you find out that Parallel.ForEach()
with default settings doesn't parallelize the work in the way you would want, then you try fiddling with it, by setting MaxDegreeOfParallelism
or using a custom partitioner.
And only when that still isn't good enough, then you should consider using Task
s.
Upvotes: 4
Reputation: 5515
If you could maintain a number of threads equal to the number of cores (or double if you have Hyperthreading enabled) the CPU should be utilized in the optimal way.
Also, the related post might be helpful: Optimal number of threads per core.
Upvotes: 3
Reputation: 20224
This depends on your task. If you only process and don't wait for I/O, you should have as many as you have cores.
Sending queries to many different servers, waiting 20 to 40ms for a response, reading some I/O from some disk drive or tape recorder, and then processing only a single ms, every core can serve 30 threads or more.
Upvotes: 2