// 2017.10.10
// This program is designed to illustrate the MPI Scatter command. The idea is to distribute
// data from an array to compute local averages, then return those averages to process 0 who
// will the compute the final average.
// To run this program, you need two command line arguments, the first is the value N
// which will be the upper bound on the range of random numbers to fill the array with
// the second is the amount of numbers to send to each process. For the example below
// there will be 40*100 random numbers between 1 and 40 in an array, with each process getting
// 100 of them to average.
// qsub -q computeonly.q -pe mpirr 50 subfile.qsub MPI_scatter_example 40 100
#include
#include
#include "mpi.h"
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
int n, rank, size, i; // all the interesting variables go here
int N = 0; // largest value for the random numbers which will fill the array
int sum = 0; // to compute averages, you need sums!
float localAvg = 0; // each process will compute its own local average (localAvg)
float avg = 0; // then process zero will compute the final avg (avg)
float finalsum = 0;
N = atoi(argv[1]); // input of largest possible number for guessing game, done command line
int perGroup = atoi(argv[2]); // the number of values each process should get for the scatter command, done command line
int *numSubArray = new int[perGroup] (); // each process creates its own array of length perGroup here
MPI::Status status; // MPI:Status variable status is a class, and can be used for debugging purposes if need be
// start of the MPI communication structure
MPI::Init(argc, argv);
size = MPI::COMM_WORLD.Get_size();
rank = MPI::COMM_WORLD.Get_rank();
float *groupAvg = new float[size] (); // all the averages will be stored in here once computed
int *numArray = new int[size*perGroup] (); // the BIG array which will house all the numbers to be averaged
// rank 0 will now fill the numArray with random numbers between 1 and N
if (rank == 0)
{
cout << "Node " << rank << " of " << size << " : ";
srand(time(NULL));
for (i=0; i