The machines in 3060 Torgersen only have 2 cores. This severely limits our ability to do any parallel demonstrations in class; particularly because parallel MATLAB typically runs by setting aside one core for the client, and the others for the "workers"!

So don't expect any speedup today. But we would like you to become familiar with what such programs look like and how they can be executed. In particular, the BATCH command cab be very useful for running parallel programs. BATCH can run a program "in the background" on your desktop, but if you have set up the proper configuration, you can also use the same command to send jobs to the Ithaca cluster.

This exercise is intended to familiarize you with the very basic features of the Parallel Computing Toolbox.

Start up MATLAB.

Enter the following commands:

for i = 1 : 1024 a(i) = sin ( i * 2 * pi / 1024 ) end plot ( a )

We could interactively run a parallel computation of the same
quantities. To do so, we open a MATLAB pool of workers.
Then we indicate which work is to be done in parallel by replacing
the **for** statement by a **parfor** statement:

matlabpool open local 2 parfor i = 1 : 1024 b(i) = sin ( i * 2 * pi / 1024 ) end plot ( b ) matlabpool close

Using the MATLAB pool interactively is OK for development on your
desktop. However, we can't work interactively on the Ithaca cluster,
so we need to know about the **batch** command, which runs a script file
in the background.

First, we have to create a script! Inside of MATLAB, type

edit mywavewhich will open up the MATLAB editor. Enter the text of the

for i = 1 : 1024 c(i) = sin ( i * 2 * pi / 1024 ) endNow save the file, and close the editor.

To execute the script **mywave**, we simply type

job = batch ( 'mywave' )and off it goes. No matter how much work is involved in the script, we get the MATLAB prompt back immediately. The script is running "in the background".

The simplest way of finding out if the job is done is to issue the command

wait ( job )which will NOT return your prompt until the job is finished.

The script computed the vector **c**. Notice that **c** does
not show up in the MATLAB workspace. To retrieve its value,
we type

load ( job, 'c' );and the computed value enters our MATLAB workspace, and now we can say

plot ( c )

Now edit the **mywave** script by changing the output vector to **d**, and
changing the **for** to **parfor**. Now issue the following command:

jog = batch ( 'mywave', 'matlabpool', 1 )Now we are running the command in the background, and in parallel. (If we have a maximum of N cores available, the

How do we know when the job is finished?

How do we get the value of the vector **d**?

This exercise should help you to understand the SPMD command. Inside an SPMD block, each worker is doing different tasks, but storing variables using the same names. Outside the SPMD block, the client can examine the different versions by using curly brackets.

Create an M file called "spmd_plot":

spmd a = ( labindex - 1 ) / numlabs; b = labindex / numlabs; x = linspace ( a, b, 100 ); y = sin ( 2 * pi * x ); end plot ( x{1}, y{1}, x{2}, y{2}, 'LineWidth', 2 )

Run the program by typing

matlabpool open local 2 spmd_plot matlabpool closeExplain what is going on.