+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

vijaygarg1initbc9a89f

master
3.6 KiB109 linesraw
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) {
18  float *rand_nums = (float *)malloc(sizeof(float) * num_elements);
19  assert(rand_nums != NULL);
20  int i;
21  for (i = 0; i < num_elements; i++) {
22    rand_nums[i] = (rand() / (float)RAND_MAX);
23  }
24  return rand_nums;
25}
26
27// Computes the average of an array of numbers
28float compute_avg(float *array, int num_elements) {
29  float sum = 0.f;
30  int i;
31  for (i = 0; i < num_elements; i++) {
32    sum += array[i];
33  }
34  return sum / num_elements;
35}
36
37int main(int argc, char** argv) {
38  if (argc != 2) {
39    fprintf(stderr, "Usage: avg num_elements_per_proc\n");
40    exit(1);
41  }
42
43  int num_elements_per_proc = atoi(argv[1]);
44  // Seed the random number generator to get different results each time
45  srand(time(NULL));
46
47  MPI_Init(NULL, NULL);
48
49  int world_rank;
50  MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
51  int world_size;
52  MPI_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
57  float *rand_nums = NULL;
58  if (world_rank == 0) {
59    rand_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
64  float *sub_rand_nums = (float *)malloc(sizeof(float) * num_elements_per_proc);
65  assert(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)
71  MPI_Scatter(rand_nums, num_elements_per_proc, MPI_FLOAT, sub_rand_nums,
72              num_elements_per_proc, MPI_FLOAT, 0, MPI_COMM_WORLD);
73
74  // Compute the average of your subset
75  float sub_avg = compute_avg(sub_rand_nums, num_elements_per_proc);
76
77  // Gather all partial averages down to the root process
78  float *sub_avgs = NULL;
79  if (world_rank == 0) {
80    sub_avgs = (float *)malloc(sizeof(float) * world_size);
81    assert(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)
85  MPI_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.
91  if (world_rank == 0) {
92    float avg = compute_avg(sub_avgs, world_size);
93    printf("Avg of all elements is %f\n", avg);
94    // Compute the average across the original data for comparison
95    float original_data_avg =
96      compute_avg(rand_nums, num_elements_per_proc * world_size);
97    printf("Avg computed across original data is %f\n", original_data_avg);
98  }
99
100  // Clean up
101  if (world_rank == 0) {
102    free(rand_nums);
103    free(sub_avgs);
104  }
105  free(sub_rand_nums);
106
107  MPI_Barrier(MPI_COMM_WORLD);
108  MPI_Finalize();
109}