VT FDI 2009
MATLAB Exercises


During the FDI sessions, we should have access to MATLAB both locally on the desktop machines and remotely, by logging into sysx1 and from there to the matlab1 cluster.

The local machines are only dual core; MATLAB will allow you to run in parallel with up to 4 workers, but you won't see much speedup.

The matlab1 system consists of 4 dual core nodes. Using the appropriate commands, you can ask for as many as 8 workers. That's enough parallel power to start seeing some improvements. (When the big MATLAB system arrives, we will be able to ask for up to 64 workers, and soon 128!)


Exercise 1: Run HELLO on the Desktop

The "hello" program simply prints out "Hello, world!". We use it as a simple way of warming up, and making sure that we understand how to run MATLAB.

On your desktop machine, start MATLAB by clicking on the MATLAB icon. You will get an interactive command window. Now we need to type some cd commands in this window so that we move from MATLAB's current directory to the FDI examples folder. This is a little tricky, so we will all do this together.

If you reach the correct directory, you should be able to type the an "ls" command and see the following response:

      >> ls
      hello.c  hello.cc  hello.f  hello.f90  hello.m  hello_run.m
      
If you see this, you're in the right place! Files ending with "m" are MATLAB files that we can run.

Run the "hello" program interactively:

  1. Type "hello" in the command window; if your path is set up properly, the program will run, printing "Hello, world!";
  2. To see what's in the program, type "type hello.m";

What you just did involved running MATLAB interactively. On the cluster, we will need to run MATLAB programs "indirectly". This requires preparing a short script file that contains the commands you would normally type interactively. We may suppose this file is called "hello_run.m". For our "hello" program, this script file will be pretty simple.

Run the "hello" program indirectly:

  1. Type "hello_run" in the command window; This will print a message, run the "hello" program, and print the time it took to run.
  2. To see what's in the program, type "type hello_run.m";


Exercise 2: Run PRIME_NUMBER on the Desktop

If you are still running MATLAB, then it's time to move to the next example, called "prime_number", and we do that by using a cd command that goes up one level, and then down to the "md" directory:

        >> cd ../prime_number
      
If you issue the "ls" command now, you should see a number of files, but in particular, the files "prime_number.m" and "prime_number_run.m"

The "prime_number" function counts the number of primes up to N. N is a number you choose. The function doesn't print the answer; it returns it as the "value" of the function.

There are 4 primes up to 10, namely 2, 3, 5, and 7. Check this by typing

        >> prime_number ( 10 )
      
This should run very fast. It also doesn't take long to compute the number of primes up to 100, or up to 1000.

We want to study how the time increases with problem size. The prime_number_run script runs the "prime_number" program for the values from 10 to 100,000, multiplying by 10 each time. It prints out the number of primes, and the time it took. But we will see that when the problem size increase by 10, the time tends to go up much more. Issue the command

        >> prime_number_run
      
The pattern in time increase may not be obvious, but it should be clear that the time is going up very fast!


Exercise 3: Run MD on the Desktop with PROFILE

If you are still running MATLAB, then it's time to move to the next example, called "md", and we do that by using a cd command that goes up one level, and then down to the "md" directory:

        >> cd ../md
      
If you issue the "ls" command now, you should see a number of files, but in particular, the file "md.m".

The "md" program carries out a molecular dynamics simulation, using NP particles, where NP is a number you input.

Let's run the "md" program with 100 particles:

        >> md ( 100 )
      
Now let's try to time the program. MATLAB uses the "tic" and "toc" functions to do this. One way to time the program is as follows:
        >> tic
        >> md ( 100 )
        >> toc
      
Unfortunately, if you wait 5 seconds before typing "toc", those 5 seconds get added to the time measurement!

We want to study how the time increases with problem size. The md_run program runs the "md" program, and does the timing for us automatically. So issue the following commands, and record the reported times:

        >> np = 100
        >> md_run
        >> np = 200
        >> md_run
        >> np = 400
        >> md_run
        >> np = 800
        >> md_run
      
As we double the problem size, what is happening to the time?

"md" is a good candidate for parallel computation. In order to prepare the program for parallel computation, we want to know where it is spending its time. "md" is broked up into 5 separate pieces, or "functions". MATLAB has a built in way of telling us how much of our execution time is spent in each function. This will help us focus our parallelization efforts.

Run "md" with the profiler:

        >> profile on
        >> md ( 400 )
        >> profile viewer
      
By examining the profile viewer report, you should be able to name the function which consumes most of the execution time.


Exercise 4: Run PRIME_MATLAB_PARALLEL in parallel on the Desktop

Move to the directory, called "prime_matlab_parallel":

        >> cd ../prime_matlab_parallel
      
If you issue the "ls" command now, you should see the file "prime_matlab_parallel.m".

We're going to try to run this program in parallel on the destkop. Don't expect much improvement, since the desktop machines only have 2 cores.

First, let's make sure the desktop machines actually have the Parallel Computing Toolbox. The command

        >> ver
      
should list all the installed toolboxes. If the Parallel Computing Toolbox isn't there, we can't do this exercise on the desktop machines!

There is a job script that tries to run the prime number program in parallel on a local machine using 2 workers. Run this by typing:

        >> prime_local_run
      
You might see an improvement in time for the largest problem, compared to the nonparallel run.


Exercise 5: Run HELLO on matlab1

First, we must copy the program from the desktop to sysx1 using sftp:

  1. Start an X-window terminal program on your desktop.
  2. Use the cd command to move to the hello directory.
  3. Transfer two files from this desktop directory to your main directory on System X with the commands:
                sftp USERNAME@sysx1.arc.vt.edu
                put hello.m
                put hello_run.m           
              
  4. Leave the sftp terminal window available for further file transfers!

Second, we log in, first to sysx1, and then to matlab1. These commands are entered through your terminal:

  1. Start a second X-window terminal program on your desktop.
  2. ssh -X USERNAME@sysx1.arc.vt.edu (... "-X" ensures we can do graphics.)
  3. ssh -X matlab1 (... "-X" must be used for both connections.)
  4. ls (... You should see the copies of your "hello" files.)
  5. Run hello interactively:
                matlab
                >> hello   (...here's where you expect "Hello, world!")
  6. Now run hello indirectly:
                >> hello_run
              


Exercise 6: Run PRIME_MATLAB_PARALLEL on matlab1

File transfer:

  1. Move your local sftp directory:
                lcd ../prime_matlab_parallel
              
  2. Copy two files to matlab1:
                put prime_parallel.m
                put prime_remote_run.m
              

Interactive commands:

  1. Now run prime indirectly:
                >> prime_remote_run
              


Exercise 7: Run MD_PARALLEL on matlab1

File transfer:

  1. Move your local sftp directory:
                lcd ../md_matlab_parallel
              
  2. Copy two files to matlab1:
                put md_parallel.m
                put md_remote_run.m
              

Interactive commands:

  1. Now run md indirectly:
                >> md_remote_run
              


You can go back to the FDI 2009 page.


Last revised on 25 May 2009.