+/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_Gather 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 69// MPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype senddatatype, 70// void * recvbuf, int recvcount, MPI_Datatype recvdatatype, int source, MPI_Comm comm) 71MPI_Scatter (rand_nums ,num_elements_per_proc ,MPI_FLOAT ,sub_rand_nums , 72num_elements_per_proc ,MPI_FLOAT ,0 ,MPI_COMM_WORLD ); 73 74// Compute the average of your subset 75float sub_avg = compute_avg (sub_rand_nums ,num_elements_per_proc ); 76 77// Gather all partial averages down to the root process 78float * sub_avgs = NULL ; 79if (world_rank == 0 ) { 80sub_avgs = (float * )malloc (sizeof (float )* world_size ); 81assert (sub_avgs != NULL ); 82 } 83// MPI_Gather(void *sendbuf, int sendcount, MPI_Datatype senddatatype, 84// void * recvbuf, int recvcount, MPI_Datatype recvdatatype, int target, MPI_Comm comm) 85MPI_Gather (& sub_avg ,1 ,MPI_FLOAT ,sub_avgs ,1 ,MPI_FLOAT ,0 ,MPI_COMM_WORLD ); 86 87// Now that we have all of the partial averages on the root, compute the 88// total average of all numbers. Since we are assuming each process computed 89// an average across an equal amount of elements, this computation will 90// produce the correct answer. 91if (world_rank == 0 ) { 92float avg = compute_avg (sub_avgs ,world_size ); 93printf ("Avg of all elements is %f\n" ,avg ); 94// Compute the average across the original data for comparison 95float original_data_avg = 96compute_avg (rand_nums ,num_elements_per_proc * world_size ); 97printf ("Avg computed across original data is %f\n" ,original_data_avg ); 98 } 99 100// Clean up 101if (world_rank == 0 ) { 102free (rand_nums ); 103free (sub_avgs ); 104 } 105free (sub_rand_nums ); 106 107MPI_Barrier (MPI_COMM_WORLD ); 108MPI_Finalize (); 109}