### MATlab

```MATLAB
CENTER
FOR
INTEGRATED RESEARCH
COMPUTING
http://info.circ.rochester.edu/Summer_School_Workshops/Matlab.html
Outline
Part I – Interacting with Matlab
 Running Matlab interactively from the command line
 Submitting batch jobs to Matlab
 Matlab scripts, functions, paths.
 Accessing the GUI environment
Part II – Speeding up Matlab Computations
 Using the Code Profiler and the Code Analyzer
 Symmetric Multi-Processing with Matlab
 Accelerating Matlab computations with GPUs
 Running Matlab in distributed memory environments
Part III – Mixing Matlab and Fortran/C code
 Compiling MEX code from C/Fortran
 Turning Matlab routines into C code
Running Matlab interactively from the command line
 module load matlab
 srun -t 60 --pty matlab –nodesktop
Submitting Batch Jobs
 sbatch matlab.slurm
#!/bin/bash
matlab.slurm
#SBATCH -t 60
matlab -r "example1"
example1.m
nprimes=0;
primes=[];
for i=2:100
if (all(mod(i,primes(1:nprimes)) ~= 0))
nprimes=nprimes+1;
primes(nprimes)=i;
end
end
primes
Submitting Batch Jobs
 sbatch matlab2.slurm

Can also use 'here files' to pass
commands to matlab via
standard in
#!/bin/bash
matlab2.slurm
#SBATCH -t 60
matlab <<EOF
nprimes=0;
primes=[];
for i=2:100
if (all(mod(i,primes(1:nprimes)) ~= 0))
nprimes=nprimes+1;
primes(nprimes)=i;
end
end
primes
EOF
Submitting Batch Jobs
 sbatch matlab3.slurm
#!/bin/bash
matlab3.slurm
#SBATCH -t 60
 Or you can put the entire
program in quotes.
matlab -r " \
nprimes=0; \
 Use commas to separate lines
primes=[]; \
that you want the results from
for i=2:100; \
if (all(mod(i,primes(1:nprimes)) ~= 0)); \
 We are using the back slash \ as a
nprimes=nprimes+1; \
line continuation character for
primes(nprimes)=i; \
bash.
end; \
end; \
primes \
"
Calling Functions or Scripts not in your cwd
 You can use the addpath command before calling scripts or
functions.
#!/bin/bash
/home/jcarrol5/matlab5.slurm
#SBATCH -t 60
matlab -r "addpath('/public/jcarrol5/matlab'); example1"
Exercise 1
 Run the following matlab program on bluehive to
approximate the value of pi. (or one of your own
programs)
N=1e6
x=rand(N,1)
y=rand(N,1)
pi_approx=sum(x.^2+y.^2 < 1)/N*4.0
Accessing the GUI features
 To use Matlab's GUI you must connect through suitable environment
 Both MobaXterm (Windows) and Terminal (Mac/Linux) support X11
forwarding necessary for accessing the Matlab GUI.
 X2go provides a remote desktop session within the BlueHive custer.
 Why X2go?

Faster than using X11 forwarding (compresses data)

Has clients for all major operating systems

Saves your session when you are disconnected

You don’t have to restart Matlab if your network connection drops.
Running interactive jobs with X11 forwarding
 Before we ran the following to launch an interactive matlab
session (without the Matlab GUI)

srun -t 60 --pty matlab -nodesktop
 To use the GUI we have to use the interactive script


interactive -t 60
matlab
 There is also a 'JobLauncher' utility which you can access
through the Gnome Menu Bar
Using the terminal but with GUI support
 You can connect with X11 forwarding and still start matlab
without a desktop

matlab -nodesktop
 Figure windows will still pop up etc...
 You can also make plots without a GUI using
imagesc(rand(100)));
print('-dpdf','-r300','fig1-batch.pdf');
Matlab Code Analyzer and Profiler
 Matlab has sophisticated tools for analyzing and profiling
code. It will often offer suggestions on how to speed up