# Matlab

## Contents

## General Description

MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use interactive environment where problems and solutions are expressed in familiar mathematical notation. This high-level language and interactive environment enables you to perform computationally intensive tasks faster than with traditional programming languages such as C, C++, and Fortran.

## Toolboxes

The Matlab Toolboxes licensed by ND changes each year based on user demand (measured by an annual survey). Faculty may also purchase toolbox licenses which were not centrally funded based on broad user demand. To see what toolboxes are currently available simply type **ver** in the Matlab Command Window.

## Basic Usage

### Submitting Single Core Jobs

To submit a single core MATLAB job to CRC systems use the following template:

```
#!/bin/bash
#$ -q long
#$ -pe smp 1
# [optional] The MATLABPATH variable is set in the Matlab script to add additional
# directories to the internal search paths.
export MATLABPATH=directory_path_to_your_files.m:other_user_contrib_directory_path
module load matlab
matlab -singleCompThread -nodisplay -nosplash < your_file.m
```

By default, recent versions of MATLAB will try to automatically multithread processing on all the cores available on a machine. If you are only requesting one core in your submission script, you need to invoke MATLAB with the following option (to avoid interfering with other user's running jobs):

matlab -singleCompThread ...

**If you request more than 1 core, maxNumCompThreads function should be used to control the maximum number of computational threads.** For example, adding the following line in your Matlab code will limit the number of threads to 6:

maxNumCompThreads(6)

Make sure that you request 6 cores in your job script (with **#$ -pe smp** 6)

### Submitting Multicore Jobs with MATLAB PCT

To submit multicore MATLAB jobs (up to 24 cores with current CRC systems) using the PCT (Parallel Computing Toolbox), use the following template(matlab has different versions installed on CRC, type module avail matlab to see different versions):

#!/bin/csh #$ -q long #$ -pe smp 12 setenv MATLABPATH path_to_matlab_file cd path_to_matlab_file module load matlab # The MATLABPATH variable is set in the Matlab script to add additional # directories to the internal search paths. matlab -nodisplay -nosplash < matlab_file

The matlab file above includes the command `parpool('local',12)`

, allowing Matlab to use the 12 cores on the machine.

Parallelization is done in a par for loop e.g.parpool('local', 12); parfor i=1:N end % In Matlab 8.5 need to use "delete(gcp('nocreate'))"

### PCT Example To Utilize Variable Number Of Cores

The script_submission.sh file:

#!/bin/csh foreach line ("`cat list_of_files.txt`") set argv = ($line) set file = $1 set num_shift = $2 rm -f job_submission cat > job_submission << EOF #!/bin/csh #\$ -N $file #\$ -pe smp 12 module load matlab/8.3 setenv FILE $file setenv NUM_SHIFTS $num_shift setenv NUM_CORES $NSLOTS matlab -nodisplay -nosplash < gen_max_likelihood_fminunc.m > result_$file EOF qsub -q long job_submission end

The MatLab file associated with the above script_submission.sh file:The matlab file gen_max_likelihood_fminunc.m [...] % THE REST OF THE PROGRAM BELOW IS GENERALIZED likelihood_maximizers = zeros((number_of_Xvars+3),1); standard_errors = zeros((number_of_Xvars+3),1); num_cores = getenv('NUM_CORES'); num_cores = str2num(num_cores); matlabpool ('open', 'local', num_cores) [...]

### MATLAB and Array Jobs

An example for combining MATLAB and job arrays is as follows:

#!/bin/csh #$ -N testarray #$ -t 1-4:1 #$ -M afs_id@nd.edu #$ -m ae setenv MATLABPATH directory_path_to_your_files.m:other_user_contrib_directory_path matlab -nodisplay -nosplash -nojvm -r "myFunction(${SGE_TASK_ID});exit"

The myfunction.m will receive respectively for each task 1,2,3 and 4 as an input parameter.

## Memory Profiling

User can use the builtin MATLAB profiler to understand memory usage in your scripts. To enable memory profiling add the following line to your scripts:

profile -memory on;

## License Information

- Matlab Parallel Computing Toolbox can be currently configured up to 64 cores (SMP) per simulation.
- We currently do not support a license for MATLAB Distributed Computing

## Tutorials and Training

- CRC presentation discussing Simulink and Matlab from February 5, 2014: Presentation.pdf

- For more information, please visit the official Matlab training page for a wealth of video tutorials and training content.

## Known issue with *parpool* and *matlabpool*

There is a known issue when submitting multiple jobs that use either of the Matlab pool commands. Basically, if more than one task is started close to another task, they may both try and write to the same temporary file and cause problems. One solution involves changing the name of the default file Matlab writes pool information to as described in this link:

## Further Information

See the official site: MATLAB

The User's Guide may be found * here*.

Default help files for version "X.Y" (replace X and Y with desired version) may be found at:

/afs/crc.nd.edu/x86_64_linux/m/matlab/X.Y/help

## Data Visualization Examples

If you come up with any non-standard data visualizations using MATLAB, please share your method with other users here.