Link to this document's Jupyter Notebook

In order to successfully complete this assignment you need to participate both individually and in groups during class. If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class on Monday March 29. If you are attending asynchronously, turn in your assignment using D2L no later than _11:59pm on Monday March 29.


In-Class Assignment: MPI Errors

Image of a rubber stamp of the word 'Fail' intended to motivate error checking

Animation from: Pixabay

Agenda for today's class (70 minutes)

  1. (20 minutes) Pre class Review
  2. (20 minutes) MPI Error Example
  3. (30 minutes) Rumor Mill continued

1. Pre class Review

0308--MPI_Errors_pre-class-assignment


2. MPI Error Example

As a class, lets look at our code from Friday and add Error Checking.

How do we check if error handeling is working?

#include <mpi.h>
#include <stdio.h>
static long num_steps = 100000; double step;

int main(int argc, char** argv)
{ 
    int i, nthreads; double pi, sum;
     step = 1.0/(double) num_steps;
    int rank, size;

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); // Rank (ID) of this process
    MPI_Comm_size(MPI_COMM_WORLD, &size); // Total size of MPI job
    MPI_Status status;


     {
         int i, id,nthrds;
         double x;
         id = rank;
         nthrds = size;
         nthreads = nthrds;
         for (i=id, sum=0.0;i< num_steps; i=i+nthrds) {
             x = (i+0.5)*step;
             sum += 4.0/(1.0+x*x);
         }
     }


    if (rank == 0) {    
        double procsum;
        pi = sum * step;
        for(int proc=1;proc<nthreads;proc++)
        {
            /* recv sums from all other processors */
            MPI_Recv(&procsum,1,MPI_DOUBLE,proc,1,MPI_COMM_WORLD, &status);
            pi += procsum * step;
        }
        printf("Pi = %f\n",pi);
    } else {
        /*Send rank 0 my sum*/
        MPI_Send(&sum,1,MPI_DOUBLE,0,1,MPI_COMM_WORLD);
    }


    MPI_Finalize();
}

3. Rumor Mill continued

For the last few minutes of class we will revisit the rumor mill example and see if we can start making modifications to run the example inside mpi.


Congratulations, we're done!

If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending asynchronously, turn in your assignment using D2L.

Course Resources:

Written by Dr. Dirk Colbry, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.