Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
support MPI_Op_commutative call, as it was already implemented internally
[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 {
18         double a;
19         int b;
20     } in[TABLE_SIZE], out[TABLE_SIZE];
21     int i;
22     int errors = 0;
23
24     /* Initialize the environment and some variables */
25     MTest_Init(&argc, &argv);
26     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
27     MPI_Comm_size(MPI_COMM_WORLD, &size);
28
29     /* Initialize the maxloc data */
30     for (i = 0; i < TABLE_SIZE; i++)
31         a[i] = 0;
32     for (i = rank; i < TABLE_SIZE; i++)
33         a[i] = (double) rank + 1.0;
34
35     /* Copy data to the "in" buffer */
36     for (i = 0; i < TABLE_SIZE; i++) {
37         in[i].a = a[i];
38         in[i].b = rank;
39     }
40
41     /* Reduce it! */
42     MPI_Reduce(in, out, TABLE_SIZE, MPI_DOUBLE_INT, MPI_MAXLOC, 0, MPI_COMM_WORLD);
43     MPI_Bcast(out, TABLE_SIZE, MPI_DOUBLE_INT, 0, MPI_COMM_WORLD);
44
45     /* Check to see that we got the right answers */
46     for (i = 0; i < TABLE_SIZE; i++)
47         if (i % size == rank)
48             if (out[i].b != rank) {
49                 printf("MAX (ranks[%d] = %d != %d\n", i, out[i].b, rank);
50                 errors++;
51             }
52
53     /* Initialize the minloc data */
54     for (i = 0; i < TABLE_SIZE; i++)
55         a[i] = 0;
56     for (i = rank; i < TABLE_SIZE; i++)
57         a[i] = -(double) rank - 1.0;
58
59     /* Copy data to the "in" buffer */
60     for (i = 0; i < TABLE_SIZE; i++) {
61         in[i].a = a[i];
62         in[i].b = rank;
63     }
64
65     /* Reduce it! */
66     MPI_Allreduce(in, out, TABLE_SIZE, MPI_DOUBLE_INT, MPI_MINLOC, MPI_COMM_WORLD);
67
68     /* Check to see that we got the right answers */
69     for (i = 0; i < TABLE_SIZE; i++)
70         if (i % size == rank)
71             if (out[i].b != rank) {
72                 printf("MIN (ranks[%d] = %d != %d\n", i, out[i].b, rank);
73                 errors++;
74             }
75
76     /* Finish up! */
77     MTest_Finalize(errors);
78     MPI_Finalize();
79     return MTestReturnValue(errors);
80 }