How many threads can I engage at the same time?
Matthew Harrington
I was recently handed a new Macbook Pro with Quad-Core Intel Core i5 (1.4 GHz) listing 1 processor and 4 total number of cores, with 8 GB Memory and Hyper-Threading technology to perform some MRI analyses. The command lines usually allow me to input the number of threads I want the analysis to performed with, and since they are very computationally intensive and time-consuming I want to know how to engage as many threads as possible to run the analyses as fast as possible. However I don't know how many threads can I use at a time with my current setup.
I was reading around the internet that apparently you have 2 threads per core, meaning the setup I have can engage up to 8 at a time, but a quick look at my Activity Monitor shows me that some processes like Photoshop are using up to 80 threads while the analysis I am performing (and is taking forever) is using only 2.
How do I know how many threads I have available, so I can redirect as many resources as possible towards the execution of the analaysis?
11 Answer
Your PC is capable of running 4 threads and 4 "virtual" threads at any given nano-second. A thread is a stream of instructions. Each core of a CPU is capable of running 1 instruction at a time, though Hypertheading allows a partial execution of two instructions simultaneously. It's not as fast as doubling the cores, but costs less, generates less heat and uses less power. for most workloads hyperthreading improves performance.
So first understand that PCs used to have only one CPU. So how could the OS execute while a program was? OSes allow multiple executables to use the CPU, by granting each of them small amounts of processing time, and switches between them very quickly. The OS schedules which threads are given how much time based on things like process priority.
So your PC has hundreds of threads established across all the apps you have running, but those threads are mostly idle at any given instant. only 8 of them are executing at a time.
Resource availability and IO will impact the ability of the system to schedule the tasks required by the applications. If you are low on RAM, or if your process is regularly reading or writing from disk, that can limit your ability to maximize parallel execution (though in fact many parallel algorithms do use multiple threads for IO, to preserve a responsive UI, and interleave stages of IO). When a disk read is required, or the system is low on ram and is constantly exchanging memory pages from disk, the system will pause the thread to wait for the data to be retrieved, and only one command can be processed by a hard disk at a time. For RAM, add more, but for IO problems, you need an engineering solution from the program's makers.
In terms of practicals, I've not used Photoshop in many years, but it traditionally has a very large impact on the systems running state. It does not surprise me that having it open would bog down a standard off-the-shelf macbook.
5