1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2010 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
7 * Test of reduce scatter with large data (needed in MPICH to trigger the
10 * Each processor contributes its rank + the index to the reduction,
11 * then receives the ith sum
13 * Can be called with any number of processors.
21 /* Limit the number of error reports */
24 int main( int argc, char **argv )
27 int *sendbuf, *recvbuf, *recvcounts;
28 int size, rank, i, j, idx, mycount, sumval;
32 MTest_Init( &argc, &argv );
33 comm = MPI_COMM_WORLD;
35 MPI_Comm_size( comm, &size );
36 MPI_Comm_rank( comm, &rank );
37 recvcounts = (int *)malloc( size * sizeof(int) );
39 fprintf( stderr, "Could not allocate %d ints for recvcounts\n",
41 MPI_Abort( MPI_COMM_WORLD, 1 );
43 mycount = (1024 * 1024) / size;
44 for (i=0; i<size; i++)
45 recvcounts[i] = mycount;
46 sendbuf = (int *) malloc( mycount * size * sizeof(int) );
48 fprintf( stderr, "Could not allocate %d ints for sendbuf\n",
50 MPI_Abort( MPI_COMM_WORLD, 1 );
53 for (i=0; i<size; i++) {
54 for (j=0; j<mycount; j++) {
55 sendbuf[idx++] = rank + i;
58 recvbuf = (int *)malloc( mycount * sizeof(int) );
60 fprintf( stderr, "Could not allocate %d ints for recvbuf\n",
62 MPI_Abort( MPI_COMM_WORLD, 1 );
64 for (i=0; i<mycount; i++) {
68 MPI_Reduce_scatter( sendbuf, recvbuf, recvcounts, MPI_INT, MPI_SUM, comm );
70 sumval = size * rank + ((size - 1) * size)/2;
71 /* recvbuf should be size * (rank + i) */
72 for (i=0; i<mycount; i++) {
73 if (recvbuf[i] != sumval) {
75 if (err < MAX_ERRORS) {
76 fprintf( stdout, "Did not get expected value for reduce scatter\n" );
77 fprintf( stdout, "[%d] Got recvbuf[%d] = %d expected %d\n",
78 rank, i, recvbuf[i], sumval );
83 MPI_Reduce_scatter( MPI_IN_PLACE, sendbuf, recvcounts, MPI_INT, MPI_SUM,
86 sumval = size * rank + ((size - 1) * size)/2;
87 /* recv'ed values for my process should be size * (rank + i) */
88 for (i=0; i<mycount; i++) {
89 if (sendbuf[i] != sumval) {
91 if (err < MAX_ERRORS) {
92 fprintf( stdout, "Did not get expected value for reduce scatter (in place)\n" );
93 fprintf( stdout, "[%d] Got buf[%d] = %d expected %d\n",
94 rank, i, sendbuf[rank*mycount+i], sumval );
103 MTest_Finalize( err );