1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2011 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
7 * Test of reduce scatter with large data on an intercommunicator
8 * (needed in MPICH to trigger the long-data algorithm)
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 int main( int argc, char **argv )
25 int size, rsize, rank, i;
26 int recvcount, /* Each process receives this much data */
27 sendcount, /* Each process contributes this much data */
28 basecount; /* Unit of elements - basecount *rsize is recvcount,
31 long long *sendbuf, *recvbuf;
36 MTest_Init( &argc, &argv );
37 comm = MPI_COMM_WORLD;
41 while (MTestGetIntercomm( &comm, &isLeftGroup, 2 )) {
42 if (comm == MPI_COMM_NULL) continue;
44 MPI_Comm_remote_size( comm, &rsize );
45 MPI_Comm_size( comm, &size );
46 MPI_Comm_rank( comm, &rank );
49 printf( "[%d] %s (%d,%d) remote %d\n", rank,
50 isLeftGroup ? "L" : "R",
54 recvcount = basecount * rsize;
55 sendcount = basecount * rsize * size;
57 recvcounts = (int *)malloc( size * sizeof(int) );
59 fprintf( stderr, "Could not allocate %d int for recvcounts\n",
61 MPI_Abort( MPI_COMM_WORLD, 1 );
64 for (i=0; i<size; i++)
65 recvcounts[i] = recvcount;
67 sendbuf = (long long *) malloc( sendcount * sizeof(long long) );
69 fprintf( stderr, "Could not allocate %d ints for sendbuf\n",
71 MPI_Abort( MPI_COMM_WORLD, 1 );
75 for (i=0; i<sendcount; i++) {
76 sendbuf[i] = (long long)(rank*sendcount + i);
78 recvbuf = (long long *)malloc( recvcount * sizeof(long long) );
80 fprintf( stderr, "Could not allocate %d ints for recvbuf\n",
82 MPI_Abort( MPI_COMM_WORLD, 1 );
85 for (i=0; i<recvcount; i++) {
86 recvbuf[i] = (long long)(-i);
89 MPI_Reduce_scatter( sendbuf, recvbuf, recvcounts, MPI_LONG_LONG, MPI_SUM,
92 /* Check received data */
93 for (i=0; i<recvcount; i++) {
94 sumval = (long long)(sendcount) * (long long)((rsize * (rsize-1))/2) +
95 (long long)(i + rank * rsize * basecount) * (long long)rsize;
96 if (recvbuf[i] != sumval) {
99 fprintf( stdout, "Did not get expected value for reduce scatter\n" );
100 fprintf( stdout, "[%d] %s recvbuf[%d] = %lld, expected %lld\n",
102 isLeftGroup ? "L" : "R",
103 i, recvbuf[i], sumval );
112 MTestFreeComm( &comm );
115 MTest_Finalize( err );