Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
update collectives teshsuite from mpich git (only minor changes)
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / exscan.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[] = "Test MPI_Exscan";
14 */
15
16 int main( int argc, char *argv[] )
17 {
18     int errs = 0;
19     int rank, size;
20     int minsize = 2, count; 
21     int *sendbuf, *recvbuf, i;
22     MPI_Comm      comm;
23
24     MTest_Init( &argc, &argv );
25
26     /* The following illustrates the use of the routines to 
27        run through a selection of communicators and datatypes.
28        Use subsets of these for tests that do not involve combinations 
29        of communicators, datatypes, and counts of datatypes */
30     while (MTestGetIntracommGeneral( &comm, minsize, 1 )) {
31         if (comm == MPI_COMM_NULL) continue;
32
33         MPI_Comm_rank( comm, &rank );
34         MPI_Comm_size( comm, &size );
35         
36         for (count = 1; count < 65000; count = count * 2) {
37
38             sendbuf = (int *)malloc( count * sizeof(int) );
39             recvbuf = (int *)malloc( count * sizeof(int) );
40
41             for (i=0; i<count; i++) {
42                 sendbuf[i] = rank + i * size;
43                 recvbuf[i] = -1;
44             }
45             
46             MPI_Exscan( sendbuf, recvbuf, count, MPI_INT, MPI_SUM, comm );
47
48             /* Check the results.  rank 0 has no data */
49             if (rank > 0) {
50                 int result;
51                 for (i=0; i<count; i++) {
52                     result = rank * i * size + ((rank) * (rank-1))/2;
53                     if (recvbuf[i] != result) {
54                         errs++;
55                         if (errs < 10) {
56                             fprintf( stderr, "Error in recvbuf[%d] = %d on %d, expected %d\n",
57                                      i, recvbuf[i], rank, result );
58                         }
59                     }
60                 }
61             }
62
63 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
64             /* now try the MPI_IN_PLACE flavor */
65             for (i=0; i<count; i++) {
66                 sendbuf[i] = -1; /* unused */
67                 recvbuf[i] = rank + i * size;
68             }
69
70             MPI_Exscan( MPI_IN_PLACE, recvbuf, count, MPI_INT, MPI_SUM, comm );
71
72             /* Check the results.  rank 0's data must remain unchanged */
73             for (i=0; i<count; i++) {
74                 int result;
75                 if (rank == 0)
76                     result = rank + i * size;
77                 else
78                     result = rank * i * size + ((rank) * (rank-1))/2;
79                 if (recvbuf[i] != result) {
80                     errs++;
81                     if (errs < 10) {
82                         fprintf( stderr, "Error in recvbuf[%d] = %d on %d, expected %d\n",
83                                  i, recvbuf[i], rank, result );
84                     }
85                 }
86             }
87
88             MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
89             /* Make sure that we check for buffer aliasing properly */
90             if (MPI_SUCCESS == MPI_Exscan( recvbuf, recvbuf, count, MPI_INT, MPI_SUM, comm ))
91                 errs++;
92 #endif
93
94             free( sendbuf );
95             free( recvbuf );
96         }
97         MTestFreeComm( &comm );
98     }
99
100     MTest_Finalize( errs );
101     MPI_Finalize();
102     return 0;
103 }