Software Exercise 2.4: Running Compiled Matlab¶
The goal of this exercise is to compile Matlab code and run it. This exercise will draw on the idea of writing a wrapper script to install and run code, first introduced in Exercise 1.2 and should take 25-30 minutes.
Matlab is licensed; however, unlike most licensed software, it has the ability to be compiled and the compiled code can be run without a license. We will be compiling Matlab
.m files into a binary file and running that binary using a set of files called the Matlab runtime.
- Log in to the CHTC submit server (
- Create a directory for this exercise and
cdinto it .
- Copy the following code into a file called
A = randi(100,4,4) b = randi(100,4,1); x = A*b save results.txt x -ascii
Compiling Matlab Code¶
The first step in making Matlab portable is compiling our Matlab script.
To compile this code, we need to access the machines with the Matlab compiler installed.
For this exercise, we will use the compilers installed on special CHTC build machines.
In the CHTC pool, you can't use
ssh to directly connect to these machines.
Instead, you must submit an interactive job (similar to Exercise 2.2) that
specifically requests these build machines.
Create a file called
compile.submitwith the lines below:
log = compile.log should_transfer_files = YES when_to_transfer_output = ON_EXIT transfer_input_files = matrix.m +IsBuildJob = true request_memory = 1GB request_disk = 512MB queue
You can initiate the interactive job by using
-ioption. Enter the following command:
[email protected] $ condor_submit -i compile.submit
Make sure you've submitted this command from
learn.chtc.wisc.edu! Once the job starts, continue with the following instructions.
Since you are a guest user on our system, you will need to set your
HOMEdirectory by running this command:
[email protected] $ export HOME=$PWD
The Matlab software on these build servers is accessible via modules, just like the software installed on OSG Connect. Check which modules are available and then load the older version of Matlab.
[email protected] $ module load MATLAB/R2015b
Once the module is loaded (you can check by running
module list), compile the
matrix.mfile with this command:
[email protected] $ mcc -m -R -singleCompThread -R -nodisplay -R -nojvm matrix.m
The extra arguments to the
mcccommand are very important here. Matlab, by default, will run on as many CPUs as it can find. This can be a big problem when running on someone else's computers, because your Matlab code might interfere with what the owner wants. The
-singleCompThreadoption compiles the code to run on a single CPU, avoiding this problem. In addition, the
-nojvmoptions turn off the display (which won't exist where the code runs).
To exit the interactive session, type
Now that you're back on the submit server, look at the files that were created by the Matlab compiler. Which one is the compiled binary?
The newly compiled binary will require the 2015b Matlab runtime to run. You can download the runtime from the Mathworks website and build it yourself, but to save time, for this exercise you can use the pre-built runtimes hosted by CHTC.
- Download the 2015b Matlab runtime hosted by CHTC:
[email protected] $ wget http://proxy.chtc.wisc.edu/SQUID/r2015b.tar.gz
We will need a wrapper script to open the Matlab runtime and then run our compiled Matlab code. Our wrapper script will need to accomplish the following steps:
- Unpack the transferred runtime
- Set the environment variables
- Run our compiled matlab code
Fortunately, the Matlab compiler has pre-written most of this wrapper script for us!
Take a look at
run_matrix.sh. Which of the above steps do we need to add? Once you have an idea, move to the next step.
We'll need to add commands to unpack the runtime (which will have been transferred with the job). Add this line to the beginning of the
#!/bin/bashand the comments, but before
tar -xzf r2015b.tar.gz
readme.txtto determine what arguments our wrapper script requires. Once you have an idea, move to the next step.
The name of the Matlab runtime directory is a required argument to the wrapper script
run_matrix.sh. We'll have to do a little extra work to find out the name of that directory. Run this command
tar -tf r2015b.tar.gz
The output of the previous command is a list of all the files in the tar.gz file. What is the name of the first folder of the path for each file? This is the name of the runtime directory, and the argument you should pass to
Submitting the Job¶
Copy an existing submit file into your current directory. The submit file we used for Exercise 2.2 example would be a good candidate, as that example also used a wrapper script.
Modify your submit file for this job.
Check your changes against the list below.
executablefor this job is going to be our wrapper script
executable = run_matrix.sh
You need to transfer the compiled binary
matrix, as well as the runtime
transfer_input_files = matrix, r2015b.tar.gz
The argument for the executable (
run_matrix.sh) is "v90", as that is the name of the un-tarred runtime directory.
arguments = v90
We need to request plenty of disk space for the runtime.
request_disk = 2GB
Submit the job using
After it completes, the job should have produced a file called