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!)
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.mIf 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:
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:
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_numberIf 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_runThe pattern in time increase may not be obvious, but it should be clear that the time is going up very fast!
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 ../mdIf 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 ) >> tocUnfortunately, 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_runAs 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 viewerBy examining the profile viewer report, you should be able to name the function which consumes most of the execution time.
Move to the directory, called "prime_matlab_parallel":
>> cd ../prime_matlab_parallelIf 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
>> vershould 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_runYou might see an improvement in time for the largest problem, compared to the nonparallel run.
First, we must copy the program from the desktop to sysx1 using sftp:
sftp USERNAME@sysx1.arc.vt.edu put hello.m put hello_run.m
Second, we log in, first to sysx1, and then to matlab1. These commands are entered through your terminal:
matlab >> hello (...here's where you expect "Hello, world!")
put prime_parallel.m put prime_remote_run.m
put md_parallel.m put md_remote_run.m
You can go back to the FDI 2009 page.