+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
bc9a89f
master
1// Author: Wes Kendall 2// Copyright 2012 www.mpitutorial.com 3// This code is provided freely with the tutorials on mpitutorial.com. Feel 4// free to modify it for your own use. Any distribution of the code must 5// either provide a link to www.mpitutorial.com or keep this header intact. 6// 7// Program that computes the average of an array of elements in parallel using 8// MPI_Scatter and MPI_Allgather 9// 10#include <stdio.h> 11#include <stdlib.h> 12#include <time.h> 13#include <mpi.h> 14#include <assert.h> 15 16// Creates an array of random numbers. Each number has a value from 0 - 1 17float * create_rand_nums (int num_elements ) { 18float * rand_nums = (float * )malloc (sizeof (float )* num_elements ); 19assert (rand_nums != NULL ); 20int i ; 21for (i = 0 ;i < num_elements ;i ++ ) { 22rand_nums [i ]= (rand () / (float )RAND_MAX ); 23 } 24return rand_nums ; 25} 26 27// Computes the average of an array of numbers 28float compute_avg (float * array ,int num_elements ) { 29float sum = 0.f ; 30int i ; 31for (i = 0 ;i < num_elements ;i ++ ) { 32sum += array [i ]; 33 } 34return sum /num_elements ; 35} 36 37int main (int argc ,char ** argv ) { 38if (argc != 2 ) { 39fprintf (stderr ,"Usage: avg num_elements_per_proc\n" ); 40exit (1 ); 41 } 42 43int num_elements_per_proc = atoi (argv [1 ]); 44// Seed the random number generator to get different results each time 45srand (time (NULL )); 46 47MPI_Init (NULL ,NULL ); 48 49int world_rank ; 50MPI_Comm_rank (MPI_COMM_WORLD ,& world_rank ); 51int world_size ; 52MPI_Comm_size (MPI_COMM_WORLD ,& world_size ); 53 54// Create a random array of elements on the root process. Its total 55// size will be the number of elements per process times the number 56// of processes 57float * rand_nums = NULL ; 58if (world_rank == 0 ) { 59rand_nums = create_rand_nums (num_elements_per_proc * world_size ); 60 } 61 62// For each process, create a buffer that will hold a subset of the entire 63// array 64float * sub_rand_nums = (float * )malloc (sizeof (float )* num_elements_per_proc ); 65assert (sub_rand_nums != NULL ); 66 67// Scatter the random numbers from the root process to all processes in 68// the MPI world 69MPI_Scatter (rand_nums ,num_elements_per_proc ,MPI_FLOAT ,sub_rand_nums , 70num_elements_per_proc ,MPI_FLOAT ,0 ,MPI_COMM_WORLD ); 71 72// Compute the average of your subset 73float sub_avg = compute_avg (sub_rand_nums ,num_elements_per_proc ); 74 75// Gather all partial averages down to all the processes 76float * sub_avgs = (float * )malloc (sizeof (float )* world_size ); 77assert (sub_avgs != NULL ); 78MPI_Allgather (& sub_avg ,1 ,MPI_FLOAT ,sub_avgs ,1 ,MPI_FLOAT ,MPI_COMM_WORLD ); 79 80// Now that we have all of the partial averages, compute the 81// total average of all numbers. Since we are assuming each process computed 82// an average across an equal amount of elements, this computation will 83// produce the correct answer. 84float avg = compute_avg (sub_avgs ,world_size ); 85printf ("Avg of all elements from proc %d is %f\n" ,world_rank ,avg ); 86 87// Clean up 88if (world_rank == 0 ) { 89free (rand_nums ); 90 } 91free (sub_avgs ); 92free (sub_rand_nums ); 93 94MPI_Barrier (MPI_COMM_WORLD ); 95MPI_Finalize (); 96}