MATLAB is a high-level language and interactive environment that enables you to perform computationally intensive tasks faster than with traditional programming languages such as C, C++, and Fortran.
Usage
module load 2021 module load MATLAB/2021a-upd3 matlab -nodisplay < matlabprogram.m
Available toolboxes
We have the following toolboxes available:
- Bioinformatics Toolbox
- Communications Toolbox
- Control System Toolbox
- Curve Fitting Toolbox
- DSP System Toolbox
- Deep Learning Toolbox
- Econometrics Toolbox
- Financial Toolbox
- Global Optimization Toolbox
- Image Processing Toolbox
- MATLAB Coder
- MATLAB Compiler
- MATLAB Compiler SDK
- Mapping Toolbox
- Optimization Toolbox
- Parallel Computing Toolbox
- Partial Differential Equation Toolbox
- Signal Processing Toolbox
- Statistics and Machine Learning Toolbox
- Symbolic Math Toolbox
Before you can use it, you have to contact SURF's HPC helpdesk to get a validation for the use of Matlab. The number of licenses available for all the users on the system is limited. We advise compiling your Matlab script to avoid the need for an available license when you run your code.
Compilation of Matlab programs
We advise compiling your Matlab script first to a binary, which can be run from a batch job. The compilation can be done on an interactive node and, once compiled, your code will not require access to an available license to run.
Compilation example
Here is an example:
# create a simple matlabprogram in the file 'matlabprogram.m': cat <<eof > matlabprogram.m rand(5) eof module load 2021 module load MATLAB/2021a-upd3 # create the environment needed by matlab echo "mcc -m matlabprogram.m" | matlab -nodisplay # call the matlab compiler mcc # If there are more scripts you need to compile, the command would be: # echo "mcc -m script1.m script2.m ..." | matlab -nodisplay module unload MATLAB/2021a-upd3 # just to prove you can run the resulting # program without the matlab environment module load MCR/R2021a-upd3 # for the run time environment ./matlabprogram # run the program
If everything goes all right, there will be a file called 'matlabprogram' that, when called, will produce a random order-5 matrix. The mcc
command generates some extra files which you can remove.
To run the compiled program, the module mcr
is needed for the run time environment. Running a compiled Matlab program creates a directory with lock files, the default is $HOME/.mcrCachex.y
. We recommend defining a unique directory on the local scratch disk by setting the environment variable MCR_CACHE_ROOT
(see the example above)
Job example
A job, running this program could be:
#SBATCH --ntasks 32 #SBATCH -t 00:30:00 module load 2021 module load MCR/R2021a.3 # create unique mcr cache directory on scratch: export MCR_CACHE_ROOT=`mktemp -d $TMPDIR/mcr.XXXXXXXXXX` ./matlabprogram
Reading and writing data from a compiled Matlab program
Matlab programs can be defined to accept a variable number of input arguments. The following example shows how to define a function that accepts two numbers in input and print their sum.
%myscript.m matlab code function result = myscript(a,b) if isdeployed a=str2num(a); end if isdeployed b=str2num(b); end result=a+b; disp("Result is:") disp(result) end
Once compiled with the Matlab compiler, as specified above, the resulting program can be invoked by passing the two arguments in the command line:
module load 2021 module load MATLAB/2021a-upd3 mcc -m myscript.m ./myscript 3 5
Often, Matlab programs read input data and write output data from and to folders specified by the user. On your own computer, you would simply specify the full path to the folder to which your program should write. However, if you copy the script to Snellius, these folders should, of course, be changed.
While you could still hard code the folder path in your Matlab program before you compile it, it is generally more flexible if you adapt your script in such a way that you can pass input and/or output folder (and potentially also a folder where your program can write temporary files) as arguments when calling your compiled matlab function.
Consider the following example script (which we have changed to a function, so we can pass arguments):
function my_script(input_dir, output_dir, temp_dir) % Suppose we want to read a file 'file1' from the input dir fullFilePathIn = sprintf('%s/file1', input_dir) data = importdata(fullFilePathIn) % ... Here we run our actual code, that may read/write to the temp_dir % And finally we write our result (the object 'out') to a file 'output' in the output_dir fullFilePathOut = sprintf('%s/output', output_dir) save(fullFilePathOut,'out','-ascii') A corresponding job script would then look something like this #SBATCH -n 1 -t 100 module load 2020 module load MCR/2020a #Create input, output folders on scratch, and copy input data from home to scratch mkdir ${TMPDIR}/input mkdir ${TMPDIR}/output cp file1 ${TMPDIR}/input #This assumes file1 is in the $SLURM_SUBMIT_DIR directory ./matlabprogram ${TMPDIR}/input ${TMPDIR}/output ${TMPDIR} #Copy results back to your home cp -r ${TMPDIR}/output . #This assumes you want to copy your output to the directory from which the job was submitted
Note that in this example, we have used the scratch file system (${TMPDIR}) for input, output and temporary files, as this is faster than the home file system.
Accessing the Software
Do to resource management and planning reasons, access to Matlab is not directly available. In order to use Matlab, you need to raise a request to our servicedesk specifying that you wish to be included in the matlab
group. Note that this does not require to own a licence.
Additional information
More explanation on the mcc
command is available with:
mcc | less
Homepage
http://www.mathworks.com/products/matlab