Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Include directory is in source_dir, not in binary_dir.
[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       }
51       
52       /* Load up the buffers */
53       for (i=0; i<size*size; i++) {
54         sbuf[i] = i + 100*rank;
55         rbuf[i] = -i;
56       }
57       
58       /* Create and load the arguments to alltoallv */
59       sendcounts = (int *)malloc( size * sizeof(int) );
60       recvcounts = (int *)malloc( size * sizeof(int) );
61       rdispls    = (int *)malloc( size * sizeof(int) );
62       sdispls    = (int *)malloc( size * sizeof(int) );
63       sendtypes    = (MPI_Datatype *)malloc( size * sizeof(MPI_Datatype) );
64       recvtypes    = (MPI_Datatype *)malloc( size * sizeof(MPI_Datatype) );
65       if (!sendcounts || !recvcounts || !rdispls || !sdispls || !sendtypes || !recvtypes) {
66         fprintf( stderr, "Could not allocate arg items!\n" );
67         MPI_Abort( comm, 1 );
68       }
69       /* Note that process 0 sends no data (sendcounts[0] = 0) */
70       for (i=0; i<size; i++) {
71         sendcounts[i] = i;
72         recvcounts[i] = rank;
73         rdispls[i]    = i * rank * sizeof(int);
74         sdispls[i]    = (((i+1) * (i))/2) * sizeof(int);
75         sendtypes[i] = recvtypes[i] = MPI_INT;
76       }
77       MPI_Alltoallw( sbuf, sendcounts, sdispls, sendtypes,
78                      rbuf, recvcounts, rdispls, recvtypes, comm );
79       
80       /* Check rbuf */
81       for (i=0; i<size; i++) {
82         p = rbuf + rdispls[i]/sizeof(int);
83         for (j=0; j<rank; j++) {
84           if (p[j] != i * 100 + (rank*(rank+1))/2 + j) {
85             fprintf( stderr, "[%d] got %d expected %d for %dth\n",
86                      rank, p[j],(i*(i+1))/2 + j, j );
87             err++;
88           }
89         }
90       }
91
92       free(sendtypes);
93       free(sdispls);
94       free(sendcounts);
95       free(sbuf);
96
97 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
98       /* check MPI_IN_PLACE, added in MPI-2.2 */
99       free( rbuf );
100       rbuf = (int *)malloc( size * (2 * size) * sizeof(int) );
101       if (!rbuf) {
102         fprintf( stderr, "Could not reallocate rbuf!\n" );
103         MPI_Abort( comm, 1 );
104       }
105
106       /* Load up the buffers */
107       for (i = 0; i < size; i++) {
108         /* alltoallw displs are in bytes, not in type extents */
109         rdispls[i]    = i * (2 * size) * sizeof(int);
110         recvtypes[i]  = MPI_INT;
111         recvcounts[i] = i + rank;
112       }
113       memset(rbuf, -1, size * (2 * size) * sizeof(int));
114       for (i=0; i < size; i++) {
115         p = rbuf + (rdispls[i] / sizeof(int));
116         for (j = 0; j < recvcounts[i]; ++j) {
117           p[j] = 100 * rank + 10 * i + j;
118         }
119       }
120
121       MPI_Alltoallw( MPI_IN_PLACE, NULL, NULL, NULL,
122                      rbuf, recvcounts, rdispls, recvtypes, comm );
123
124       /* Check rbuf */
125       for (i=0; i<size; i++) {
126         p = rbuf + (rdispls[i] / sizeof(int));
127         for (j=0; j<recvcounts[i]; j++) {
128           int expected = 100 * i + 10 * rank + j;
129           if (p[j] != expected) {
130             fprintf(stderr, "[%d] got %d expected %d for block=%d, element=%dth\n",
131                     rank, p[j], expected, i, j);
132             ++err;
133           }
134         }
135       }
136 #endif
137
138       free(recvtypes);
139       free(rdispls);
140       free(recvcounts);
141       free(rbuf);
142       MTestFreeComm( &comm );
143     }
144
145     MTest_Finalize( err );
146     MPI_Finalize();
147     return 0;
148 }