Link to this document's Jupyter Notebook

In order to successfully complete this assignment you must do the required reading, watch the provided videos and complete all instructions. The embedded survey form must be entirely filled out and submitted on or before 11:59pm on Sunday April 4. Students must come to class the next day prepared to discuss the material covered in this assignment.


Pre-Class Assignment: MPI Reduce

Goals for today's pre-class assignment

  1. Monte Carlo Method (Another way for Estimating $\Pi$)
  2. MPI Reduce Options
  3. Review Pi Program Serial Code
  4. Assignment wrap up

1. Monte Carlo Method (Another way for Estimating $\Pi$)

In the next class we are going to implement a different kind of $\Pi$ estimator using the algorithm described in the following video:


2. MPI Reduce Options

Like OpenMP there are some special MPI messages that are specifically designed to help with common types of algorithms. These are:

int MPI_Allreduce(const void *sendbuf, 
                  void *recvbuf, 
                  int count,
                  MPI_Datatype datatype, 
                  MPI_Op op, MPI_Comm comm)

The MPI_Op is a reduce operator that can have one of the following values.

Name Meaning
MPI_MAX maximum
MPI_MIN minimum
MPI_SUM sum
MPI_PROD product
MPI_LAND logical and
MPI_BAND bit-wise and
MPI_LOR logical or
MPI_BOR bit-wise or
MPI_LXOR logical xor
MPI_BXOR bit-wise xor
MPI_MAXLOC max value and location
MPI_MINLOC min value and location

Above table from: https://www.mpi-forum.org/docs/mpi-1.1/mpi-11-html/node78.html

QUESTION: Think about the Pi program described in the video above and show in the algorithm below. Which reduce operator(s) do you think may be helpful in implementing this algorithm in MPI?

Put your answer here.


3. Review Pi Program Serial Code

Review the following serial code implementation of the Pi program described in the above video.

DO THIS: Lets check our instincs. Without doing too much googling, include comments in the folloing code on where you think you would need to make modifications for the code to run in MPI.

For example, You may want to include /** Inicialize MPI here **/, /** Clean up MPI Here, /** Have each process send X to master process **/, /** have master rpocess recicve X from all other processes and combine them to do Y **/

Put as much or as little detail as you want. You are not going to turn this in but I will expect everyone to get this working as a class.

#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);
     }

QUESTION: Please share notes with the instructor. Did you find this activity easy/hard. Where did you get stuck?

Put your answer to the above question here


4. Assignment wrap up

Please fill out the form that appears when you run the code below. You must completely fill this out in order to receive credits for the assignment!

Direct Link to Google Form

If you have trouble with the embedded form, please make sure you log on with your MSU google account at googleapps.msu.edu and then click on the direct link above.

Assignment-Specific QUESTION: Please share notes with the instructor. Did you find this activity easy/hard. Where did you get stuck?

Put your answer to the above question here

QUESTION: Summarize what you did in this assignment.

Put your answer to the above question here

QUESTION: What questions do you have, if any, about any of the topics discussed in this assignment after working through the jupyter notebook?

Put your answer to the above question here

QUESTION: How well do you feel this assignment helped you to achieve a better understanding of the above mentioned topic(s)?

Put your answer to the above question here

QUESTION: What was the most challenging part of this assignment for you?

Put your answer to the above question here

QUESTION: What was the least challenging part of this assignment for you?

Put your answer to the above question here

QUESTION: What kind of additional questions or support, if any, do you feel you need to have a better understanding of the content in this assignment?

Put your answer to the above question here

QUESTION: Do you have any further questions or comments about this material, or anything else that's going on in class?

Put your answer to the above question here

QUESTION: Approximately how long did this pre-class assignment take?

Put your answer to the above question here


Congratulations, we're done!

To get credit for this assignment you must fill out and submit the above survey from on or before the assignment due date.

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.