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 April 5. If you are attending asynchronously, turn in your assignment using D2L no later than 11:59pm on Monday April 5.


In-Class Assignment: MPI Reduce Example

animation showing how the diameter of a circle is related to it's circumference. aka pi

Animation from: Wikipedia

Agenda for today's class (70 minutes)

  1. (20 minutes) Quiz Reivew
  2. (20 minutes) Pre class Review
  3. (30 minutes) Pi Monte Carlo

1. Quiz Review


2. Pre class Review

QUESTION: From a parallel point of view. What are some fundamental differences between the two pi estimation programs we have looked at? How do these differences influence which parallel library we can use?


3. Pi Monte Carlo

As a class lets figure compare our notes from the pre-class and rewrite the following program as an MPI program. NOTE: There are probablly hundreds of MPI solutions on-line for this problem. Lets avoid looking at the andwers and see if we came come up with a resonable solution on our own.

DO THIS: Step 1: Write and test a makefile and submission script.

DO THIS: Step 2: Initialize and finalize MPI.

DO THIS: Step 3: Separate the Master (Rank 0) with the workers.

DO THIS: Step 4: Benchmark the results.

#include <stdlib.h>
#include <stdio.h>
#define sqr(x)  ((x)*(x))
long random(void);

double dboard(int darts)
     {
     double x_coord,       /* x coordinate, between -1 and 1  */
            y_coord,       /* y coordinate, between -1 and 1  */
            pi,            /* pi  */
            r;             /* random number between 0 and 1  */
     int score,            /* number of darts that hit circle */
         n;
     long rd;
     unsigned long cconst; /* used to convert integer random number */
                           /* between 0 and 2^31 to double random number */
                           /* between 0 and 1  */

     cconst = 2 << (31 - 1); 
     cconst = RAND_MAX;
     score = 0;

     /* "throw darts at board" */
     for (n = 1; n <= darts; n++)  {
          /* generate random numbers for x and y coordinates */
          rd = random();
      //printf("Rand - %ld\t",rd);
          r = (double)rd/cconst;
          //printf("%10.8f\n",r);
          x_coord = (2.0 * r) - 1.0;
          r = (double)random()/cconst;
          y_coord = (2.0 * r) - 1.0;

          /* if dart lands in circle, increment score */
          if ((sqr(x_coord) + sqr(y_coord)) <= 1.0)
               score++;
          }

     /* calculate pi */
     pi = 4.0 * (double)score/(double)darts;
     return(pi);
     } 

int main(int argc, char ** argv) {

  /** Inicialize MPI **/

  double pi = dboard(1000000);  
  printf("%0.16f\n",pi);

}

DO THIS: Share idea's and code here so we can work as a group.

Link to google doc


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.