MATLAB: Does MATLAB R2013a only use about 50% of a 4 core 8 threads CPU at most

MATLAB

Running the following code:
a=randn(10^5,10^3)+1i*randn(10^5,10^3);
for i=1:10
b=inv(a'*a);
end
The CPU usage is only 50% and not 100%, although INV is multithreaded. Why is this happening?

Best Answer

INV will run on multiple threads in R2013a by default. However, currently the maximum number of threads that MATLAB will choose is equal to the maximum number of physical cores (instead of threads, or logical cores) of the CPU. Therefore, the code will only use 4 threads at most on a CPU with 4 cores 8 threads.
With a 4 cores 8 threads CPU, Windows displays it as having 8 logical cores in the Task Manager. The 4 threads from MATLAB will show up as occupying 4 logical cores in Windows Task Manager. Even the 4 MATLAB threads occupy the 4 logical cores 100% of the time , the CPU usage only shows 50% instead of 100% because only 4 out of 8 logical cores are used.
Some CPUs haves two logical cores assigned to one physical core is because this allows scheduling two threads to run on the same physical core simultaneously. This can offer potential speed gains, especially if each of the threads has some significant idle time in it.
Usually, using more number of threads than the number of physical cores provides little gain in speed, sometimes even results in reduction of speed because of the overhead involved in managing the extra threads. This is especially true if the code involved does not have significant CPU idle time.
Related Question