MATLAB (MATrix LABoratory) is a commercial tool for numerical computing, matrix operations, data plotting, algorithm development, and more. The HPC cluster supports both serial (single-core) and distributed (multi-core/MDCS) MATLAB jobs.
To list available versions:
module spider matlab
Sample output:
matlab/R2020b
matlab/R2021b
matlab/R2023a
module load matlab/R2023a
Request a compute node:
srun --pty --mem=4gb --x11 /bin/bash
Run MATLAB:
matlab -singleCompThread -nodisplay
Use -nodisplay to disable GUI. Use -singleCompThread for serial or distributed jobs.
Check version and toolboxes:
>> ver
Set breakpoints by clicking beside the line number.
Use the Debug menu to step, continue, or stop.
From the command line:
>> dbstop in file.m at 10
>> dbstep % Executes current line
>> dbcont % Resumes to next breakpoint
>> dbquit % Exits debug mode
Copy the example files:
cp /usr/local/doc/MATLAB/fixedmontecarlo.m ./
Create a SLURM script (calPi.slurm):
#!/bin/bash
#SBATCH -o test.o%j
#SBATCH --time=02:00:00
#SBATCH -N 1 -n 1 --mem=5gb
cp -r fixedmontecarlo.m $PFSDIR
cd $PFSDIR
module load matlab
matlab -singleCompThread -nodisplay -r fixedmontecarlo
quit
echo Master process running on $HOSTNAME
echo Directory is `pwd`
NPROCS=$(( $SLURM_NPROCS * $SLURM_NNODES ))
echo This job is allocated $NPROCS nodes
echo `date`
cp -ru * $SLURM_SUBMIT_DIR
Submit the job: sbatch calPi.slurm . After completion, review the output in test.o<jobid>.
Memory must be requested explicitly. Even for serial jobs, large matrices or operations may require more memory.
Estimate memory usage using:
htop command during runtime
MATLAB's memory profiler
Valgrind (for compiled code)
Example test case:
Copy and run:
cp /usr/local/doc/MATLAB/solveEq.m ./
Try with: dim = 3e5; then dim = 5e5;
Monitor memory usage for each.
Check MATLAB licenses by issuing the commands below:
lic 27000@swc-licenses-1.case.edu
Looking for Distributed (Parallel) Computing Licenses
lic 27000@swc-licenses-1.case.edu | grep Distr
output:
Users of Distrib_Computing_Toolbox: (Total of 10000 licenses issued; Total of 13 licenses in use)
You can check the number of licenses you have checked out using "| grep <caseID>" at the end of the above commands.
lic 27000@swc-licenses-1.case.edu | grep <caseID>
output:
<caseID> comp137 …
You can use MATLAB C Compiler (MCC) feature not to checkout other licenses as described in MCC Section below.
Using MCC, you can make an executable of your MATLAB script and run as many instances of it as you want. Regarding MDCS jobs, though you will be checking out MDCS licenses, the PCT and other campus-wide licenses are not checked out. So, it is applicable to MDCS jobs as well.
Compiling:
module load matlab/R2023a
mcc -m <matlab_script>
(Note: For non-GUI jobs, use -R -nodisplay and for serial job, use -R -singleCompThread)
The compiler produces two outputs <matlab_script> executable and run_<matlab_script>.sh shell script. You can include the executable in the SLURM script or run it interactively.
There are two licenses for the matlab compiler (mcc), and managing the license is important to allow everyone who needs the license to use it. Please think carefully before using mcc within a matlab session -- that will check-out the license to your use for the duration of the matlab session. The alternative is to compile with mcc from the command line -- either linux or in a dos shell. Read for more details.
If you are using number as an input to the function, in the MATLAB script, you need to include the str2num function as showed:
<variable_name>=str2num(<input>);
Serial Sample Job: Copy MATLAB script "plot_function.m" and job script "mcc_plotFunction.slurm" from /usr/local/doc/MATLAB and submit the MCC job. Compilation is performed through the script as well.
sbatch mcc_plotFunction.slurm
MDCS jobs: Copy MATLAB script "central_theorem.m" and job script "mcc_CenTh.slurm" from /usr/local/doc/MATLAB and submit MCC MDCS job.
sbatch mcc_CenTh.slurm
Open the output file:
evince-previewer plot_central.ps
MATLAB has GPU support for NVIDIA-CUDA enabled GPU devices which let you accelerate your application. You can look for GPU enabled MATLAB functions in MATLAB website.
Request the GPU node
srun --x11 -p gpu --gres=gpu:1 -N 1 -c 2 --time=1:00:00 --mem=5gb --pty /bin/bash
Load MATLAB module
module load matlab/R2023a
Open matlab and issue gpuDevice command
matlab -nodisplay
>>gpuDevice
output:
parallel.gpu.CUDADevice handle
Package: parallel.gpu
Properties:
Name: 'Tesla M2090'
Index: 1
ComputeCapability: '2.0'
SupportsDouble: 1
DriverVersion: 5
MaxThreadsPerBlock: 1024
MaxShmemPerBlock: 49152
MaxThreadBlockSize: [1024 1024 64]
MaxGridSize: [65535 65535]
SIMDWidth: 32
TotalMemory: 5.6366e+09
FreeMemory: 5.5386e+09
MultiprocessorCount: 16
ClockRateKHz: 1301000
ComputeMode: 'Exclusive process'
GPUOverlapsTransfers: 1
KernelExecutionTimeout: 0
CanMapHostMemory: 1
DeviceSupported: 1
DeviceSelected: 1
Example: Copy MATLAB script file "myCal.m" and the job script file "cuda_mat.slurm" from /usr/local/doc/MATLAB and submit the job:
sbatch cuda_mat.slurm
Note the function Array with the keyword "gpu" in the front; Array function replaced by gpuArray. You may also want to refer to Jacket application installed in HPC for GPU MATLAB jobs.
Profiling is a way to measure where a program spends time. MATLAB parallel profiling enables to see how much time each worker spends on evaluating each function and how much time communicating and waiting for other workers.
Performance Improvement:
Identify the functions that are consuming the most time and try to optimize them
Decide whether the number of times the code calls a particular function is reasonable
Identify the functions within your code that might be calling other time-consuming functions that can be several layers down in the code
Serial (GUI):
profile on
<matlab_script> % Compare array_random.m & vector_gammarand.m
profile off
profile viewer
See three optimization effort in vector_gammarand.m file. Compare its performance with array_random.m. Copy both the files from /usr/local/doc/MATLAB.
nValues = 1e7;
% 1. Preallocation of 1 by 10^7 contiguous block of memory and initialization
firstArray = ones(1,nValues);
secondArray = 2*ones(1,nValues);
resultArray = zeros(1, nValues);
% 2. Using Vector Instead of Array
tic()
resultArray = firstArray .* exp(secondArray);
toc()
% 3. Using the function having better execution time
nValues = 1e4;
tic()
for i=1:nValues
gamrnd(2, 2); % instead of random('gamma', 2, 2);
end
toc()
Serial Profiling output for both array_rand.m and vector_gamrand.m.
Matlab Coder is now available for all CWRU user. Matlab coder is a very useful tool to convert your Matlab code to a C/C++ codes, either to convert it completely or to use part of the function to merge it with a different code snippet.
In the Matlab prompt, type coder and follow the instructions.
>>coder
More information about Matlab Coder on the Mathworks. The manual (preparation, acceleration, code generation) quick start guides for Matlab 2013b can be downloaded from the /usr/local/doc/MATLAB directory.
Reference at [4].
Setup environment variables
BUILDDIR=/home/<caseID>/<path>
MATLABROOT=/usr/local/matlab/<matlab-version>
export INSTALLDIR=`python -m site --user-site`
Build the package
cd $MATLABROOT/extern/engines/python
python setup.py build --build-base=$BUILDDIR install --user
Add the user directory to the python search path
python
>>> import sys, os
>>> sys.path.append(os.getenv("INSTALLDIR"))
Test: Import the matlab engine
>>> import matlab.engine
Troubleshooting MATLAB Issues
1. Validation using SLURM profile did not go through
Solution: move your matlab directory at /home/<CaseID> to something else like matlab-test
mv ~/matlab ~/matlab-test
Issue: Encounter the following error when trying to execute the compiled file using MCC:
./tempt: symbol lookup error: /usr/local/matlab/R2020b/sys/os/glnxa64/libstdc++.so.6: undefined symbol: __cxa_thread_atexit_impl
Solution:
HPC system libraries can be older than what matlab expects, and so they bundle newer libraries with their releases to be preloaded. Set the environment variable:
export LD_PRELOAD="<matlabroot>/bin/glnxa64/glibc-2.17_shim.so"
There is a good (brief) discussion in this thread.
https://itectec.com/matlab/matlab-error-running-matlab-parallel-server-toolbox-admincenter/
Issue: quit/exit throwing - "Error using exit Unrecognized function or variable 'Settings'".
Solution: To workaround this issue, you can ensure the preferences are properly initialized by calling "settings" in your MATLAB code.
>> settings
[1] MATLAB Home: http://www.mathworks.com/
[2] Users Guide Site
[3] Coder - http://www.mathworks.com/products/matlab-coder/
[4] MATLAB Engine API for Python: https://www.mathworks.com/help/matlab/matlab_external/install-matlab-engine-api-for-python-in-nondefault-locations.html