Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
df377720b464b621a437062108e2a58258fe79ef
[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     }
48
49     source  = 0;
50     dest    = size - 1;
51
52     if (rank == dest) {
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 );
57
58         if (comm != MPI_COMM_NULL) {
59             errs++;
60             printf( "Freed comm was not set to COMM_NULL\n" );
61         }
62
63         for (i=0; i<NCOMM; i++) {
64             MPI_Comm_split( MPI_COMM_WORLD, 0, size - rank, &tmpComm[i] );
65         }
66
67         MPI_Sendrecv( 0, 0, MPI_INT, source, 1, 
68                       0, 0, MPI_INT, source, 1, MPI_COMM_WORLD, &status );
69
70         MPI_Wait( &req, &status );
71         for (i=0; i<NELM; i++) {
72             if (buf[i] != i) {
73                 errs++;
74                 if (errs < 10) {
75                     printf( "buf[%d] = %d, expected %d\n", i, buf[i], i );
76                 }
77             }
78         }
79         for (i=0; i<NCOMM; i++) {
80             MPI_Comm_free( &tmpComm[i] );
81         }
82         free( buf );
83     }
84     else if (rank == source) {
85         buf = (int *)malloc( NELM * sizeof(int) );
86         for (i=0; i<NELM; i++) buf[i] = i;
87
88         for (i=0; i<NCOMM; i++) {
89             MPI_Comm_split( MPI_COMM_WORLD, 0, size - rank, &tmpComm[i] );
90         }
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 );
95         free( buf );
96     }
97     else {
98         for (i=0; i<NCOMM; i++) {
99             MPI_Comm_split( MPI_COMM_WORLD, 0, size - rank, &tmpComm[i] );
100         }
101     }
102
103     MPI_Barrier( MPI_COMM_WORLD );
104
105     if (rank != dest) {
106         /* Clean up the communicators */
107         for (i=0; i<NCOMM; i++) {
108             MPI_Comm_free( &tmpComm[i] );
109         }
110     }
111     if (comm != MPI_COMM_NULL) {
112         MPI_Comm_free( &comm );
113     }
114     
115     MTest_Finalize( errs );
116     MPI_Finalize();
117     return 0;
118 }