MATLAB Compiler Runtime (MCR) and Job Farming
Introduction
The purpose of the MATLAB Compiler Runtime is to run MATLAB standalone applications on systems without a MATLAB installation. The standalone application is compiled by the MATLAB Compiler which requires a licensed version of MATLAB. In contrast, the standalone application should be executable on any system providing a MCR installation, and without the need of a MATLAB license. On LRZ systems we provide all MCR versions which correspond to the installed MATLAB versions.
MCR-based standalones may run embarrassingly parallel. So-called MPMD (Multiple-Program Multiple-Data) or job-farming techniques are used to run many processes of a program simultaneously and independently, that is, without the need of communication. Typical applications are:
- parameter studies,
- independent processing of large datasets which are split into subsets.
Example: Basic Parallel MPMD Application with MATLAB MCR
Prerequisites
- The job must be parallelizable. That is, the work can be divided into independent subtasks (e. g. embarrassing parallelization).
- The compilation of the application's MATLAB script(s) must be feasible. That is, some MATLAB components may not be compiled or my not be available in the MCR.
Now what? A brief summary
- Your MATLAB script needs to be serial with respect to PCT parallelization. If necessary, convert your MATLAB script, e. g. by removing parfor loops. The work of the parfor loops will be done by the MPMD concept. MATLAB's intrinsic multithreading is still usable.
- Create a standalone application by compiling your script(s) within MATLAB.
- The execution of the standalone application will require the MATLAB Compiler Runtime (MCR).
- In your Slurm job script, run the standalone application via MPI. MPI will distribute the application to all resources you have allocated in the job. That is, multiple instances of your application will run simultaneously and independently.
- How does each instance of my application know, what task to do?
MPI assigns an ID to each instance (in case of Intel MPI stored in environment variable PMI_RANK), which can be evaluated within the job script or even the MATLAB script.
General Workflow
The following table depicts the procedure from creating the standalone application to the submission of a Slurm job.
Step | Comment |
---|---|
1. Create folder structure of example | |
mcr_mpmd_example ├── bin ├── data ├── mcr_build.m ├── mcrjob.cmuc2.slurm ├── output └── src ├── dep1 │ └── matmul_serial.m ├── dep2 │ └── plot_save_matrix.m └── mcr_run.m | -> base path -> path for executable file -> path to data files created by application (log files, plots) -> build script running MATLAB Compiler -> Slurm script to run MCR job -> directory for Slurm job output files -> application sources: m-files -> dependencies -> serial matrix-matrix multiplication -> dependencies -> plot result -> run script of application ("main" function) |
2. Prepare application sources | |
Within the scope of the job-farming approach, MPI will be used to spawn multiple instances of compiled mcr_run.exe. Therefore, each instance needs to identify its individual sub-task. Consequently, we need one important modification. The easiest way: Each instance obtains its MPI rank from the MPI environment. The rank will be used to identify sub-task(s) assigned to each instance. The MPI rank is unique. In general, the range is 0, 1, ..., ("number of Slurm tasks" - 1). | |
| |
3. Check build script | |
This is a generic build script which can be used for any use case. It should not be necessary to modify it. | |
4. Compile application and set options to disable/enable multithreading in compiled application | |
1. Load Matlab module > module switch spack/23.1.0 > module load <MATLAB MODULE> # EDIT HERE (see supported releases) 2. Compile (multithreading in application disabled) > matlab -nodisplay -r "mcr_build('./src/','mcr_run','disableThreading');quit" 2. Compile (multithreading in application enabled) > matlab -nodisplay -r "mcr_build('./src/','mcr_run','enableThreading');quit" 2. Compile (multithreading in application enabled) > matlab -nodisplay -r "mcr_build('./src/','mcr_run');quit" | The "standalone" application will be created. In this example we will get "mcr_run.exe". This step can be done on any computer providing a Matlab installation and all necessary toolboxes. The MATLAB release version used for compilation has to match the MCR version at runtime! The update level may be different. If you work on the Linux Cluster, please load the desired MATLAB module and then call the command to compile. It is allowed to run this compilation procedure on the login nodes. |
5. (A) Prepare Slurm batch script | |
> module switch spack/23.1.0 > module avail matlab-mcr | First panel:
Second panel:
The MATLAB release version used for compilation has to match the MCR version at runtime! The update level may be different. In the script, you need to load a Matlab MCR module, not Matlab! |
5. (B) Adjust Slurm batch script on SuperMUC-NG | |
Workaround for "missing library" error Jobs (mainly on SuperMUC-NG) may show the following error: libXt.so.6: cannot open shared object file: No such file or directory As a workaround you may modify the LD_LIBRARY_PATH environment variable in your job script. | |
6. Run job | |
For example: > sbatch mcrjob.supermuc-ng.slurm |
Example
Construction site