MPI Scatter/Gather Lab
From Education
Yesterday we learned about MPI_Reduce during the Trapezoid Rule lab. Each one of the processes calculated the area under its section of the curve, and then all of them called MPI_Reduce to perform a global sum. We used the MPI operator MPI_SUM. There are quite a few other operators available, including MPI_MAX, MPI_MIN, and MPI_PROD.
Today we talked about MPI_Scatter and MPI_Gather. Your task is to write a program that will take a file of integers as input and find the largest number. Have the server process read in the file and then scatter the data out to the individual processors, each of which should calculate the maximum of its subset. Then, gather each process’s answer to the server process and use it to find the global maximum value.
The first line of the input file will give the number of lines in the file. For instance:
4 <---- This is the number of input lines 12 <--| 4 |--- These are the four input lines 8 | 11 <--|
Once you’ve done that, make a copy of your code and implement the same problem using MPI_Reduce instead. You can use the Linux/UNIX command time in order to tell how long your program is running. How do the two times compare? Does the size of the data set affects which is faster?
Here are a few hints...
- There’s a C file started for you called
input.c. Code from this program will do all of your file input/output for you. You can copy this file here: http://wiki.sc-education.org/index.php/MPI_Scatter/Gather_Lab_Files Recommendation: Make a copy of this code and start working on this copy. - There are also three different input files for you to test on:
small.txt,medium.txt, andtherealdeal.txt. You can right-click the links on the above page, copy the destination, and then runwget <link>to download these. - Something to consider: where do you have to declare variables so that all of the processes have a copy? Where do you have to declare variables so that only the head node has a copy?
Some Syntax for Reference
MPI_Scatter (&send_data, send_count, MPI_INT, &receive_data,
receive_count, MPI_INT, source, MPI_COMM_WORLD);
MPI_Gather (&send_data, send_count, MPI_INT, &receive_data,
receive_count, MPI_INT, dest, MPI_COMM_WORLD);