Week 8 - Practical Exercise

Aims

Tasks

Exercise 1

Firstly copy the contents of the StarterCode/ directory for this tutorial to the directory where you are doing the tutorial:

bourbaki.une.edu.au.Solutions> cp -r /homes/unit/comp309/public_html/Tutorials/Week_08/StarterCode StarterCode

This will give you the following files:

turing.une.edu.au.StarterCode> ls -l
total 168
-rw-r--r--  1 comp309 admin    424 Sep 11  2002 hello.c
-rw-r--r--  1 comp309 admin    320 Aug 29  2002 hello_other.c
-rw-r--r--  1 comp309 admin    364 Oct 29  2004 makefile
-rw-r--r--  1 comp309 admin 144706 Oct 29  2004 memerror
-rw-r--r--  1 comp309 admin   1279 Oct 29  2004 memerror.c
-rw-r--r--  1 comp309 admin    597 Oct 29  2004 pvm_suppressions

Now log onto b1, and compile the files: b1.une.edu.au.Solutions> make gcc -Wall hello.c -lpvm3 -o hello gcc -Wall hello_other.c -lpvm3 -o hello_other

Now copy the executable files to your ~/pvm3/bin/LINUXX86_64/ directory:

b1.une.edu.au.Solutions> make host
cp -f hello /homes/courses/comp309/pvm3/bin/LINUXX86_64/  
cp -f hello_other /homes/courses/comp309/pvm3/bin/LINUXX86_64/

Change to the pvm3/bin/LINUXX86_64/ directory and run hello:

b1.une.edu.au.LINUX> cd ~/pvm3/bin/LINUXX86_64/
b1.LINUX> ./hello
from t40003: hello, world from bourbaki

Don’t forget to start the PVM daemon up if it isn’t already running.

Exercise 2

Now then, if you take a look at the code in hello.c, you will see a pvm_spawn call. In the function arguments, there is “hello_other”, which is the job being spawned (executable). To understand the function arguments, take a look at the man page:

b1.LINUX> man pvm_spawn
``

What you are to do now is merge hello and hello_other, so copy hello.c to hello_combined.c. The thing to keep in mind is that hello was the master, and hello_other was the slave. You will need to use a call to pvm_parent in your code:

ptid = pvm_parent(); ```

Take a look at the pvm_parent man page if necessary. Now we check the value of ptid to see whether we are master or slave:

if(ptid == PvmNoParent){
    //master code goes here 
}
else{
    //slave code goes here
}

The call to pvm_spawn should now spawn the program itself - hello_combined. You will need to modify the makefile to get your program to compile. Make sure your program compiles and runs (make sure the executables are in the right place - make host), output should still be exactly the same as hello:

b1.LINUX> ./hello_combined
from t40003: hello, world from b1

Exercise 3

Now copy hello_combined.c to hello_combined_swarm.c. Make the following changes to your program:

Your program, when executed with a number should now look like:

b1.une.edu.au.LINUX> ./hello_combined_swarm 2
from t40007: hello, world from b1.une.edu.au
from t40008: hello, world from b1.une.edu.au
b1.une.edu.au.LINUX> ./hello_combined_swarm 5
from t4000a: hello, world from b1.une.edu.au
from t4000b: hello, world from b1.une.edu.au
from t4000c: hello, world from b1.une.edu.au
from t4000d: hello, world from b1.une.edu.au
from t4000e: hello, world from b1.une.edu.au

Exercise 4

OK, copy your working hello_combined_swarm.c to hello_combined_hosts.c. In this version, you want to be able to spawn tasks on specific hosts. For example, if you give the program an argument of 4, jobs will spawned on b1, b2, b3, and b4. To do this, you will need play with the pvm_spawn call. In a loop, you will need to generate the host name to spawn on:

char host[BUFF];    
sprintf(host, "b%d",i+1);

In your pvm_spawn call you will then spawn one task only, on the host you just made a name for. You will need to make use of PvmTaskHost in the flag argument to pvm_spawn, ortherwise your host name will be ignored and the daemon will decide which machine it will spawn on. Now you will need to add some hosts to the PVM daemon. You can do this manually using add in the PVM console, or start PVM with a file containing the names of hosts to add:

b1.LINUX> pvm hostfile
The hostfile contains the hosts, one per line:
b1
b2
.
.
.
b8

Now run hello_combined_hosts and compare the results with hello_combined_swarm, when you run it with more hosts available. My output for hello_combined_hosts looks like:

b1.Solutions> ./hello_combined_swarm 2
from t300001: hello, world from b2
from t2c0001: hello, world from b1