Using the MEX Compiler
This is a short tutorial on how to build and use MEX files on the cluster. MEX files allow to use codes written in Fortran or C/C++ to be used within Matlab, mainly for better performance or to include libraries not available in Matlab. See the Matlab documentation for more details on MEX files, e.g here .
It is assumed you know how to write the needed wrapper function for a MEX file.
Building a MEX file on the cluster
If you open a terminal on your computer and ssh the cluster, you can build MEX files after loading the modules gcc and Matlab, e.g.
module load gcc module load matlab/r2014b
After that, change to the directory where you have stored your Fortran or C/C++ code, e.g.
Here we assume the file timestwo.F is located in this directory. Then the command
should build the MEX file on the cluster. If you type ls you should therefore see
Alternatively, you can build a MEX file during a Matlab job which you have submitted to the cluster. This can be done by adding the following lines to your Matlab script:
cd('/data/work/hrz/abcd1234/MATLAB/R2014b/mex'); [status, cmdout] = system('module load gcc matlab/r2014b; mex timestwo.F')
This would build the MEX file from the code as before, however you still need to copy the source code to the cluster first (it maybe possible to include the code file in the list of attached files, however this was not tested yet).
If the code is not changed you only need to build the MEX file once on the cluster.
Using a Function from a MEX file
Once the MEX file has been build on the cluster you can call this function from a Matlab script submitted as a job. Within the script you either need to cd into the directory where the MEX file is located:
or you add the directory to the Matlab path:
After this Matlab will be able the find the MEX file when the implemented function is called. In the example
x = timestwo(7)
would use the MEX file and x would have the value 14 once the job has completed (you need to load the jobData of course).