Skip to main content

more options


Add MPI

Please use the "Switch to Details" button to return to the lab instructions.

In this section we will parallelize karp.c or karp.f by adding MPI calls.  Starting with a serial version of karp, create a parallel version in SPMD (Single Program, Multiple Data) form. The program will be compiled in the next section.

Prerequisites:

  1. Read MPI Basics.
  2. Make sure you have copied the source code karp.c or karp.f as instructed in the previous section.  The files should be in your directory called $HOME/lab or ~/lab.

Instructions:

  1. You have already compiled and run the serial program. In preparation to parallelize it, think about these questions:
    • How does it calculate PI? (Hint: look at the program comments)
    • How does the precision of the calculation depend on N, the number of approximation steps? (Hint: edit values to have various input values from 10 to 10000)
    • What do you think will happen to the precision with which we calculate PI when we split up the work among the nodes?
  2. Edit karp.c or karp.f. 

    Add MPI calls to create a SPMD karp propram, splitting up the work among parallel processes. Modify karp to divide up work among the processes. Use only the six basic MPI calls.

    Strategy: the master needs to let all the workers know the total number of iterations, and then each worker calculates its loop indices so it does its share of the work. When done, each worker sends its partial sum back to the master, which receives them and calculates the final sum.

    Hint: The code is commented to indicate where you need to add or modify code.

    Fortran: The fortran code requires the statement
    include "mpif.h".

    Solution: If you need help with this section, you can view the solution, karp.parallel.c or karp.parallel.f .

Results:

When you are done with this section, you should have a parallel program source file.