Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add mpich3 test suite, to replace older one.
[simgrid.git] / teshsuite / smpi / mpich3-test / comm / commcreate1.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2007 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <string.h>
9 #include "mpitest.h"
10
11 /* Check that Communicators can be created from various subsets of the
12    processes in the communicator.
13 */
14
15 void abortMsg( const char *, int );
16 int BuildComm( MPI_Comm, MPI_Group, const char [] );
17
18 void abortMsg( const char *str, int code )
19 {
20     char msg[MPI_MAX_ERROR_STRING];
21     int class, resultLen;
22
23     MPI_Error_class( code, &class );
24     MPI_Error_string( code, msg, &resultLen );
25     fprintf( stderr, "%s: errcode = %d, class = %d, msg = %s\n", 
26              str, code, class, msg );
27     MPI_Abort( MPI_COMM_WORLD, code );
28 }
29
30 int main( int argc, char *argv[] )
31 {
32     MPI_Comm  dupWorld;
33     int       wrank, wsize, gsize, err, errs = 0;
34     int       ranges[1][3];
35     MPI_Group wGroup, godd, ghigh, geven;
36
37     MTest_Init( &argc, &argv );
38
39     MPI_Comm_size( MPI_COMM_WORLD, &wsize );
40     MPI_Comm_rank( MPI_COMM_WORLD, &wrank );
41
42     /* Create some groups */
43     MPI_Comm_group( MPI_COMM_WORLD, &wGroup );
44
45     MTestPrintfMsg( 2, "Creating groups\n" );
46     ranges[0][0] = 2*(wsize/2)-1;
47     ranges[0][1] = 1;
48     ranges[0][2] = -2;
49     err = MPI_Group_range_incl( wGroup, 1, ranges, &godd );
50     if (err) abortMsg( "Failed to create odd group: ", err );
51     err = MPI_Group_size( godd, &gsize );
52     if (err) abortMsg( "Failed to get size of odd group: ", err );
53     if (gsize != wsize/2) {
54         fprintf( stderr, "Group godd size is %d should be %d\n", gsize, 
55                  wsize/2 );
56         errs++;
57     }
58
59     ranges[0][0] = wsize/2+1;
60     ranges[0][1] = wsize-1;
61     ranges[0][2] = 1;
62     err = MPI_Group_range_incl( wGroup, 1, ranges, &ghigh );
63     if (err) abortMsg( "Failed to create high group\n", err );
64     ranges[0][0] = 0;
65     ranges[0][1] = wsize-1;
66     ranges[0][2] = 2;
67     err = MPI_Group_range_incl( wGroup, 1, ranges, &geven );
68     if (err) abortMsg( "Failed to create even group:", err );
69
70     MPI_Comm_dup( MPI_COMM_WORLD, &dupWorld );
71     MPI_Comm_set_name( dupWorld, (char*)"Dup of world" );
72     /* First, use the groups to create communicators from world and a dup
73        of world */
74     errs += BuildComm( MPI_COMM_WORLD, ghigh, "ghigh" );
75     errs += BuildComm( MPI_COMM_WORLD, godd, "godd" );
76     errs += BuildComm( MPI_COMM_WORLD, geven, "geven" );
77     errs += BuildComm( dupWorld, ghigh, "ghigh" );
78     errs += BuildComm( dupWorld, godd, "godd" );
79     errs += BuildComm( dupWorld, geven, "geven" );
80
81 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
82     /* check that we can create multiple communicators from a single collective
83      * call to MPI_Comm_create as long as the groups are all disjoint */
84     errs += BuildComm( MPI_COMM_WORLD, (wrank % 2 ? godd : geven), "godd+geven" );
85     errs += BuildComm( dupWorld,       (wrank % 2 ? godd : geven), "godd+geven" );
86     errs += BuildComm( MPI_COMM_WORLD, MPI_GROUP_EMPTY, "MPI_GROUP_EMPTY" );
87     errs += BuildComm( dupWorld,       MPI_GROUP_EMPTY, "MPI_GROUP_EMPTY" );
88 #endif
89
90     MPI_Comm_free( &dupWorld );
91     MPI_Group_free( &ghigh );
92     MPI_Group_free( &godd );
93     MPI_Group_free( &geven );
94     MPI_Group_free( &wGroup );
95
96     MTest_Finalize( errs );
97
98     MPI_Finalize();
99     return 0;
100 }
101
102 int BuildComm( MPI_Comm oldcomm, MPI_Group group, const char gname[] )
103 {
104     MPI_Comm newcomm;
105     int grank, gsize, rank, size, errs = 0;
106     char cname[MPI_MAX_OBJECT_NAME+1];
107     int  cnamelen;
108
109     MPI_Group_rank( group, &grank );
110     MPI_Group_size( group, &gsize );
111     MPI_Comm_get_name( oldcomm, cname, &cnamelen );
112     MTestPrintfMsg( 2, "Testing comm %s from %s\n", cname, gname );
113     MPI_Comm_create( oldcomm, group, &newcomm );
114     if (newcomm == MPI_COMM_NULL && grank != MPI_UNDEFINED) {
115         errs ++;
116         fprintf( stderr, "newcomm is null but process is in group\n" );
117     }
118     if (newcomm != MPI_COMM_NULL && grank == MPI_UNDEFINED) {
119         errs ++;
120         fprintf( stderr, "newcomm is not null but process is not in group\n" );
121     }
122     if (newcomm != MPI_COMM_NULL && grank != MPI_UNDEFINED) {
123         MPI_Comm_rank( newcomm, &rank );
124         if (rank != grank) {
125             errs ++;
126             fprintf( stderr, "Rank is %d should be %d in comm from %s\n", 
127                      rank, grank, gname );
128         }
129         MPI_Comm_size( newcomm, &size );
130         if (size != gsize) {
131             errs++;
132             fprintf( stderr, "Size is %d should be %d in comm from %s\n",
133                      size, gsize, gname );
134         }
135         MPI_Comm_free( &newcomm );
136         MTestPrintfMsg( 2, "Done testing comm %s from %s\n", cname, gname );
137     }
138     return errs;
139 }