Compiling MATLAB code
The Matlab compiler "mcc" is a standalone environment that will allow MATLAB scripts to run without requiring a license. We recommend this method for Matlab codes that generate large number of license requests such as parallel or distributed code.
MATLAB R2022b and Newer
Compiling your MATLAB script
To begin, first load the MATLAB module. N.B. Use applicable release name for the matlab release, where you see <matlab-release>
substitute the release name with a lowercase 'r' e.g. "r2022b" where you see <release-version>
use the release name with uppercase 'R' e.g. *"R2022b"
$> module load matlab/<matlab-release>
To compile myMatlabScript.m with the MATLAB Compiler, you would use the following command:
$> mcc -v -R -nodisplay -R -singleCompThread -m myMatlabScript.m
$> mcc -?
Running your compiled MATLAB script
Your program can now be executed using the shell script generated during compilation, the environment variable MATLAB_RUN is set by the Matlab MCR module.
$> ./run_myMatlabScript.sh $MATLAB_RUN
Submitting your job to SLURM
Once you have your compiled script running correctly, you are ready to submit you job to the SLURM resource manager so that it can run on the cluster. Here is a sample SLURM script that you can use as a template your own script.
#!/bin/sh
## Specify the name for your job, this is the job name by which Slurm will
## refer to your job. This can be different from the name of your executable
## or the name of your script file.
#SBATCH --job-name MyMatlabJob
#SBATCH -p normal
## Deal with output and errors. Separate into 2 files (not the default).
## NOTE: %u=userID, %x=jobName, %N=nodeID, %j=jobID, %A=arrayMain, %a=arraySub
#SBATCH -o /scratch/%u/%x-%N-%j.out # Output file
#SBATCH -e /scratch/%u/%x-%N-%j.err # Error file
##SBATCH --mail-type=BEGIN,END,FAIL # NONE,BEGIN,END,FAIL,REQUEUE,ALL,...
##SBATCH --mail-user=<userID>@gmu.edu # Put your GMU email address here
## Specifying an upper limit on needed resources will improve your scheduling
## priority, but if you exceed these values, your job will be terminated.
## Check your "Job Ended" emails for actual resource usage info.
##SBATCH --mem=<X> # Total memory needed for your job (suffixes: K,M,G,T)
##SBATCH --time=<D-HH:MM> # Total time needed for your job: Days-Hours:Minutes
## These options are more useful when running parallel and array jobs
#SBATCH --nodes 1 # Number of nodes (computers) to reserve
#SBATCH --tasks 1 # Number of independent processes per job
## Load the relevant modules needed for the job
module load matlab/<matlab_release>
unset DISPLAY
## Start the job
./run_myMatlabScript.sh $MATLAB_RUN
Be sure to fill in the appropriate sbatch
command with your submission script.
Linking External Libraries to your MatLab Script
You may wish to run your MatLab script using a user-supplied library. To
do this you must first configure the build environment to use the
compiler used to compile the library. This is accomplished using the
mbuild
command. In this example we will use C
as the programming
language our library is compiled by, mbuild
can also be configured for
C++
and also FORTRAN
.
Remove or rename the file
\~/.matlab/
$> mv ~/.matlab/R2022b/MBUILD_C_glnxa64.xml ~/.matlab/R2022b/MBUILD_C_glnxa64.xml.orig
Now run the mbuild -setup command
$> mbuild -setup
This will create a new file: MBUILD_C_glnxa64.xml
with the appropriate
compiler paths configured.
There are a two different but equivalent ways of specifying the compile parameters with linking an external library.
Method 1, copy the file
~/.matlab/
/MBUILD_C_glnxa64.xml
to new name and edit
it to embed the necessary changes to CFLAGS
, INCLUDES
, and
LDFLAGS
, then compile with:
$> mcc -mv mymain.m myCfile.c -f <full_path_to_new_options_file>
The alternative method is to pass the compilation options directly in the command line:
$> mcc -M "-I<path/to/library/headers> -L<path/to/libraries> -l<library-to-link-1> -l <library-to-link-2> ..." -m myMatlabScript.m
-M
with override the other
entries.