Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'v3_8_x'
[simgrid.git] / teshsuite / smpi / mpich-test / coll / coll12.c
1
2 #include <stdio.h>
3 #include "mpi.h"
4 #include "test.h"
5
6 #define TABLE_SIZE 2
7
8 int main( int argc, char **argv )
9 {
10   int    rank, size;
11   double a[TABLE_SIZE];
12   struct { double a; int b; } in[TABLE_SIZE], out[TABLE_SIZE];
13   int    i;
14   int    errors = 0, toterrors;
15
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 );
20
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;
24
25   /* Copy data to the "in" buffer */
26   for (i=0; i<TABLE_SIZE; i++) { 
27         in[i].a = a[i];
28         in[i].b = rank;
29   }
30
31   /* Reduce it! */
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 );
34
35   /* Check to see that we got the right answers */
36   for (i=0; i<TABLE_SIZE; i++) 
37         if (i % size == rank)
38           if (out[i].b != rank) {
39         printf("MAX (ranks[%d] = %d != %d\n", i, out[i].b, rank );
40                 errors++;
41       }
42
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;
46
47   /* Copy data to the "in" buffer */
48   for (i=0; i<TABLE_SIZE; i++)  {
49         in[i].a = a[i];
50         in[i].b = rank;
51   }
52
53   /* Reduce it! */
54   MPI_Allreduce( in, out, TABLE_SIZE, MPI_DOUBLE_INT, MPI_MINLOC, MPI_COMM_WORLD );
55
56   /* Check to see that we got the right answers */
57   for (i=0; i<TABLE_SIZE; i++) 
58         if (i % size == rank)
59           if (out[i].b != rank) {
60         printf("MIN (ranks[%d] = %d != %d\n", i, out[i].b, rank );
61                 errors++;
62       }
63
64   /* Finish up! */
65   MPI_Allreduce( &errors, &toterrors, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
66   if (toterrors) {
67       if (errors)
68           printf( "[%d] done with ERRORS(%d)!\n", rank, errors );
69   }
70   else {
71       if (rank == 0) printf( " No Errors\n" );
72   }
73       
74   MPI_Finalize();
75   return errors;
76 }