Examples
The mpi examples folder includes the source codes used in discussions and a pbs file queue.pbs . To run an mpi program, for example, the scatter.c, follow the steps:
- login hpc-login3.usc.edu
- source /usr/usc/openmpi/default/setup.sh
- Go to your working directory which has job.sl and scatter.c.
- mpicc -o go scatter.c
- sbatch job.sl
- After you get the email saying your job is completed, check mpijob.out for output and mpijob.err for any possible error.
For more information, you can visit https://hpcc.usc.edu/support/documentation/examples-of-mpi-programs/
1 Pass Message in a Ring
Write an MPI program that passes a value around a ring of 4 processes using the following steps.
- Process 0 initializes Msg = 451 and prints value of Msg
- Process 0 sends the value of Msg to Process 1
- Process 1 receives the value of Msg, increases it by 1, prints the value and sends the current value of Msg to Process 2
- Process 2 receives the value of Msg, increases it by 1, prints the value and sends the current value of Msg to Process 3
- Process 3 receives the value of Msg, increases it by 1, prints the value and sends the current value of Msg to Process 0
- Process 0 receives the value of Msg from Process 3 and prints the value
Name this program as p1.c. Figure 1 illustrates the steps. The output messages look like:
- Process 0: Initially Msg = 451
- Process 1: Msg = 452
- Process 2: Msg = 453
- Process 3: Msg = 454
- Process 0: Received Msg = 454. Done!
Figure 1: Example diagram
2 Add 64 numbers using 4 processes
In the number.txt file, you can find 64 numbers. Your task is to write an MPI program with 4 processes to compute the sum of these 64 numbers. There are 3 approaches:
- Approach 1, name this program as p2 1.c:
- Each process reads the entire array.
- Do in parallel: Process 0 computes]; Process 1 computes]; Process 2 computes]; Process 3 computes
- Process 1,2,3 send their partial sum to Process 0.
- Process 0 computes the sum of all the partial sums and prints it out.
- Approach 2, name this program as p2 2.c:
- Process 0 reads the array
- Process 0 broadcasts the entire array to every process
- Do in parallel: Process 0 computes]; Process 1 computes]; Process 2 computes]; Process 3 computes
- Process 0 uses MPI SUM reduction to sum these partial sum
- Process 0 prints out the result.
- Approach 3, name this program as p2 3.c:
- Process 0 reads the array and scatters the entire array to every process using the scatter
- Each process sums up the portion of the array it receives.
- Process 0 uses the gather operation to gather these partial sums, computes the sum of all the partial sums and prints it out.

![[Solved] EE451 Homework4- Pass Message in a Ring](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip.jpg)

![[Solved] EE451 Homework6- K-means Clustering](https://assignmentchef.com/wp-content/uploads/2022/08/downloadzip-1200x1200.jpg)
Reviews
There are no reviews yet.