HOWTO: Matlab Compilation Workflow

Introduction

Within a cluster computer setting, MATLAB can allocate as many CPU’s as it can. Due to that, there are only a limited number of nodes that have a few cores assigned to the MATLAB queue. As a solution, we can utilize a MATLAB feature that compiles a script to build a binary.This binary application can then be submitted to Scientific Computing (SciC) Linux Clusters as a job, via bsub. Using this method, MATLAB licenses are not required to run this compiled application. The instructions below, from MathWorks, show how to compile a MATLAB script to generate a standalone binary application.

 

Instructions

The instructions below, from MathWorks, show how to compile a MATLAB script to generate a standalone binary application. 

https://www.mathworks.com/help/compiler/create-and-install-a-standalone-application-from-matlab-code.html

Using theses instructions, this article will show you how to do the same in SciC Linux Clusters. This article will be composed of two parts. The first part will explain how to compile a MATLAB script into a binary. The second part is a how-to on installing and running the binary in SciC Linux Clusters.

 

Part 1: How To Compile a MATLAB Script Into a Binary

First, you will want to ssh to SciC Linux Clusters, load the MATLAB module,  send the job to the queue, and open the MATLAB window. In this how-to we will be using the 2017a version of MATLAB, but this process works with all versions from 2012b.

For this process to work on MAC, you must have the XQuartz app installed. You can find that here, https://www.xquartz.org/. Similarly, if you are using a Windows machine, you will need to download PuTTY. You download it from this link, https://www.putty.org/.

$ ssh -Y erisone.partners.org
hp911@erisone.partners.org's password:

[hp911@eris1n2 ~]$ module load matlab/2017a
[hp911@eris1n2 ~]$ bsub -Is -XF bash
Job <128779> is submitted to defau lt queue <interact>.
<<ssh X11 forwarding job>>
<<Waiting for dispatch ...>>
<<Starting on cmu016>>
[hp911@cmu016 ~]$ matlab &
[1] 13398

Once MATLAB is opened, you will need to open your script. For this example we will be using the script provided from the MathWorks site 

function m = magicsquare(n)
if ischar(n)
    n=str2num(n);
end
m = magic(n)

 

the ribbon above, select the ‘APPS’ tab from there select ‘Application Complier’. If you do not immediately see this option, then select ‘Application Deployment’ > ‘Application Compiler’”

 

Located at the top of the header select ‘Add Main File’ and choose the script you would like to compile. In our case it is “magicsquare.m”. Fill in the ‘Application Name’ and any other information about the application. At this step, be sure to include all files needed for the application to run. Click “Package”, provide the project folder name and wait, it may take a couple of minutes. Once completed, you can close the Matlab compiler window.

Back in the MATLAB session, you should see the project directory you have set. Within that folder you should see three directories and a PackingLog.html.

$ls for_redistribution  for_redistribution_files_only_for_testing  PackagingLog.html 

for_redistribution_files_only directory contains:

$ls default_icon_48.png  magicsquare  readme.txt  run_magicsquare.sh  splash.png

magicsquare is the binary file run_magicsquare.sh is the wrapper bash file.

Congratulations! You have compiled your MATLAB script and generated a binary. Next, we have to run the binary.

 

Part 2: Running a Binary in SciC Linux Clusters

In order to run a binary file, the MATLAB Runtime library is required. If you do not have it installed, you will need to do so. The next set of steps will show the process.

The library’s location depends on the MATLAB compiler version. The library installer’s location can be found by typing “mcrinstaller” into the MATLAB console.

>> mcrinstaller
ans =
'/apps/source/matlab/2017a/toolbox/compiler/deploy/glnxa64/MCRInstaller.zip'
>>

Using your terminal make a new directory that you can copy the above zip file to. After copying the zip file, unzip it.

$ ls archives  bin  install  java  MCRInstaller.zip  MCR_license.txt  productdata  sys  ui
$ ./install
Preparing installation files ...
Installing ...

 

 

Be sure to keep track of where the installation folder is located as you will need that to execute the binary.

 

Once the library Is installed, we can now run the executable binary. In your terminal, cd to the for_redistribution_files_only folder that is inside your MATLAB project and run the binary.

In our example below, ‘./run_magicsquare.sh’ is the binary we are executing, ‘~/local/MATLAB/MATLAB_Runtime/v92’ is where we installed MATLAB Runtime, and 5 is the parameter we are passing to our script.

$ ./run_magicsquare.sh ~/local/MATLAB/MATLAB_Runtime/v92/ 5
------------------------------------------
Setting up environment variables
---
LD_LIBRARY_PATH is .:/PHShome/hp911/local/MATLAB/MATLAB_Runtime/v92//runtime/glnxa64:/PHShome/hp911/local/MATLAB/MATLAB_Runtime/v92//bin/glnxa64:/PHShome/hp911/local/MATLAB/MATLAB_Runtime/v92//sys/os/glnxa64:/PHShome/hp911/local/MATLAB/MATLAB_Runtime/v92//sys/opengl/lib/glnxa64

m =

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9


 

Go to KB0035526 in the IS Service Desk

Related articles