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 / alltoallw2.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include "mpitest.h"
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 /*
13   This program tests MPI_Alltoallw by having processor i send different
14   amounts of data to each processor.  This is just the MPI_Alltoallv test,
15   but with displacements in bytes rather than units of the datatype.
16
17   Because there are separate send and receive types to alltoallw,
18   there need to be tests to rearrange data on the fly.  Not done yet.
19   
20   The first test sends i items to processor i from all processors.
21
22   Currently, the test uses only MPI_INT; this is adequate for testing systems
23   that use point-to-point operations
24  */
25
26 int main( int argc, char **argv )
27 {
28
29     MPI_Comm comm;
30     int      *sbuf, *rbuf;
31     int      rank, size;
32     int      *sendcounts, *recvcounts, *rdispls, *sdispls;
33     int      i, j, *p, err;
34     MPI_Datatype *sendtypes, *recvtypes;
35     
36     MTest_Init( &argc, &argv );
37     err = 0;
38     
39     while (MTestGetIntracommGeneral( &comm, 2, 1 )) {
40       if (comm == MPI_COMM_NULL) continue;
41
42       /* Create the buffer */
43       MPI_Comm_size( comm, &size );
44       MPI_Comm_rank( comm, &rank );
45       sbuf = (int *)malloc( size * size * sizeof(int) );
46       rbuf = (int *)malloc( size * size * sizeof(int) );
47       if (!sbuf || !rbuf) {
48         fprintf( stderr, "Could not allocated buffers!\n" );
49         MPI_Abort( comm, 1 );
50         exit(1);
51       }
52       
53       /* Load up the buffers */
54       for (i=0; i<size*size; i++) {
55         sbuf[i] = i + 100*rank;
56         rbuf[i] = -i;
57       }
58       
59       /* Create and load the arguments to alltoallv */
60       sendcounts = (int *)malloc( size * sizeof(int) );
61       recvcounts = (int *)malloc( size * sizeof(int) );
62       rdispls    = (int *)malloc( size * sizeof(int) );
63       sdispls    = (int *)malloc( size * sizeof(int) );
64       sendtypes    = (MPI_Datatype *)malloc( size * sizeof(MPI_Datatype) );
65       recvtypes    = (MPI_Datatype *)malloc( size * sizeof(MPI_Datatype) );
66       if (!sendcounts || !recvcounts || !rdispls || !sdispls || !sendtypes || !recvtypes) {
67         fprintf( stderr, "Could not allocate arg items!\n" );
68         MPI_Abort( comm, 1 );
69         exit(1);
70       }
71       /* Note that process 0 sends no data (sendcounts[0] = 0) */
72       for (i=0; i<size; i++) {
73         sendcounts[i] = i;
74         recvcounts[i] = rank;
75         rdispls[i]    = i * rank * sizeof(int);
76         sdispls[i]    = (((i+1) * (i))/2) * sizeof(int);
77         sendtypes[i] = recvtypes[i] = MPI_INT;
78       }
79       MPI_Alltoallw( sbuf, sendcounts, sdispls, sendtypes,
80                      rbuf, recvcounts, rdispls, recvtypes, comm );
81       
82       /* Check rbuf */
83       for (i=0; i<size; i++) {
84         p = rbuf + rdispls[i]/sizeof(int);
85         for (j=0; j<rank; j++) {
86           if (p[j] != i * 100 + (rank*(rank+1))/2 + j) {
87             fprintf( stderr, "[%d] got %d expected %d for %dth\n",
88                      rank, p[j],(i*(i+1))/2 + j, j );
89             err++;
90           }
91         }
92       }
93
94       free(sendtypes);
95       free(sdispls);
96       free(sendcounts);
97       free(sbuf);
98
99 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
100       /* check MPI_IN_PLACE, added in MPI-2.2 */
101       free( rbuf );
102       rbuf = (int *)malloc( size * (2 * size) * sizeof(int) );
103       if (!rbuf) {
104         fprintf( stderr, "Could not reallocate rbuf!\n" );
105         MPI_Abort( comm, 1 );
106         exit(1);
107       }
108
109       /* Load up the buffers */
110       for (i = 0; i < size; i++) {
111         /* alltoallw displs are in bytes, not in type extents */
112         rdispls[i]    = i * (2 * size) * sizeof(int);
113         recvtypes[i]  = MPI_INT;
114         recvcounts[i] = i + rank;
115       }
116       memset(rbuf, -1, size * (2 * size) * sizeof(int));
117       for (i=0; i < size; i++) {
118         p = rbuf + (rdispls[i] / sizeof(int));
119         for (j = 0; j < recvcounts[i]; ++j) {
120           p[j] = 100 * rank + 10 * i + j;
121         }
122       }
123
124       MPI_Alltoallw( MPI_IN_PLACE, NULL, NULL, NULL,
125                      rbuf, recvcounts, rdispls, recvtypes, comm );
126
127       /* Check rbuf */
128       for (i=0; i<size; i++) {
129         p = rbuf + (rdispls[i] / sizeof(int));
130         for (j=0; j<recvcounts[i]; j++) {
131           int expected = 100 * i + 10 * rank + j;
132           if (p[j] != expected) {
133             fprintf(stderr, "[%d] got %d expected %d for block=%d, element=%dth\n",
134                     rank, p[j], expected, i, j);
135             ++err;
136           }
137         }
138       }
139
140       MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
141       if (MPI_SUCCESS == MPI_Alltoallw(rbuf, recvcounts, rdispls, recvtypes,
142                                        rbuf, recvcounts, rdispls, recvtypes, comm))
143           err++;
144 #endif
145
146       free(recvtypes);
147       free(rdispls);
148       free(recvcounts);
149       free(rbuf);
150       MTestFreeComm( &comm );
151     }
152
153     MTest_Finalize( err );
154     MPI_Finalize();
155     return 0;
156 }