MPI Example Programs: ex1
Simple Example
Table of Contents
This is a SMPD code which simulates master-worker with if-endif
sections for each. The "master" breaks up an array for each of the
"workers" to do an operation on the portion they are given.
This program does a simple value assignment to each of the elements in
an array, then selected elements are displayed.
In this simple example, the master task initiates
numtasks-1 worker tasks. It then distributes an equal
portion of an array to each worker task. Each worker task receives
its portion of the array, and performs a simple value assignment to
each of its elements. The value assigned to each element is simply
that element's index in the array+1. Each worker task then sends its
portion of the array back to the master task. As the master receives
each returned portion of the array, selected elements are displayed.
Note: For this example, use spattach (or in batch, the MP_PROCS environment variable)
to get an odd number of processes in order to insure even distribution of the array
to numtasks-1 worker tasks.
- First, copy the source files to your current working directory.
You may copy the files from within your browser, or use the
cp command. Some browsers add or replace characters when files are copied.
Tutorial directory: /afs/pdc.kth.se/public/www/training/Tutor/MPI/Templates/ex1/
- Compile using the mpxlf or mpcc command.
For Fortran:
mpxlf -o mpi.ex1 mpi.ex1.f
or, for C:
mpcc -o mpi.ex1 mpi.ex1.c
- Specify how many nodes to run on:
setenv MP_PROCS 3 or use -procs 3
- Make sure that at least one of the following is true:
- There is no file named host.list in your current working directory
- The environment variable MP_RESD is NOT set to "yes"
- Execute the master program several times and notice the output. You should notice that the workers do not always finish "in order".
Some or all of the following files will be left in your directory and should be removed to save space:
mpi.ex1.f or mpi.ex1.c
mpi.ex1
URL http://www.pdc.kth.se/training/Tutor/MPI/Templates/ex1/