Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update comm, datatype from mpich trunk
[simgrid.git] / teshsuite / smpi / mpich3-test / comm / cmfree.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "mpitest.h"
11
12 /*
13 static char MTEST_Descrip[] = "Test that communicators have reference count semantics";
14 */
15
16 #define NELM 128
17 #define NCOMM 1020
18
19 int main( int argc, char *argv[] )
20 {
21     int errs = 0;
22     int rank, size, source, dest, i;
23     MPI_Comm      comm;
24     MPI_Comm      tmpComm[NCOMM];
25     MPI_Status    status;
26     MPI_Request   req;
27     int           *buf=0;
28
29     MTest_Init( &argc, &argv );
30
31     MPI_Comm_dup( MPI_COMM_WORLD, &comm );
32
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 
39     */
40
41     MPI_Comm_rank( comm, &rank );
42     MPI_Comm_size( comm, &size );
43
44     if (size < 2) {
45         fprintf( stderr, "This test requires at least two processes." );
46         MPI_Abort( MPI_COMM_WORLD, 1 );
47         exit(1);
48     }
49
50     source  = 0;
51     dest    = size - 1;
52
53     if (rank == dest) {
54         buf = (int *)malloc( NELM * sizeof(int) );
55         for (i=0; i<NELM; i++) buf[i] = -i;
56         MPI_Irecv( buf, NELM, MPI_INT, source, 0, comm, &req );
57         MPI_Comm_free( &comm );
58
59         if (comm != MPI_COMM_NULL) {
60             errs++;
61             printf( "Freed comm was not set to COMM_NULL\n" );
62         }
63
64         for (i=0; i<NCOMM; i++) {
65             MPI_Comm_split( MPI_COMM_WORLD, 0, size - rank, &tmpComm[i] );
66         }
67
68         MPI_Sendrecv( NULL, 0, MPI_INT, source, 1,
69                       NULL, 0, MPI_INT, source, 1, MPI_COMM_WORLD, &status );
70
71         MPI_Wait( &req, &status );
72         for (i=0; i<NELM; i++) {
73             if (buf[i] != i) {
74                 errs++;
75                 if (errs < 10) {
76                     printf( "buf[%d] = %d, expected %d\n", i, buf[i], i );
77                 }
78             }
79         }
80         for (i=0; i<NCOMM; i++) {
81             MPI_Comm_free( &tmpComm[i] );
82         }
83         free( buf );
84     }
85     else if (rank == source) {
86         buf = (int *)malloc( NELM * sizeof(int) );
87         for (i=0; i<NELM; i++) buf[i] = i;
88
89         for (i=0; i<NCOMM; i++) {
90             MPI_Comm_split( MPI_COMM_WORLD, 0, size - rank, &tmpComm[i] );
91         }
92         /* Synchronize with the receiver */
93         MPI_Sendrecv( NULL, 0, MPI_INT, dest, 1,
94                       NULL, 0, MPI_INT, dest, 1, MPI_COMM_WORLD, &status );
95         MPI_Send( buf, NELM, MPI_INT, dest, 0, comm );
96         free( buf );
97     }
98     else {
99         for (i=0; i<NCOMM; i++) {
100             MPI_Comm_split( MPI_COMM_WORLD, 0, size - rank, &tmpComm[i] );
101         }
102     }
103
104     MPI_Barrier( MPI_COMM_WORLD );
105
106     if (rank != dest) {
107         /* Clean up the communicators */
108         for (i=0; i<NCOMM; i++) {
109             MPI_Comm_free( &tmpComm[i] );
110         }
111     }
112     if (comm != MPI_COMM_NULL) {
113         MPI_Comm_free( &comm );
114     }
115     
116     MTest_Finalize( errs );
117     MPI_Finalize();
118     return 0;
119 }