Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add mpich3 test suite, to replace older one.
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / coll12.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 #include <stdio.h>
8 #include "mpi.h"
9 #include "mpitest.h"
10
11 #define TABLE_SIZE 2
12
13 int main( int argc, char **argv )
14 {
15   int    rank, size;
16   double a[TABLE_SIZE];
17   struct { double a; int b; } in[TABLE_SIZE], out[TABLE_SIZE];
18   int    i;
19   int    errors = 0;
20
21   /* Initialize the environment and some variables */
22   MTest_Init( &argc, &argv );
23   MPI_Comm_rank( MPI_COMM_WORLD, &rank );
24   MPI_Comm_size( MPI_COMM_WORLD, &size );
25
26   /* Initialize the maxloc data */
27   for ( i=0; i<TABLE_SIZE; i++ ) a[i] = 0;
28   for ( i=rank; i<TABLE_SIZE; i++ ) a[i] = (double)rank + 1.0;
29
30   /* Copy data to the "in" buffer */
31   for (i=0; i<TABLE_SIZE; i++) { 
32         in[i].a = a[i];
33         in[i].b = rank;
34   }
35
36   /* Reduce it! */
37   MPI_Reduce( in, out, TABLE_SIZE, MPI_DOUBLE_INT, MPI_MAXLOC, 0, MPI_COMM_WORLD );
38   MPI_Bcast ( out, TABLE_SIZE, MPI_DOUBLE_INT, 0, MPI_COMM_WORLD );
39
40   /* Check to see that we got the right answers */
41   for (i=0; i<TABLE_SIZE; i++) 
42         if (i % size == rank)
43           if (out[i].b != rank) {
44         printf("MAX (ranks[%d] = %d != %d\n", i, out[i].b, rank );
45                 errors++;
46       }
47
48   /* Initialize the minloc data */
49   for ( i=0; i<TABLE_SIZE; i++ ) a[i] = 0;
50   for ( i=rank; i<TABLE_SIZE; i++ ) a[i] = -(double)rank - 1.0;
51
52   /* Copy data to the "in" buffer */
53   for (i=0; i<TABLE_SIZE; i++)  {
54         in[i].a = a[i];
55         in[i].b = rank;
56   }
57
58   /* Reduce it! */
59   MPI_Allreduce( in, out, TABLE_SIZE, MPI_DOUBLE_INT, MPI_MINLOC, MPI_COMM_WORLD );
60
61   /* Check to see that we got the right answers */
62   for (i=0; i<TABLE_SIZE; i++) 
63         if (i % size == rank)
64           if (out[i].b != rank) {
65         printf("MIN (ranks[%d] = %d != %d\n", i, out[i].b, rank );
66                 errors++;
67       }
68
69   /* Finish up! */
70   MTest_Finalize( errors );
71   MPI_Finalize();
72   return MTestReturnValue( errors );
73 }