Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge tag 'v3_9_90' into hypervisor
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / icreduce.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 reduce 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)
30             continue;
31
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_Comm_set_errhandler( comm, MPI_ERRORS_RETURN );
38
39         for (count = 1; count < 65000; count = 2 * count) {
40             sendbuf = (int *)malloc( count * sizeof(int) );
41             recvbuf = (int *)malloc( count * sizeof(int) );
42             for (i=0; i<count; i++) {
43                 sendbuf[i] = -1;
44                 recvbuf[i] = -1;
45             }
46             if (leftGroup) {
47                 err = MPI_Reduce( sendbuf, recvbuf, count, datatype, MPI_SUM,
48                                  (rank == 0) ? MPI_ROOT : MPI_PROC_NULL,
49                                  comm );
50                 if (err) {
51                     errs++;
52                     MTestPrintError( err );
53                 }
54                 /* Test that no other process in this group received the 
55                    broadcast, and that we got the right answers */
56                 if (rank == 0) {
57                     for (i=0; i<count; i++) {
58                         if (recvbuf[i] != i * rsize) {
59                             errs++;
60                         }
61                     }
62                 }
63                 else {
64                     for (i=0; i<count; i++) {
65                         if (recvbuf[i] != -1) {
66                             errs++;
67                         }
68                     }
69                 }
70             }
71             else {
72                 /* In the right group */
73                 for (i=0; i<count; i++) sendbuf[i] = i;
74                 err = MPI_Reduce( sendbuf, recvbuf, count, datatype, MPI_SUM, 
75                                   0, comm );
76                 if (err) {
77                     errs++;
78                     MTestPrintError( err );
79                 }
80                 /* Check that we have received no data */
81                 for (i=0; i<count; i++) {
82                     if (recvbuf[i] != -1) {
83                         errs++;
84                     }
85                 }
86             }
87         free( sendbuf ); 
88         free( recvbuf );
89         }
90         MTestFreeComm( &comm );
91     }
92
93     MTest_Finalize( errs );
94     MPI_Finalize();
95     return 0;
96 }