/*
** A reduce example using maxloc
** Copyright Rolf Riesen 2008
**
*/
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>


typedef struct maxfind_t {
    int value;
    int rank;
} maxfind_t;



int
main(int argc, char *argv[])
{

int my_rank, nproc;
maxfind_t my_value;
maxfind_t result;
int root;
int count;



    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    /* Initialize the random number generator with a */
    /* different seed on each node. */
    srandom(my_rank);

    my_value.value= random();
    my_value.rank= my_rank;
    root= 0;
    count= 1;

    MPI_Reduce(&my_value, &result, count, MPI_2INT, MPI_MAXLOC, root,
            MPI_COMM_WORLD);

    printf("Node %3d sent value %d\n", my_rank, my_value.value);

    if (my_rank == root)   {
	printf("Value %d from rank %d is the maximum\n",
                result.value, result.rank);
    }

    MPI_Finalize();
    return 0;

}  /* end of main() */
