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_Alltoallv by having processor i send different
14 amounts of data to each processor.
16 Because there are separate send and receive types to alltoallv,
17 there need to be tests to rearrange data on the fly. Not done yet.
19 The first test sends i items to processor i from all processors.
21 Currently, the test uses only MPI_INT; this is adequate for testing systems
22 that use point-to-point operations
25 int main( int argc, char **argv )
31 int *sendcounts, *recvcounts, *rdispls, *sdispls;
34 MTest_Init( &argc, &argv );
37 while (MTestGetIntracommGeneral( &comm, 2, 1 )) {
38 if (comm == MPI_COMM_NULL) continue;
40 /* Create the buffer */
41 MPI_Comm_size( comm, &size );
42 MPI_Comm_rank( comm, &rank );
43 sbuf = (int *)malloc( size * size * sizeof(int) );
44 rbuf = (int *)malloc( size * size * sizeof(int) );
46 fprintf( stderr, "Could not allocated buffers!\n" );
51 /* Load up the buffers */
52 for (i=0; i<size*size; i++) {
53 sbuf[i] = i + 100*rank;
57 /* Create and load the arguments to alltoallv */
58 sendcounts = (int *)malloc( size * sizeof(int) );
59 recvcounts = (int *)malloc( size * sizeof(int) );
60 rdispls = (int *)malloc( size * sizeof(int) );
61 sdispls = (int *)malloc( size * sizeof(int) );
62 if (!sendcounts || !recvcounts || !rdispls || !sdispls) {
63 fprintf( stderr, "Could not allocate arg items!\n" );
67 for (i=0; i<size; i++) {
70 rdispls[i] = i * rank;
71 sdispls[i] = (i * (i+1))/2;
73 MPI_Alltoallv( sbuf, sendcounts, sdispls, MPI_INT,
74 rbuf, recvcounts, rdispls, MPI_INT, comm );
77 for (i=0; i<size; i++) {
78 p = rbuf + rdispls[i];
79 for (j=0; j<rank; j++) {
80 if (p[j] != i * 100 + (rank*(rank+1))/2 + j) {
81 fprintf( stderr, "[%d] got %d expected %d for %dth\n",
82 rank, p[j],(i*(i+1))/2 + j, j );
92 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
93 /* check MPI_IN_PLACE, added in MPI-2.2 */
95 rbuf = (int *)malloc( size * (2 * size) * sizeof(int) );
97 fprintf( stderr, "Could not reallocate rbuf!\n" );
102 /* Load up the buffers */
103 for (i = 0; i < size; i++) {
104 recvcounts[i] = i + rank;
105 rdispls[i] = i * (2 * size);
107 memset(rbuf, -1, size * (2 * size) * sizeof(int));
108 for (i=0; i < size; i++) {
109 p = rbuf + rdispls[i];
110 for (j = 0; j < recvcounts[i]; ++j) {
111 p[j] = 100 * rank + 10 * i + j;
114 MPI_Alltoallv( MPI_IN_PLACE, NULL, NULL, MPI_INT,
115 rbuf, recvcounts, rdispls, MPI_INT, comm );
117 for (i=0; i<size; i++) {
118 p = rbuf + rdispls[i];
119 for (j=0; j<recvcounts[i]; j++) {
120 int expected = 100 * i + 10 * rank + j;
121 if (p[j] != expected) {
122 fprintf(stderr, "[%d] got %d expected %d for block=%d, element=%dth\n",
123 rank, p[j], expected, i, j);
129 /* Check to make sure that aliasing is disallowed correctly */
130 MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
131 if (MPI_SUCCESS == MPI_Alltoallv(rbuf, recvcounts, rdispls, MPI_INT,
132 rbuf, recvcounts, rdispls, MPI_INT, comm))
139 MTestFreeComm( &comm );
142 MTest_Finalize( err );