MPI Example Programs: ex1

Simple Example


Table of Contents


Quick Summary

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.


Description of Problem

This program does a simple value assignment to each of the elements in an array, then selected elements are displayed.


Parallel Implementation

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.


Instructions for Compiling and Running

  1. 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/

  2. Compile using the mpxlf or mpcc command.

    For Fortran:


    or, for C:

  3. Specify how many nodes to run on:

  4. Make sure that at least one of the following is true:

  5. Execute the master program several times and notice the output. You should notice that the workers do not always finish "in order".


Cleanup

Some or all of the following files will be left in your directory and should be removed to save space:


URL http://www.pdc.kth.se/training/Tutor/MPI/Templates/ex1/