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.
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();
}
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.
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.
Written by Dr. Dirk Colbry, Michigan State University
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.