Introduction to MPI
 

Some mechanics:
 

0.  Getting ssh configured

a) First slogin to nash.math.colostate.edu and type

ssh-keygen -t dsa

followed by 3 Returns

b) Next cd to  the the .ssh directory cd .ssh

c) While in  the .ssh directory,  type cp id_dsa.pub authorized_keys
 
 
 

We will be using MPI on the Math Department's "IBM cluster

1.  Check to see if you have an account on

             nash.math.colostate.edu
 
 

2. From nash  go to

/usr/local/mpich/examples
and copy cpi.c to your home directory  ( type  cp cpi.c ~).
 
 

3.  Compile cpi.c by typing

mpicc cpi.c


4.  Run cpi.c on 4 processors by typing

mpirun -np 4 a.out


to get output something like:

nash> mpirun -np 4 a.out
Process 0 on nash.math.colostate.edu
Process 3 on node3
Process 1 on node1
Process 2 on node2
pi is approximately 3.1416009869231245, Error is 0.0000083333333314
wall clock time = 0.008474
 

Introduction to MPI coding:


Hello World
 

#include <stdio.h>
#include "mpi.h"

int main( argc, argv )
int  argc;
char **argv;
{
    int rank, size;
    MPI_Init( &argc, &argv );
    MPI_Comm_size( MPI_COMM_WORLD, &size );
    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    printf( "Hello world from process %d of %d\n", rank, size );
    MPI_Finalize();
    return 0;
}
 
 

Finding Pi:

#include "mpi.h"
#include <math.h>

int main(argc,argv)
int argc;
char *argv[];
{
    int done = 0, n, myid, numprocs, i;
    double PI25DT = 3.141592653589793238462643;
    double mypi, pi, h, sum, x;

    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);
    while (!done)
    {
        if (myid == 0) {
            printf("Enter the number of intervals: (0 quits) ");
            scanf("%d",&n);
        }
        MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
        if (n == 0) break;

        h   = 1.0 / (double) n;
        sum = 0.0;
        for (i = myid + 1; i <= n; i += numprocs) {
            x = h * ((double)i - 0.5);
            sum += 4.0 / (1.0 + x*x);
        }
        mypi = h * sum;

        MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0,
                   MPI_COMM_WORLD);

        if (myid == 0)
            printf("pi is approximately %.16f, Error is %.16f\n",
                   pi, fabs(pi - PI25DT));
    }
    MPI_Finalize();
    return 0;
}
 

Resources:

On the GS510-511 Home Page, go to Internet Resources and then to