If you run MATLAB jobs routinely, you may have had the experience of denied MATLAB access or having your MATLAB batch jobs killed due to unavailable MATLAB licenses. This can be avoided if you compile your MATLAB application into an executable with the MATLAB mcc compiler. A standalone executable runs without using a MATLAB license, and has several other benefits.

Benefits of standalone executables

  1. Broader distribution: Extend the reach of your program to users who lack a MATLAB license.
  2. Access reliability: Standalone executables never fail due to license availability.
  3. Embarrassingly parallel applications: Hundreds or thousands of independent runs are common for many applications. Using a standalone makes it possible to run these tasks concurrently — e.g. on the SCC cluster — without running into licensing issues.
  4. Hide source code for intellectual property protection

To be compatible with the MATLAB mcc compiler, your program must meet a few requirements. The most important of these are listed below.

  • The starting, or “main,” program must be a function — with or without returning output. The standalone, regardless, will not return any output. You can use either the original main or you create a command function wrapper as the new “main” which in turn calls the original main. For many, a wrapper may be more appealing as it enables the original program to be kept completely separate from the corresponding standalone program.
  • All runtime input parameters are always passed to the standalone as strings. Any passed string parameter intended as double in the code must be converted with str2double. Your wrapper will perform data type conversions (if any) and pass them to the original main for further processing.

Compilation

scc1% module load matlab/2016a 
scc1% mcc -mv -o myExecR2016a myStandalone.m myApp.m 
  • -mv produces a standalone and shows actions taken
  • -o myExecR2016a specifies the executable name. Though not required, “R2016a” suffix in the output name indicates MATLAB release used for compilation.

Running Standalone executable interactively

scc1%  qrsh -pe omp 4 
scc-pi4% module load mcr/9.0.1_2016a 
scc-pi4% mcr ./myExecR2016a 2000 4 
scc-pi4% exit 

Submit a job

Example Source Files

Additional notes

myStandalone.m is an optional application-dependent wrapper and is a command function. Note that, because myApp.m takes two input parameters — by design rather than mandatory — myStandalone.m retains the same 2 run time input.
The following command might need to be added into your matlab script to help myStandalone.m find files it needs:

>> addpath my/script/dir   % your m-files folder
  • Recompiling required whenever changes are made to one or more m-files.
  • For batch processing, mcc runtime option (e.g., -R -singleCompThread) may be required if you run a single threaded job.
scc1%  mcc -mv -o myExec myStandalone.m myApp.m -R -singleCompThread -R -nodisplay
  • -R -singleCompThread turns off multithreading while -R -nodisplay disables graphics display.
  • The standalone built with these runtime switches will always run with single thread and will not display graphics — in batch or interactive.