Fall 2021, Siena College

Lab 8: More Collective Communication
Due: 11:59 PM, Wednesday, October 20, 2021

In this lab, you will work with more of MPI's collective communication functionality.

You may work alone or with a partner on this lab.

Learning goals:

1. To gain experience using MPI collective communication functionality.

Getting Set Up

You can find the link to follow to set up your GitHub repository coll2-lab-yourgitname for this Lab in Canvas. One member of the group should follow the link to set up the repository on GitHub, then that person should email the instructor with the other group members' GitHub usernames so they can be granted access. This will allow all members of the group to clone the repository and commit and push changes to the origin on GitHub. At least one group member should make a clone of the repository to begin work.

Scatter and Gather

Question 1: Answer Pacheco Exercise 3.8 on p. 141. You may submit pictures of hand drawings or use a drawing program. (10 points)

A Monte Carlo Method to Compute pi

Not only games make use of random numbers. There is a class of algorithms knows as Monte Carlo methods that use random numbers to help compute some result.

We will write a parallel program that uses a Monte Carlo method to estimate the value of pi.

The algorithm is fairly straightforward. We repeatedly choose (x,y) coordinate pairs, where the x and y values are in the range 0-1 (i.e.the square with corners at (0,0) and (1,1). For each pair, we determine if its distance from (0,0) is less than or equal to 1. If it is, it means that point lies within the first quardant of a unit circle. Otherwise, it lies outside. If we have a truly random sample of points, there should be an equal probability that they have been chosen at any location in our square domain. The space within the circle occupies (pi)/(4) of the square of area 1.

So we can approximate pi by taking the number of random points found to be within the unit circle, dividing that by the total number of points and multiplying it by 4!

A sequential Java program that uses this method to approximate pi is included for your reference in the pi directory of your repository.

Practice Program: Write a C program pi.c in the pi directory of your repository. Your program should be parallelized with MPI, and compute an approximation of pi using the Monte Carlo method described. See below for some additional requirements and suggestions. (25 points)

• Your program should take a single command-line parameter, which is the number of random points to generate on each process. Store this in a long so you can generate large numbers of points to get good approximations. Convert this to a long only on the rank 0 process (with good error checking) and use MPI to broadcast the value to all other processes. If the rank 0 process finds an error condition when parsing the command-line parameter, it should call `MPI_Abort` to terminate the computation.
• Use the drand48 function to generate your random numbers. Each process needs to seed the random number generator with a different value so they all will compute a different pseudorandom sequence. You might make the seed a function of the current time, the rank, and maybe the number of processes.
• No process other than the rank 0 process should produce output.
• After each process has generated its random points and counted the number that lie within the unit circle, gather all of those counts back to the rank 0 process so it can print out information and compute the approximation of pi.

Here is a sample run of my program, on 4 processes with 100,000,000 points per process. Your program should output the same information in a similar format. Of course, we are choosing random numbers, so your answers will vary.

```Will use 100000000 points per process
[0] 78540219 in circle, pi approx = 3.141609
[1] 78538052 in circle, pi approx = 3.141522
[2] 78541818 in circle, pi approx = 3.141673
[3] 78543977 in circle, pi approx = 3.141759
in circle values range from 78538052 to 78543977
Final approximation of pi: 3.141641
```

Question 2: Run your program for 1 billion points per process on 32, 128, and 512 processes on Stampede2. Rename your output files to stampede32.output, stampede128.output, and stampede512.output, and include them in your repository. (6 points)

Prefix Computations

Complete Pacheco Exercise 3.11 on p. 142. You need not write code for parts a, b, and c. The program you are asked to write in part d will be graded as a practice program. It should be in the sum directory of your repository and be named prefix_sum.c. (Points breakdown: a. 2 points, b. 4 points, c. 8 points, d. 10 points)

Submission

Commit and push!