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 / icallreduce.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "mpitest.h"
11 #include "mpicolltest.h"
12
13 /*
14 static char MTEST_Descrip[] = "Simple intercomm allreduce test";
15 */
16
17 int main(int argc, char *argv[])
18 {
19     int errs = 0, err;
20     int *sendbuf = 0, *recvbuf = 0;
21     int leftGroup, i, count, rank, rsize;
22     MPI_Comm comm;
23     MPI_Datatype datatype;
24
25     MTest_Init(&argc, &argv);
26
27     datatype = MPI_INT;
28     /* Get an intercommunicator */
29     while (MTestGetIntercomm(&comm, &leftGroup, 4)) {
30         if (comm == MPI_COMM_NULL)
31             continue;
32         MPI_Comm_rank(comm, &rank);
33         MPI_Comm_remote_size(comm, &rsize);
34
35         /* To improve reporting of problems about operations, we
36          * change the error handler to errors return */
37         MPI_Errhandler_set(comm, MPI_ERRORS_RETURN);
38
39         for (count = 1; count < 65000; count = 2 * count) {
40             /* printf("rank = %d(%d)\n", rank, leftGroup); fflush(stdout); */
41             sendbuf = (int *) malloc(count * sizeof(int));
42             recvbuf = (int *) malloc(count * sizeof(int));
43             if (leftGroup) {
44                 for (i = 0; i < count; i++)
45                     sendbuf[i] = i;
46             }
47             else {
48                 for (i = 0; i < count; i++)
49                     sendbuf[i] = -i;
50             }
51             for (i = 0; i < count; i++)
52                 recvbuf[i] = 0;
53             err = MTest_Allreduce(sendbuf, recvbuf, count, datatype, MPI_SUM, comm);
54             if (err) {
55                 errs++;
56                 MTestPrintError(err);
57             }
58             /* In each process should be the sum of the values from the
59              * other process */
60             if (leftGroup) {
61                 for (i = 0; i < count; i++) {
62                     if (recvbuf[i] != -i * rsize) {
63                         errs++;
64                         if (errs < 10) {
65                             fprintf(stderr, "recvbuf[%d] = %d\n", i, recvbuf[i]);
66                         }
67                     }
68                 }
69             }
70             else {
71                 for (i = 0; i < count; i++) {
72                     if (recvbuf[i] != i * rsize) {
73                         errs++;
74                         if (errs < 10) {
75                             fprintf(stderr, "recvbuf[%d] = %d\n", i, recvbuf[i]);
76                         }
77                     }
78                 }
79             }
80             free(sendbuf);
81             free(recvbuf);
82         }
83         MTestFreeComm(&comm);
84     }
85
86     MTest_Finalize(errs);
87     MPI_Finalize();
88     return 0;
89 }