mcc Matlab Compiler
Because of limited availability of Matlab tokens (licenses) the CAC strongly encourages all users of Matlab code that can use the Matlab Compiler, mcc, to do so.
The Matlab Compiler, mcc, takes mcode and converts it into a C executable that can be run without Matlab, or dynamic libraries that can be used in C and FORTRAN code. It also supports most toolboxes. This compiled code then can be run without the need for Matlab or toolbox tokens (licenses). This allows users of the CAC systems to run many more copies of Matlab code than could be run with Matlab. This code can also be run on like systems that do not even have Matlab installed.
Compiling mcode with mcc
Your main mcode file needs to be have the function added to control the number of threads it will try to use. In most cases we found threads to not be beneficial.
maxNumCompThreads(1);
Even compiled mcode requires that the Matlab module be loaded. Also make sure you use version 2008b or newer.
Compile the mcode in the file tocmpile.m with the mcc command.
mcc -m tocmpile.m
The C executable tocmpile will be created that may be run without Matlab.
If you have your own custom functions that are stored in their own mfiles called by tocmpile.c you must tell the compiler where to find these mfiles. Note that the compiler will not give warnings about un-found mfiles (functions) until the final code is found. This is done with the -I flag. There may be multiple -I's.
Include mfiles in the directory /home/username/mcodes/.
mcc -m tocmpile.m -I /home/username/mcodes/
Error messages when running compiled of of the following means that the values of -I should be checked:
??? Undefined function or variable 'functionname'.
If you continue to receive Undefined function or variable messages, it could be you are using function pointers which requires you to force the addition of the needed mfiles to the CTF archive. Using the below option has also been known to solve other run time issues. The -a option may be used more than once.
Force include mfiles from directory.
mcc -m tocmpile.m -a /home/username/mcodes/*.m
Functions may also be compiled. Arguments are passed on the Linux Command Line at run time. This allows a single compiled mfile to be ran multiple times with different inputs.
Matlab functions that would be called as:
matlab -r myfcn\(arg1\)
Compiling myfcn.m will then be ran as:
./myfcn arg1
In most cases users wish for arg1 to be numeric, the compiled version will see it as a string. To fix this add in this small bit of code to parse the string into a number.
%code to change string to number function m = magicsquare(arg1) maxNumCompThreads(1); % by the input parameter arg1 % Copyright 2003-2006 The MathWorks, Inc. if ischar(arg1) arg1=str2num(arg1); end m = magic(arg1);
Startup perofrmance of compiled code can be poor if using a network filesystem like /home or /nobackup, for the MCR cache directory, the variable MCR_CACHE_ROOT lets the cache relocate to the local node.
#define the location, unique to job export MCR_CACHE_ROOT=/tmp/mcr_cache_$PBS_JOBID #create the cache space mkdir -p $MCR_CACHE_ROOT
Compiled mcode PBS script
A sample PBS script that shows a 1-CPU Matlab job that is compiled looks like this:
#PBS -N matlab1
#PBS -q route
#PBS -l nodes=1,mem=1gb,walltime=24:00:00
# note the lack of a need for gres here
#PBS -M your-email-address
#PBS -m abe
#PBS -V
#
echo "I ran on:"
cat $PBS_NODEFILE
#
#Setup MCR cache directory locally
export MCR_CACHE_ROOT=/tmp/mcr_cache_$PBS_JOBID
mkdir -p $MCR_CACHE_ROOT
#
#cd to your execution directory first
cd /home/your-user-name/your-matlab-directory
#Run the compiled mcode tocmpile.m
./tocmpile
#


