Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change include order for smpi tests/examples to avoid conflicts
[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
12 /*
13 static char MTEST_Descrip[] = "Simple intercomm allreduce test";
14 */
15
16 int main( int argc, char *argv[] )
17 {
18     int errs = 0, err;
19     int *sendbuf = 0, *recvbuf = 0;
20     int leftGroup, i, count, rank, rsize;
21     MPI_Comm comm;
22     MPI_Datatype datatype;
23
24     MTest_Init( &argc, &argv );
25
26     datatype = MPI_INT;
27     /* Get an intercommunicator */
28     while (MTestGetIntercomm( &comm, &leftGroup, 4 )) {
29         if (comm == MPI_COMM_NULL) continue;
30         MPI_Comm_rank( comm, &rank );
31         MPI_Comm_remote_size( comm, &rsize );
32
33         /* To improve reporting of problems about operations, we
34            change the error handler to errors return */
35         MPI_Errhandler_set( comm, MPI_ERRORS_RETURN );
36
37         for (count = 1; count < 65000; count = 2 * count) {
38             /* printf( "rank = %d(%d)\n", rank, leftGroup ); fflush(stdout); */
39             sendbuf = (int *)malloc( count * sizeof(int) );
40             recvbuf = (int *)malloc( count * sizeof(int) );
41             if (leftGroup) {
42                 for (i=0; i<count; i++) sendbuf[i] = i;
43             }
44             else {
45                 for (i=0; i<count; i++) sendbuf[i] = -i;
46             }
47             for (i=0; i<count; i++) recvbuf[i] = 0;
48             err = MPI_Allreduce( sendbuf, recvbuf, count, datatype, 
49                                  MPI_SUM, comm );
50             if (err) {
51                 errs++;
52                 MTestPrintError( err );
53             }
54             /* In each process should be the sum of the values from the
55                other process */
56             if (leftGroup) {
57                 for (i=0; i<count; i++) {
58                     if (recvbuf[i] != -i * rsize) {
59                         errs++;
60                         if (errs < 10) {
61                             fprintf( stderr, "recvbuf[%d] = %d\n", i, recvbuf[i] );
62                         }
63                     }
64                 }
65             }
66             else {
67                 for (i=0; i<count; i++) {
68                     if (recvbuf[i] != i * rsize) {
69                         errs++;
70                         if (errs < 10) {
71                             fprintf( stderr, "recvbuf[%d] = %d\n", i, recvbuf[i] );
72                         }
73                     }
74                 }
75             }
76             free( sendbuf );
77             free( recvbuf );
78         }
79         MTestFreeComm( &comm );
80     }
81
82     MTest_Finalize( errs );
83     MPI_Finalize();
84     return 0;
85 }