The goal of the previous lab was to set up Visual Studio to compile a program with
MPI calls. 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,
then compile it.
Prerequisites:
- Read MPI Basics.
- The Visual Studio .sln file created in the previous lab segment. If you do
not have this, use either the karpCserialSetup or karpFserialSetup
directories. Rename one of these directories to karp. Alternatively,
if you plan to use command line, you will need an open command line window and your
source code.
Instructions:
- Open the Visual Studio .sln file created in the previous section. If working
in command line, edit your source code.
- 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?
- Add MPI calls to create a SPMD karp propram, splitting up the work among parallel
processes. Edit karp to divide up work among the processes. Use only the six basic
MPI calls.
Hint: 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.
- The C solution needs the include statement (#include "mpi.h") and
the Fortran solution requires the statement use mpi. If you are
working in Fortran, you need to add the file
C:\Program File\MPIPro\include\mpif.f90 to your project's
Source Files. It is required to create the module MPI.mod that
is invoked by use mpi.
If you need help with this section, you can view the solution, karp.parallel.c
or karp.parallel.f . If you plan to use one of the solution
files, remember to rename it karp.c or karp.f.
Results:
When you are done with this section, you should have a parallel program ready to
compile.