Using Parallelism in Matlab
One way to improve MATLAB code performance is using parallelism. In MATLAB, the most common way to do this is to replace a slow for loop with a parfor loop.
However, there are some restrictions on what loops can be converted to parfor. Loops must be independent. That is, the loop iterations should be executable in any order, where the results of one iteration do not rely on previous iterations.
More information on converting loops to parfor can be found at: https://www.mathworks.com/help/distcomp/troubleshoot-variables-in-parfor-loops.html
Using the Console
It is possible to use MATLAB without its graphical interface. In particular, on remote Linux servers it is often best to run MATLAB from the command-line and not interactively.
Once your MATLAB code is finished and tested, you can run it from the Linux server’s shell:
- SSH to remote server
- Change directory to your MATLAB script’s location (let’s assume your script is called myexample.m)
- Run this command:
matlab -nodisplay -r 'myexample; quit'
You can even put this MATLAB command in the background using techniques found at either:
Forcing Single-Threaded Execution
MATLAB has commands that operate on entire matrices or vectors at once. These are called vectorized operations. MATLAB automatically runs many vectorized operations in parallel.
Sometimes you don’t want MATLAB to automatically use multiple processor cores. Perhaps you want to evaluate how effective MATLAB’s core use is, or perhaps you need to share the cores with other programs. You can disable MATLAB’s automatic parallelism by invoking matlab from the command line with this option: matlab -singleCompThread