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.


ICA 32: MPI Reduce Example#

Animation from: [Wikipedia](https://en.wikipedia.org/wiki/File:Pi-unrolled-720.gif)

Agenda for today’s class (70 minutes)#

  1. (10 minutes) Pre-class Review

  2. (60 minutes) Pi Monte Carlo


1. Pre-class Review#

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


2. Pi Monte Carlo#

Compare your notes/solutions 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 answers 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) {
 
  /** Initialize MPI **/
    
  double pi = dboard(1000000);  
  printf("%0.16f\n",pi);
    
}

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.