Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-threading with a CPU bound routine?

I'm getting mixed signals whether it makes sense to have multiple threads performing the same CPU bound routine (each thread plowing through different data, of course).

If you are CPU bound, you are already taxing the processor, right? Why would it make sense to add additional CPU work then? Is the idea that you should try to match the number of CPU-bound threads to cores?

What about the case of a single core machine. Does multi-threading a CPU bound operation ever make sense?

Thanks in advance.

like image 792
jglouie Avatar asked Jun 11 '12 18:06

jglouie


1 Answers

If you are CPU bound, you are already taxing the processor, right? Why would it make sense to add additional CPU work then? Is the idea that you should try to match the number of CPU-bound threads to cores?

Yes, this is basically the idea. If you're algorithm is CPU bound, it will tie up one processing core. If there are 4 in your system, using all 4 can give you a significant boost in overall throughput. Without multiple threads, you'll only ever use a single core to process your data.

What about the case of a single core machine. Does multi-threading a CPU bound operation ever make sense?

It can, but typically only if it's not purely CPU bound. If your routine is completely CPU bound, you typically want no more than 1 thread per core. If, however, certain portions of your algorithm have other characteristics, and are bound by other things, that portion can sometimes benefit from having the overall routine threaded.

like image 71
Reed Copsey Avatar answered Sep 18 '22 13:09

Reed Copsey