1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2001 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
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.
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.
20 The first test sends i items to processor i from all processors.
22 Currently, the test uses only MPI_INT; this is adequate for testing systems
23 that use point-to-point operations
26 int main( int argc, char **argv )
32 int *sendcounts, *recvcounts, *rdispls, *sdispls;
34 MPI_Datatype *sendtypes, *recvtypes;
36 MTest_Init( &argc, &argv );
39 while (MTestGetIntracommGeneral( &comm, 2, 1 )) {
40 if (comm == MPI_COMM_NULL) continue;
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) );
48 fprintf( stderr, "Could not allocated buffers!\n" );
53 /* Load up the buffers */
54 for (i=0; i<size*size; i++) {
55 sbuf[i] = i + 100*rank;
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" );
71 /* Note that process 0 sends no data (sendcounts[0] = 0) */
72 for (i=0; i<size; i++) {
75 rdispls[i] = i * rank * sizeof(int);
76 sdispls[i] = (((i+1) * (i))/2) * sizeof(int);
77 sendtypes[i] = recvtypes[i] = MPI_INT;
79 MPI_Alltoallw( sbuf, sendcounts, sdispls, sendtypes,
80 rbuf, recvcounts, rdispls, recvtypes, comm );
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 );
99 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
100 /* check MPI_IN_PLACE, added in MPI-2.2 */
102 rbuf = (int *)malloc( size * (2 * size) * sizeof(int) );
104 fprintf( stderr, "Could not reallocate rbuf!\n" );
105 MPI_Abort( comm, 1 );
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;
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;
124 MPI_Alltoallw( MPI_IN_PLACE, NULL, NULL, NULL,
125 rbuf, recvcounts, rdispls, recvtypes, comm );
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);
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))
150 MTestFreeComm( &comm );
153 MTest_Finalize( err );