Running Matlab on Kay
This tutorial covers a typical scenario where we would like to run a Matlab script at the command line.
We have the following very simple script:
A = [0.756,0.262,0.877,0.371,0.142,0.810,0.797,0.262;... 0.754,0.401,0.618,0.611,0.607,0.819,0.0960,0.325;... 0.0223,0.454,0.975,0.646,0.556,0.103,0.712,0.450;... 0.319,0.0207,0.748,0.842,0.211,0.249,0.0722,0.753;... 0.547,0.810,0.374,0.999,0.180,0.127,0.573,0.0220;... 0.773,0.184,0.0847,0.881,0.909,0.536,0.0621,0.763;... 0.0313,0.195,0.766,0.798,0.831,0.600,0.391,0.985;... 0.341,0.293,0.168,0.894,0.891,0.634,0.678,0.694]; disp(A) v1 = A(1,:); disp(v1) tic n = 200; V = 500; a = zeros(n); for i = 1:n a(i) = max(abs(eig(rand(V)))); end toc
which we save as example1.m. The script creates a 8x8 matrix A, prints A, then prints its top row. Then we time a for loop which does a random intensive computation.
The key step is how to run this script on the command line. Remember that Kay is a linux HPC cluster and does not have a GUI, which may be how people are more accustomed to using Matlab. However, luckily Matlab allows a command-line interface. We illustrate with an example slurm script which runs the above m-file in command line mode.
#!/bin/sh #SBATCH -p DevQ #SBATCH -N 1 #SBATCH -t 00:10:00 #SBATCH -A account_name # This job's working directory cd $SLURM_SUBMIT_DIR module load matlab matlab -nodisplay -nosplash -nodesktop -r "run('/ichec/work/project/example1.m');exit;"
Notice that we must first load the matlab module with 'module load matlab'. Then we invoke matlab with -nodisplay -nosplash -nodesktop to supress all gui features, and the -r file to run the specified m-file.
This is a very simple example that shows you how to get up and running with Matlab in the command line. Note that the parallel processing toolbox is available, but bear in mind it can be difficult to get a speedup with this as it is tailored for quite large problems - users used to shared-memory parallel platforms such as OpenMP may be initially frustrated with Matlab's parfor parallel-loop construct, which for small, even single-node problems may not appear to give a speedup.
Any text output from Matlab will be printed to the slurm output file. You cannot plot to screen when running a slurm script, but you can either save the intermediate data or plot directly to a file, as shown in the following Matlab example:
x = -2*pi:0.01:2*pi; y = sin(x); h = figure('Visible','off'); plot(x,y); print(h,'-dpng','SineWave.png'); close(h);