1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
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
28 * <0> sbuf: (#9): [0][1][2][3][4][5][6][7][8]
29 <0> scount: (#3): [0][1][2]
30 <0> rcount: (#3): [0][0][0]
31 <0> sdisp: (#3): [0][1][3]
32 <0> rdisp: (#3): [0][0][0]
34 <1> sbuf: (#9): [100][101][102][103][104][105][106][107][108]
35 <1> scount: (#3): [0][1][2]
36 <1> rcount: (#3): [1][1][1]
37 <1> sdisp: (#3): [0][1][3]
38 <1> rdisp: (#3): [0][1][2]
40 <2> sbuf: (#9): [200][201][202][203][204][205][206][207][208]
41 <2> scount: (#3): [0][1][2]
42 <2> rcount: (#3): [2][2][2]
43 <2> sdisp: (#3): [0][1][3]
44 <2> rdisp: (#3): [0][2][4]
47 <0> rbuf: (#9): [0][-1][-2][-3][-4][-5][-6][-7][-8]
48 <1> rbuf: (#9): [1][101][201][-3][-4][-5][-6][-7][-8]
49 <2> rbuf: (#9): [3][4][103][104][203][204][-6][-7][-8]
53 static void print_buffer_int(void *buf, int len, char *msg, int rank)
56 printf("**<%d> %s (#%d): ", rank, msg,len);
57 for (tmp = 0; tmp < len; tmp++) {
59 printf("[%d]", v[tmp]);
66 int main( int argc, char **argv )
70 int *sbuf, *rbuf, *erbuf;
72 int *sendcounts, *recvcounts, *rdispls, *sdispls;
75 MPI_Init( &argc, &argv );
78 comm = MPI_COMM_WORLD;
80 /* Create the buffer */
81 MPI_Comm_size( comm, &size );
82 MPI_Comm_rank( comm, &rank );
83 sbuf = (int *)malloc( size * size * sizeof(int) );
84 rbuf = (int *)malloc( size * size * sizeof(int) );
85 erbuf = (int *)malloc( size * size * sizeof(int) ); // expected
87 fprintf( stderr, "Could not allocated buffers!\n" );
91 /* Load up the buffers */
92 for (i=0; i<size*size; i++) {
93 sbuf[i] = i + 100*rank;
98 /* Create and load the arguments to alltoallv */
99 sendcounts = (int *)malloc( size * sizeof(int) );
100 recvcounts = (int *)malloc( size * sizeof(int) );
101 rdispls = (int *)malloc( size * sizeof(int) );
102 sdispls = (int *)malloc( size * sizeof(int) );
103 if (!sendcounts || !recvcounts || !rdispls || !sdispls) {
104 fprintf( stderr, "Could not allocate arg items!\n" );
105 MPI_Abort( comm, 1 );
107 for (i=0; i<size; i++) {
109 recvcounts[i] = rank;
110 rdispls[i] = i * rank;
111 sdispls[i] = (i * (i+1))/2;
116 print_buffer_int( sbuf, size*size, strdup("sbuf:"),rank);
117 print_buffer_int( sendcounts, size, strdup("scount:"),rank);
118 print_buffer_int( recvcounts, size, strdup("rcount:"),rank);
119 print_buffer_int( sdispls, size, strdup("sdisp:"),rank);
120 print_buffer_int( rdispls, size, strdup("rdisp:"),rank);
126 for (i=0; i<size; i++) {
127 for (j=0; j<rank; j++) {
128 *(erbuf+j+ rdispls[i]) = i * 100 + (rank*(rank+1))/2 + j;
134 //print_buffer_int( erbuf, size*size, strdup("erbuf:"),rank);
136 MPI_Alltoallv( sbuf, sendcounts, sdispls, MPI_INT,
137 rbuf, recvcounts, rdispls, MPI_INT, comm );
139 // debug: print_buffer_int( rbuf, size*size, strdup("rbuf:"),rank);
143 for (i=0; i<size; i++) {
144 p = rbuf + rdispls[i];
145 for (j=0; j<rank; j++) {
146 if (p[j] != i * 100 + (rank*(rank+1))/2 + j) {
147 fprintf( stderr, "** Error: <%d> got %d expected %d for %dth\n",
148 rank, p[j],(i*(i+1))/2 + j, j );
156 printf("<%d> Alltoallv test: failure (%d errors).\n",rank,err);
159 printf("* Alltoallv TEST COMPLETE.\n");
168 MPI_Barrier( MPI_COMM_WORLD);