2. Mandelbrot with Matlab

In this example, we'll create a Mandelbrot fractal with Matlab. First, we'll test our code interactively on a qrsh session and then we'll create some Matlab and SGE scripts to run the process in batch.

1. Interactive session:

Xshell:\> ssh -X ijimenez@hpc.dtic.upf.edu
Looking up host 'hpc.dtic.upf.edu'...
Host 'hpc.dtic.upf.edu' resolved to 193.145.51.37.
Connecting to 193.145.51.37:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Linux login 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64
[ ... ]
ijimenez@login:~$ qrsh
Linux node02 3.2.0-4-amd64 #1 SMP Debian 3.2.51-1 x86_64
[ ... ]
Last login: Thu Dec 12 13:26:11 2013 from 192.168.7.6
ijimenez@node02:~$ matlab & 

This is the source for the  Mandelbrot set we're going to create: http://www.mathworks.com/help/distcomp/examples/illustrating-three-approaches-to-gpu-computing-the-mandelbrot-set.html

The Mandelbrot code depends on four variables we set at the beginning, and will model the behavior of the Mandelbrot set:

>> maxIterations = 500;
gridSize = 1000;
xlim = [-0.748766713922161, -0.748766707771757];
ylim = [ 0.123640844894862,  0.123640851045266];

The full Matlab code is shown on the next figure:

If we execute the code, we'll obtain this figure:

 

2. Mandelbrot set in serial batch: 

Let's create the usual directory struct:

ijimenez@node04:~/Matlab$ mkdir Mandelbrot
ijimenez@node04:~/Matlab$ cd Mandelbrot
ijimenez@node04:~/Mandelbrot$ mkdir data
ijimenez@node04:~/Mandelbrot$ mkdir script
ijimenez@node04:~/Mandelbrot$ mkdir job-out
ijimenez@node04:~/Mandelbrot$ mkdir out

Let's create now the Matlab script:

ijimenez@node04:~/Mandelbrot$ cd script 
ijimenez@node04:~/Mandelbrot$ matlab

We can write it using Matlab's own editor. We'll introduce some modifications, as long as we are not going to handle a drawing but an image object:

% Show
cpuTime = toc( t );
%set( gcf, 'Position', [200 200 600 600] );
% imagesc( x, y, count );
% axis image
% colormap( [jet();flipud( jet() );0 0 0] )
% title( sprintf( '%1.2fsecs (without GPU)', cpuTime ) );
imwrite((count/max(count(:)))*255,jet,'/homedtic/ijimenez/Matlab/Mandelbrot/out/mandelbrot.png');

The full code of our simulation is shown below:

Some notes on the script:

a) we'll control the time trhourh the tic() - toc() commands.

b) The 'imwrite' command gets three values: the image matrix, the colormap and the file where we want to write the data. We apply a transformation to every element of the matrix to 'colorize' the value. Also, we'll use Matlab's builtin colormap 'jet'. 

c) We're going to write the output file in the /scratch/Mandelbrot/out rather than on $HOME, and we'll move the data back with the submission script:

#!/bin/bash
 
# Load modules directive
. /etc/profile.d/modules.sh
 
# Copy sources to the SSD:
#
 
# First, make sure to delete previous versions of the sources:
# -----------------------------------------------------------
if [ -d /scratch/Mandelbrot ]; then
        rm -Rf /scratch/Mandelbrot
fi
 
# Second, replicate the structure of the experiment's folder:
# -----------------------------------------------------------
mkdir /scratch/Mandelbrot
mkdir /scratch/Mandelbrot/data
mkdir /scratch/Mandelbrot/error
mkdir /scratch/Mandelbrot/script
mkdir /scratch/Mandelbrot/out
 
# Thir, copy the experiment's data:
# ----------------------------------
cp -rp /homedtic/ijimenez/Matlab/Mandelbrot/data/* /scratch/Mandelbrot/data
cp -rp /homedtic/ijimenez/Matlab/Mandelbrot/script/* /scratch/Mandelbrot/script
 
# Fourth, prepare the submission parameters:
# Remember SGE options are marked up with '#$':
# ---------------------------------------------
# Requested resources:
#
# Simulation name
# ----------------
#$ -N "Mandelbrot"
#
# Expected walltime: ten minutes maximum
# ---------------------------------------
#$ -l h_rt=00:10:00
#
# Shell
# -----
#$ -S /bin/bash
#
# Output and error files go on the user's home:
# -------------------------------------------------
#$ -o /homedtic/ijimenez/Matlab/Mandelbrot/job-out/Mandelbrot.out
#$ -e /homedtic/ijimenez/Matlab/Mandelbrot/job-out/Mandelbrot.err
#
# Send me a mail when processed and when finished:
# ------------------------------------------------
#$ -m bea
#$ -M  my.email@upf.edu
# Start script
# --------------------------------
#
# Print some informational data:
# ------------------------------
printf "Starting execution of job Mandelbrot with ID: $JOB_ID from user: $SGE_O_LOGNAME\n"
printf "Starting at `date`\n"
printf "Calling Matlab now\n"
printf "_____________________\n"
#
# Execute the Matlab script
# -------------------------
/soft/MATLAB/R2013b/bin/matlab -nosplash -nojvm -nodesktop -r "run /scratch/Mandelbrot/script/Mandelbrot.m"
#
# Copy data back, if any
# ----------------------
printf "Matlab processing done. Moving data back\n"
cp -rf /scratch/Mandelbrot/out/* /homedtic/ijimenez/Matlab/Mandelbrot/out
printf "_________________\n"
#
# Clean the crap:
# ---------------
printf "Removing local scratch directories...\n"
if [ -d /scratch/Mandelbrot ]; then
        rm -Rf /scratch/Mandelbrot
fi
printf "Job done. Ending at `date`\n"

That's it!. Submit the job:

ijimenez@login:~/Matlab/Mandelbrot$ qsub mandelbrot.sub 

and wait for it to end:

ijimenez@login:~/Matlab/Mandelbrot$ qstat
job-ID  prior   name       user         state submit/start at     queue                          slots ja-task-ID 
-----------------------------------------------------------------------------------------------------------------
 154071 0.47500 Mandelbrot ijimenez     r     01/07/2014 11:47:59 short.q@node06                     1  

Once it finishes, we'll get the output time in ./job-out/Mandelbrot.out:

Starting execution of job Mandelbrot with ID: 154071 from user: ijimenez
Starting at Tue Jan  7 11:47:59 CET 2014
Calling Matlab now
_____________________
Warning: No display specified.  You will not be able to display graphics on the screen.
Warning: No window system found.  Java option 'MWT' ignored.
 
                            < M A T L A B (R) >
                  Copyright 1984-2013 The MathWorks, Inc.
                    R2013b (8.2.0.701) 64-bit (glnxa64)
                              August 13, 2013
 
 
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
 
Elapsed time is 109.338394 seconds.
Matlab processing done. Moving data back.
_________________
Removing local scratch directories...
Job done. Ending at Tue Jan  7 11:49:53 CET 2014

and the output image file will be in ./out/mandelbrot.png. We can use the gpicview utility to view the file:

ijimenez@login:~/Matlab/Mandelbrot/out$ gpicview mandelbrot.png