+/EE360P

distributed systems

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

vijaygarg1initbc9a89f

master
3.0 KiB96 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_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) {
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(rand_nums, num_elements_per_proc, MPI_FLOAT, sub_rand_nums,
70              num_elements_per_proc, MPI_FLOAT, 0, MPI_COMM_WORLD);
71
72  // Compute the average of your subset
73  float sub_avg = compute_avg(sub_rand_nums, num_elements_per_proc);
74
75  // Gather all partial averages down to all the processes
76  float *sub_avgs = (float *)malloc(sizeof(float) * world_size);
77  assert(sub_avgs != NULL);
78  MPI_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.
84  float avg = compute_avg(sub_avgs, world_size);
85  printf("Avg of all elements from proc %d is %f\n", world_rank, avg);
86
87  // Clean up
88  if (world_rank == 0) {
89    free(rand_nums);
90  }
91  free(sub_avgs);
92  free(sub_rand_nums);
93
94  MPI_Barrier(MPI_COMM_WORLD);
95  MPI_Finalize();
96}