Kiran Vedula
Kiran Vedula

Reputation: 583

Ideal number of Tasks

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

Answers (3)

svick
svick

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 Tasks.

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 Tasks.

Upvotes: 4

Igor Ševo
Igor Ševo

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

Alexander
Alexander

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

Related Questions