Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MPI_Abort can theorically fail. Add a call to exit() to ensure that the program...
[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     exit(code);
29 }
30
31 int main( int argc, char *argv[] )
32 {
33     MPI_Comm  dupWorld;
34     int       wrank, wsize, gsize, err, errs = 0;
35     int       ranges[1][3];
36     MPI_Group wGroup, godd, ghigh, geven;
37
38     MTest_Init( &argc, &argv );
39
40     MPI_Comm_size( MPI_COMM_WORLD, &wsize );
41     MPI_Comm_rank( MPI_COMM_WORLD, &wrank );
42
43     /* Create some groups */
44     MPI_Comm_group( MPI_COMM_WORLD, &wGroup );
45
46     MTestPrintfMsg( 2, "Creating groups\n" );
47     ranges[0][0] = 2*(wsize/2)-1;
48     ranges[0][1] = 1;
49     ranges[0][2] = -2;
50     err = MPI_Group_range_incl( wGroup, 1, ranges, &godd );
51     if (err) abortMsg( "Failed to create odd group: ", err );
52     err = MPI_Group_size( godd, &gsize );
53     if (err) abortMsg( "Failed to get size of odd group: ", err );
54     if (gsize != wsize/2) {
55         fprintf( stderr, "Group godd size is %d should be %d\n", gsize, 
56                  wsize/2 );
57         errs++;
58     }
59
60     ranges[0][0] = wsize/2+1;
61     ranges[0][1] = wsize-1;
62     ranges[0][2] = 1;
63     err = MPI_Group_range_incl( wGroup, 1, ranges, &ghigh );
64     if (err) abortMsg( "Failed to create high group\n", err );
65     ranges[0][0] = 0;
66     ranges[0][1] = wsize-1;
67     ranges[0][2] = 2;
68     err = MPI_Group_range_incl( wGroup, 1, ranges, &geven );
69     if (err) abortMsg( "Failed to create even group:", err );
70
71     MPI_Comm_dup( MPI_COMM_WORLD, &dupWorld );
72     MPI_Comm_set_name( dupWorld, (char*)"Dup of world" );
73     /* First, use the groups to create communicators from world and a dup
74        of world */
75     errs += BuildComm( MPI_COMM_WORLD, ghigh, "ghigh" );
76     errs += BuildComm( MPI_COMM_WORLD, godd, "godd" );
77     errs += BuildComm( MPI_COMM_WORLD, geven, "geven" );
78     errs += BuildComm( dupWorld, ghigh, "ghigh" );
79     errs += BuildComm( dupWorld, godd, "godd" );
80     errs += BuildComm( dupWorld, geven, "geven" );
81
82 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
83     /* check that we can create multiple communicators from a single collective
84      * call to MPI_Comm_create as long as the groups are all disjoint */
85     errs += BuildComm( MPI_COMM_WORLD, (wrank % 2 ? godd : geven), "godd+geven" );
86     errs += BuildComm( dupWorld,       (wrank % 2 ? godd : geven), "godd+geven" );
87     errs += BuildComm( MPI_COMM_WORLD, MPI_GROUP_EMPTY, "MPI_GROUP_EMPTY" );
88     errs += BuildComm( dupWorld,       MPI_GROUP_EMPTY, "MPI_GROUP_EMPTY" );
89 #endif
90
91     MPI_Comm_free( &dupWorld );
92     MPI_Group_free( &ghigh );
93     MPI_Group_free( &godd );
94     MPI_Group_free( &geven );
95     MPI_Group_free( &wGroup );
96
97     MTest_Finalize( errs );
98
99     MPI_Finalize();
100     return 0;
101 }
102
103 int BuildComm( MPI_Comm oldcomm, MPI_Group group, const char gname[] )
104 {
105     MPI_Comm newcomm;
106     int grank, gsize, rank, size, errs = 0;
107     char cname[MPI_MAX_OBJECT_NAME+1];
108     int  cnamelen;
109
110     MPI_Group_rank( group, &grank );
111     MPI_Group_size( group, &gsize );
112     MPI_Comm_get_name( oldcomm, cname, &cnamelen );
113     MTestPrintfMsg( 2, "Testing comm %s from %s\n", cname, gname );
114     MPI_Comm_create( oldcomm, group, &newcomm );
115     if (newcomm == MPI_COMM_NULL && grank != MPI_UNDEFINED) {
116         errs ++;
117         fprintf( stderr, "newcomm is null but process is in group\n" );
118     }
119     if (newcomm != MPI_COMM_NULL && grank == MPI_UNDEFINED) {
120         errs ++;
121         fprintf( stderr, "newcomm is not null but process is not in group\n" );
122     }
123     if (newcomm != MPI_COMM_NULL && grank != MPI_UNDEFINED) {
124         MPI_Comm_rank( newcomm, &rank );
125         if (rank != grank) {
126             errs ++;
127             fprintf( stderr, "Rank is %d should be %d in comm from %s\n", 
128                      rank, grank, gname );
129         }
130         MPI_Comm_size( newcomm, &size );
131         if (size != gsize) {
132             errs++;
133             fprintf( stderr, "Size is %d should be %d in comm from %s\n",
134                      size, gsize, gname );
135         }
136         MPI_Comm_free( &newcomm );
137         MTestPrintfMsg( 2, "Done testing comm %s from %s\n", cname, gname );
138     }
139     return errs;
140 }