1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2003 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
13 static char MTEST_Descrip[] = "Test that communicators have reference count semantics";
19 int main( int argc, char *argv[] )
22 int rank, size, source, dest, i;
24 MPI_Comm tmpComm[NCOMM];
29 MTest_Init( &argc, &argv );
31 MPI_Comm_dup( MPI_COMM_WORLD, &comm );
33 /* This is similar to the datatype test, except that we post
34 an irecv on a simple data buffer but use a rank-reordered communicator.
35 In this case, an error in handling the reference count will most
36 likely cause the program to hang, so this should be run only
37 if (a) you are confident that the code is correct or (b)
38 a timeout is set for mpiexec
41 MPI_Comm_rank( comm, &rank );
42 MPI_Comm_size( comm, &size );
45 fprintf( stderr, "This test requires at least two processes." );
46 MPI_Abort( MPI_COMM_WORLD, 1 );
53 buf = (int *)malloc( NELM * sizeof(int) );
54 for (i=0; i<NELM; i++) buf[i] = -i;
55 MPI_Irecv( buf, NELM, MPI_INT, source, 0, comm, &req );
56 MPI_Comm_free( &comm );
58 if (comm != MPI_COMM_NULL) {
60 printf( "Freed comm was not set to COMM_NULL\n" );
63 for (i=0; i<NCOMM; i++) {
64 MPI_Comm_split( MPI_COMM_WORLD, 0, size - rank, &tmpComm[i] );
67 MPI_Sendrecv( 0, 0, MPI_INT, source, 1,
68 0, 0, MPI_INT, source, 1, MPI_COMM_WORLD, &status );
70 MPI_Wait( &req, &status );
71 for (i=0; i<NELM; i++) {
75 printf( "buf[%d] = %d, expected %d\n", i, buf[i], i );
79 for (i=0; i<NCOMM; i++) {
80 MPI_Comm_free( &tmpComm[i] );
84 else if (rank == source) {
85 buf = (int *)malloc( NELM * sizeof(int) );
86 for (i=0; i<NELM; i++) buf[i] = i;
88 for (i=0; i<NCOMM; i++) {
89 MPI_Comm_split( MPI_COMM_WORLD, 0, size - rank, &tmpComm[i] );
91 /* Synchronize with the receiver */
92 MPI_Sendrecv( 0, 0, MPI_INT, dest, 1,
93 0, 0, MPI_INT, dest, 1, MPI_COMM_WORLD, &status );
94 MPI_Send( buf, NELM, MPI_INT, dest, 0, comm );
98 for (i=0; i<NCOMM; i++) {
99 MPI_Comm_split( MPI_COMM_WORLD, 0, size - rank, &tmpComm[i] );
103 MPI_Barrier( MPI_COMM_WORLD );
106 /* Clean up the communicators */
107 for (i=0; i<NCOMM; i++) {
108 MPI_Comm_free( &tmpComm[i] );
111 if (comm != MPI_COMM_NULL) {
112 MPI_Comm_free( &comm );
115 MTest_Finalize( errs );