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 / alltoall1.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 "mpitest.h"
10 #include <stdlib.h>
11
12 /*
13 static char MTEST_Descrip[] = "";
14 */
15
16 int main( int argc, char *argv[] )
17 {
18     int errs = 0;
19     int rank, size;
20     int minsize = 2, count; 
21     MPI_Comm      comm;
22     int *sendbuf, *recvbuf, *p;
23     int sendcount, recvcount;
24     int i, j;
25     MPI_Datatype sendtype, recvtype;
26
27     MTest_Init( &argc, &argv );
28
29     /* The following illustrates the use of the routines to 
30        run through a selection of communicators and datatypes.
31        Use subsets of these for tests that do not involve combinations 
32        of communicators, datatypes, and counts of datatypes */
33     while (MTestGetIntracommGeneral( &comm, minsize, 1 )) {
34         if (comm == MPI_COMM_NULL) continue;
35
36         /* Determine the sender and receiver */
37         MPI_Comm_rank( comm, &rank );
38         MPI_Comm_size( comm, &size );
39         
40         /* printf( "Size of comm = %d\n", size ); */
41         for (count = 1; count < 65000; count = count * 2) {
42             
43             /* Create a send buf and a receive buf suitable for testing
44                all to all.  */
45             sendcount = count;
46             recvcount = count;
47             sendbuf   = (int *)malloc( count * size * sizeof(int) );
48             recvbuf   = (int *)malloc( count * size * sizeof(int) );
49             sendtype  = MPI_INT;
50             recvtype  = MPI_INT;
51
52             if (!sendbuf || !recvbuf) {
53                 errs++;
54                 fprintf( stderr, "Failed to allocate sendbuf and/or recvbuf\n" );
55                 MPI_Abort( MPI_COMM_WORLD, 1 );
56                 exit(1);
57             }
58             for (i=0; i<count*size; i++) 
59                 recvbuf[i] = -1;
60             p = sendbuf;
61             for (j=0; j<size; j++) {
62                 for (i=0; i<count; i++) {
63                     *p++ = j * size + rank + i;
64                 }
65             }
66
67             MPI_Alltoall( sendbuf, sendcount, sendtype,
68                           recvbuf, recvcount, recvtype, comm );
69
70             p = recvbuf;
71             for (j=0; j<size; j++) {
72                 for (i=0; i<count; i++) {
73                     if (*p != rank * size + j + i) {
74                         errs++;
75                         if (errs < 10) {
76                             fprintf( stderr, "Error with communicator %s and size=%d count=%d\n",
77                                      MTestGetIntracommName(), size, count );
78                             fprintf( stderr, "recvbuf[%d,%d] = %d, should %d\n",
79                                      j,i, *p, rank * size + j + i );
80                         }
81                     }
82                     p++;
83                 }
84             }
85
86 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
87             /* check MPI_IN_PLACE, added in MPI-2.2 */
88             p = recvbuf;
89             for (j=0; j<size; j++) {
90                 for (i=0; i<count; i++) {
91                     *p++ = j * size + rank + i;
92                 }
93             }
94             MPI_Alltoall( MPI_IN_PLACE, -1/*ignored*/, MPI_DATATYPE_NULL/*ignored*/,
95                           recvbuf, recvcount, recvtype, comm );
96             p = recvbuf;
97             for (j=0; j<size; j++) {
98                 for (i=0; i<count; i++) {
99                     if (*p != rank * size + j + i) {
100                         errs++;
101                         if (errs < 10) {
102                             fprintf( stderr, "Error (MPI_IN_PLACE) with communicator %s and size=%d count=%d\n",
103                                      MTestGetIntracommName(), size, count );
104                             fprintf(stderr, "recvbuf[%d,%d] = %d, should be %d\n",
105                                     j,i, *p, rank * size + j + i );
106                         }
107                     }
108                     p++;
109                 }
110             }
111 #endif
112
113             free( recvbuf );
114             free( sendbuf );
115         }
116         MTestFreeComm( &comm );
117     }
118
119 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
120     /* Check to make sure that aliasing is disallowed correctly */
121     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
122     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
123     if (MPI_SUCCESS == MPI_Alltoall(&rank, 1, MPI_INT, &rank, 1, MPI_INT, MPI_COMM_WORLD))
124         errs++;
125 #endif
126
127     MTest_Finalize( errs );
128     MPI_Finalize();
129     return 0;
130 }