8 int main( int argc, char **argv )
12 struct { double a; int b; } in[TABLE_SIZE], out[TABLE_SIZE];
14 int errors = 0, toterrors;
16 /* Initialize the environment and some variables */
17 MPI_Init( &argc, &argv );
18 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
19 MPI_Comm_size( MPI_COMM_WORLD, &size );
21 /* Initialize the maxloc data */
22 for ( i=0; i<TABLE_SIZE; i++ ) a[i] = 0;
23 for ( i=rank; i<TABLE_SIZE; i++ ) a[i] = (double)rank + 1.0;
25 /* Copy data to the "in" buffer */
26 for (i=0; i<TABLE_SIZE; i++) {
32 MPI_Reduce( in, out, TABLE_SIZE, MPI_DOUBLE_INT, MPI_MAXLOC, 0, MPI_COMM_WORLD );
33 MPI_Bcast ( out, TABLE_SIZE, MPI_DOUBLE_INT, 0, MPI_COMM_WORLD );
35 /* Check to see that we got the right answers */
36 for (i=0; i<TABLE_SIZE; i++)
38 if (out[i].b != rank) {
39 printf("MAX (ranks[%d] = %d != %d\n", i, out[i].b, rank );
43 /* Initialize the minloc data */
44 for ( i=0; i<TABLE_SIZE; i++ ) a[i] = 0;
45 for ( i=rank; i<TABLE_SIZE; i++ ) a[i] = -(double)rank - 1.0;
47 /* Copy data to the "in" buffer */
48 for (i=0; i<TABLE_SIZE; i++) {
54 MPI_Allreduce( in, out, TABLE_SIZE, MPI_DOUBLE_INT, MPI_MINLOC, MPI_COMM_WORLD );
56 /* Check to see that we got the right answers */
57 for (i=0; i<TABLE_SIZE; i++)
59 if (out[i].b != rank) {
60 printf("MIN (ranks[%d] = %d != %d\n", i, out[i].b, rank );
65 MPI_Allreduce( &errors, &toterrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
68 printf( "[%d] done with ERRORS(%d)!\n", rank, errors );
71 if (rank == 0) printf( " No Errors\n" );