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



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

int my_rank, nproc;
int my_value;
int result;
int root;
int count;
int calc;
int i;



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

    /* Each node contributes its own node number */
    my_value= my_rank;
    result= 0;
    root= 0;
    count= 1;

    MPI_Reduce(&my_value, &result, count, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD);

    printf("Node %3d: My result is %d\n", my_rank, result);
    if (my_rank == root)   {
	calc= 0;
	for (i= 0; i < nproc; i++)   {
	    calc= calc + i;
	}
	printf("Expected result is %d.\n", calc);
    }

    MPI_Finalize();
    return 0;

}  /* end of main() */
