Skip to content

Instantly share code, notes, and snippets.

@rainiera
Created February 8, 2016 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rainiera/fee472344bd3507946bd to your computer and use it in GitHub Desktop.
Save rainiera/fee472344bd3507946bd to your computer and use it in GitHub Desktop.
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
/*
* Each process computes random number
* Root process finds and prints the maximum generated value
* Have each process print its value
*
* Let each process scale its value by this maximum
*/
int main(int argc, char*argv[]) {
MPI_Init(&argc, &argv);
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
int root = 0;
double local_max_send;
double global_max_recv;
MPI_Reduce(&local_max_send, &global_max_recv, 1,
MPI_DOUBLE, MPI_MAX, root, MPI_COMM_WORLD);
srand((int) (rank * (double) RAND_MAX / size));
double randomfraction = (rand() / (double) RAND_MAX);
printf("Process %d generated number %lf\n", rank, randomfraction);
if (rank == root) {
MPI_Reduce(&local_max_send, &global_max_recv, 1,
MPI_DOUBLE, MPI_MAX, root, MPI_COMM_WORLD);
printf("Found the max! %lf\n", global_max_recv);
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment