MATLAB: Does MATLAB comparatively run slower on the machine with higher cores

comparisoncpulaptoplongerMATLABmulticoremulticoredmultithreadingoptimizationoptimizingperformanceperformingserverslowslowerworkstationworse

I have a machine with a relatively high number of cores and a machine with a relatively low number of cores. When I run various different kinds of scripts, the machine with more cores runs significantly slower than the machine with fewer cores. Why is this happening, and how can I make my "better" machine run faster than my worse machine, as would commonly be expected?

Best Answer

The reason the "stronger" or "better" multi-cored machine is performing comparatively worse is primarily due to a lack of data parallelism given the algorithms and data set a given script or function is using. Because of this, the function or script is dominated by sequential tasks.
In cases where data parallelism is low, but the data set is just large enough to trigger multithreading, the multithreading overhead associated with that can lead the multi-cored machine to run slower than the machine with less cores but a similar clock speed.
In situations like this, limiting the number of used cores to 1 with the following command:
maxNumCompThreads(1)
might even show an improvement in performance in the machine with a high number of cores.
In addition, it might be shown that this "better" computer runs faster benchmark tests with the function:
bench(N)
This is because the data parallelism in the benchmark tests is significantly higher than whatever other function or script the user has issue with while running on the higher core machine.
Unfortunately in circumstances like this, there is usually not much that can be done to parallelize the code - this is largely dependent on the kinds of algorithms being used within the code. However, best practices in improving performance should still be applied, such as the pre-allocation of arrays, use of vectorization, and the use of functions instead of scripts.
In addition, there are some performance functionalities provided by other toolboxes such as 'parfor' in the Parallel Computing Toolbox and generated C-code using MATLAB Coder. It is worth noting that both of these options are very case-dependent, and will not always lead to an increase in performance.
In summary:
1 ) The data parallelism involved in the working algorithms and data sets of the function or script is very low
2 ) This can lead to the multithreading overhead taking longer than the time improved for the parallel sections of code