In order to successfully complete this assignment you need to participate both individually and in groups during class. Have one of the instructors check your notebook and sign you out before leaving class. Turn in your assignment using D2L no later than 11:59pm.


In-Class Assignment: MPI Errors#

Animation from: [Pixabay](https://pixabay.com/)

Agenda for today’s class (70 minutes)#

  1. (10 minutes) Pre-class Review

  2. (30 minutes) MPI Error Example

  3. (20 minutes) Rumor Mill

  4. (10 minutes) Quiz Prep


1. Pre-class Review#

DO THIS: Discuss the references you found for error handling in MPI with your group. Below, summarize your findings for handling errors in MPI.


2. MPI Error Example#

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

How do we check if error handling 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 Example Continued#

Use the rest of the class to continue working on the rumor example. If you get a solution working, discuss your solution with your group.


4. Use the remainder of class to review for the MPI quiz.#

Next class we will have an MPI quiz to check understanding of the MPI content we have covered so far. Be sure to review the general MPI workflow and ask questions if you are feeling confused about any of the MPI content so far.


Congratulations, we’re done!#

Have one of the instructors check your notebook and sign you out before leaving class. Turn in your assignment using D2L.

Written by Dr. Dirk Colbry, Michigan State University (Updated by Dr. Nathan Haut in Spring 2025) Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.