© 2008 by the Rector and Visitors of the University of Virginia.

The information contained on the University of Virginia’s Department of Information Technology and Communication (ITC) website is provided as a public service with the understanding that ITC makes no representations or warranties, either expressed or implied, concerning the accuracy, completeness, reliability or suitability of the information, including warrantees of title, non-infringement of copyright or patent rights of others. These pages are expected to represent the University of Virginia community and the State of Virginia in a professional manner in accordance with the University of Virginia’s Computing Policies.

Compiling Matlab M-Files on the Aspen and Cedar Linux Clusters, and the Oak Solaris Cluster

Compiling Matlab M-Files on the Linux and Solaris Clusters

This document describes how to use the Matlab compiler to compile and run Matlab M-files as multiple concurrent standalone applications on the Aspen Linux cluster, the Cedar Linux cluster, and the Oak Solaris cluster. This allows users to run the same Matlab program with different inputs (e.g. parameter space studies) effectively in parallel, and without using multiple Matlab network licenses.

This page will be modified to include the Oak Solaris cluster.

Table of Contents


Matlab Compiler Overview

The Matlab Compiler takes function M-files as input and creates redistributable, stand-alone applications or software components that are platform specific. The Matlab Compiler can generate

The Matlab Compiler supports nearly all the functionality of Matlab, including objects. Some toolboxes will not compile with Matlab Compiler 4.

The advantages of compiling matlab M-Files as stand-alone executables to run on the Linux clusters are,

The following is a link to the MathWorks Matlab Compiler Documentation.

Configuring Your Account

The following steps are necessary and specific to accounts on the Linux Clusters to use the Matlab Compiler and to run the resulting standalone executables.
  1. Edit your startup file (e.g. .variables.ksh) so that your LD_LIBRARY_PATH variable automatically includes the platform specific Matlab libraries needed by the standalone executable. For example, you could add the following statements (verbatim) to your .variables.ksh file.
    # Set LD_LIBRARY_PATH for Matlab standalone executable 
    module add mcc
    
    This module sets the LD_LIBRARY_PATH variable for the Matlab libraries and will auotmatically be updated with each new version of the compiler.

    If you have previously set LD_LIBRARY_PATH using the version specific directory paths to the Matlab libraries, e.g.

    OS=`/uva/bin/cpuarch`
    case $OS in
      Linux ) LD_LIBRARY_PATH=/common/matlab/7.01/bin/glnx86:\
    /common/matlab/7.01/sys/os/glnx86:\
    /common/matlab/7.01/sys/java/jre/glnx86/jre1.4.2/lib/i386/client:\
    /common/matlab/7.01/sys/java/jre/glnx86/jre1.4.2/lib/i386:\
    /common/matlab/7.01/sys/opengl/lib/glnx86:${LD_LIBRARY_PATH} 
    XAPPLRESDIR=/common/matlab/7.01/X11/app-defaults ;;
    esac
    
    you should delete these lines from your startup file.

  2. To make sure you are using the system default Matlab compiler options file rather than a local one in your account, run the following command from the Linux command line to remove any local compiler options file from your account.
     rm ~/.matlab/R14/mbuildopts.sh 
    

Using the Compiler

The Matlab Compiler will only compile function M-files, so it will be necessary to convert a script M-file to a function M-file as described in the following link to the Matlab Compiler documentation

To test the compiler and account configuration copy the example M-file /common/matlab/7.1/extern/examples/compiler/magicsquare.m to your own account, From within Matlab compile the function magicsquare.m as follows:

>> mcc -m -v magicsquare
An stand-alone executable file with the name magicsquare should be create in the same directory as magicsquare. Run the stand-alone application from the Unix shell prompt by typing the executable name.
magicsquare 4 
The results should be displayed as
ans =
    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1

Example M-file for Concurrent Simulations

The following is an example of how to write a function M-file that can be compiled once and then be used to run multiple concurrent simulations using different input and output data sets.
function matlab_sim()

% Read in program initialization parameters from text file 
% e.g. see sim_input1 below 

% First read in a character string to be used to identify the simulation number
% e.g. the character 1.
sim_num=input('simulation identifier > ','s')

% Example of reading in numeric vector  used for initialization for simulation
vector_input=eval(input('vector input > ','s'))

% Example of loading data from .mat file with filename based on simulation number,
% e.g. file_in1.mat
% This assumes the .mat file already exists containing some data.
load(['file_in',sim_num,'.mat'])

% Other Matlab code can be included in the body of this function M-file.

% Create some matrix a
a=rand(5,5);

% Save variable a to .mat file with filename based on simulation number
% e.g. file_out1.mat
save(['file_out',sim_num], 'a')

% Alternatively, save variable a to ascii file with filename based on simulation number
% e.g. junk1
fid = fopen(['junk',sim_num],'w');
fprintf(fid,'%12.8f %12.8f %12.8f %12.8f %12.8f \n',a);
fclose(fid);
If this function were run from the Matlab prompt, Matlab would prompt the user for the input information. Once this M-file is compiled as a standalone, it can get its simulation specific data from a text file (e.g. sim_input1) with the Unix redirect command like the following:
matlab_sim < sim_input1

The input file for matlab_sim is sim_input1 and contains the following data:

1
[3 6 9]
The first line is read in as a character string to identify specific input and output files for the simulation run. The next line is an example of a vector of numeric initialization data for the simulation.

This way you can run different concurrent simulations by using different versions of the text file sim_input1 (e.g. sim_input2, sim_input3, etc.) without having to recompile your code.

PBS Job Command File

The following is an example of a PBS command file used to submit the Matlab stand-alone exectuable matlab_sim (after compilation) for the specific simulation defined by the input file sim_input1 to the Linux clusters. Further information on PBS command files can be found in the Aspen Cluster Tutorial

#!/bin/sh
#PBS -l nodes=1:ppn=1
#PBS -l walltime=12:00:00
#PBS -o output_filename
#PBS -j oe
#PBS -m bea
#PBS -M userid@virginia.edu

cd $PBS_O_WORKDIR
./matlab_sim < sim_input1