Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'v3_8_x'
[simgrid.git] / teshsuite / smpi / mpich-test / env / errhand.c
1 #include <stdio.h>
2 #include "mpi.h"
3 #include "test.h"
4
5 int verbose = 0;
6 int global_errors = 0;
7 int Test_errorhandling (void);
8 /* This is complicated by the fact that not all systems correctly 
9    implement stdargs (for the ...) declarations).  MPICH uses USE_STDARG
10    as the choice here, instead of the cases 
11  if defined(__STDC__) || defined(__cplusplus) || defined(HAVE_PROTOTYPES) 
12  */
13 #if defined(USE_STDARG)
14 void handler_a( MPI_Comm *, int *, ...);
15 void handler_b( MPI_Comm *, int *, ...);
16 void error_handler(MPI_Comm *, int *, ...);
17 #else
18 void handler_a ( MPI_Comm *, int * );
19 void handler_b ( MPI_Comm *, int * );
20 void error_handler ( MPI_Comm *, int * );
21 #endif
22
23 /* 
24    Test the error handers (based on a Fortran test program)
25  */
26 int main( int argc, char **argv )
27 {
28     MPI_Init( &argc, &argv );
29
30     Test_errorhandling();
31
32     Test_Waitforall( );
33     MPI_Finalize();
34     return 0;
35 }
36
37 static int a_errors, b_errors;
38
39 int Test_errorhandling( void )
40 {
41     char errstring[MPI_MAX_ERROR_STRING];
42     MPI_Comm dup_comm_world, dummy;
43     MPI_Comm tempcomm;
44     MPI_Errhandler errhandler_a, errhandler_b, errhandler, old_handler;
45     int  err, world_rank, class, resultlen;
46
47 #ifdef FOO
48     logical test_default, test_abort
49 #endif
50
51     MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
52     MPI_Comm_dup( MPI_COMM_WORLD, &dup_comm_world );
53
54     if (world_rank == 0 && verbose) {
55         printf( "*** Error Handling ***\n" );
56     }
57
58 /*
59      Exercise save/restore of user error handlers.
60  */
61     a_errors = 0;
62     MPI_Errhandler_create(handler_a, &errhandler_a);
63
64     MPI_Errhandler_set(dup_comm_world, errhandler_a);
65     MPI_Errhandler_free(&errhandler_a);
66
67     if (verbose) printf( "create with null group 1\n" );
68     MPI_Comm_create(dup_comm_world, MPI_GROUP_NULL, &dummy);
69     if (a_errors != 1) {
70         Test_Failed( "    error handler A not invoked\n" );
71         global_errors ++;
72     }
73     b_errors = 0;
74     MPI_Errhandler_create(handler_b, &errhandler_b);
75     MPI_Errhandler_get(dup_comm_world, &old_handler);
76     /* The following is needed to preserve an old handler */
77     MPI_Comm_dup( MPI_COMM_SELF, &tempcomm );
78     MPI_Errhandler_set( tempcomm, old_handler );
79     MPI_Errhandler_set(dup_comm_world, errhandler_b);
80     MPI_Errhandler_free(&errhandler_b);
81     if (verbose) printf( "create with null group 2\n" );
82     MPI_Comm_create(dup_comm_world, MPI_GROUP_NULL, &dummy);
83     if (b_errors != 1) {
84         Test_Failed( "    error handler B not invoked\n" );
85         global_errors++;
86     }
87
88     MPI_Errhandler_set(dup_comm_world, old_handler);
89     MPI_Comm_free( &tempcomm );
90     /* MPI_Errhandler_free(&old_handler); */
91     if (verbose) printf( "create with null group 3\n" );
92     MPI_Comm_create(dup_comm_world, MPI_GROUP_NULL, &dummy);
93     if (a_errors != 2) {
94         Test_Failed( "    error handler A not re-invoked\n" );
95         global_errors++;
96     }
97 /*
98       Exercise class & string interrogation.
99  */
100     MPI_Errhandler_set(dup_comm_world, MPI_ERRORS_ARE_FATAL);
101
102     if (verbose) 
103         printf( "    Three error messages (from two errors) are expected\n\
104 which should both show an error class of %d\n", MPI_ERR_GROUP );
105
106     MPI_Errhandler_set(dup_comm_world, MPI_ERRORS_RETURN);
107     if (verbose) printf( "create with null group 4\n" );
108     err = MPI_Comm_create(dup_comm_world, MPI_GROUP_NULL, &dummy);
109     if (err != MPI_SUCCESS) {
110         MPI_Error_class(err, &class);
111         MPI_Error_string(err, errstring, &resultlen);
112         if (verbose)
113             printf( "(first) %d : %s\n", class, errstring );
114         else if (class != MPI_ERR_GROUP) {
115             Test_Failed( "(first) Class is not MPI_ERR_GROUP\n" );
116             global_errors++;
117         }
118     }
119     else {
120         MPI_Comm_free( &dummy );
121         Test_Failed( "Did not detect error when building communicator\n" );
122         global_errors++;
123     }
124     MPI_Errhandler_create(error_handler, &errhandler);
125     MPI_Errhandler_set(dup_comm_world, errhandler);
126     MPI_Errhandler_free(&errhandler);
127     if (verbose) printf( "create with null group 5\n" );
128     err = MPI_Comm_create(dup_comm_world, MPI_GROUP_NULL, &dummy);
129     if (err != MPI_SUCCESS) {
130         MPI_Error_class(err, &class);
131         MPI_Error_string(err, errstring, &resultlen);
132         if (verbose) 
133             printf( "(second) %d : %s\n", class, errstring );
134         else if (class != MPI_ERR_GROUP) {
135             Test_Failed( "(second) class was not MPI_ERR_GROUP" );
136             global_errors++;
137         }
138     }
139     else {
140         MPI_Comm_free( &dummy );
141         Test_Failed( "Did not detect error in building communicator\n" );
142         global_errors++;
143     }
144     MPI_Errhandler_set(dup_comm_world, MPI_ERRORS_ARE_FATAL);
145
146 #ifdef FOO
147     if (test_default) {
148         printf("Forcing error for default handler...\n");
149         MPI_Comm_create(dup_comm_world, MPI_GROUP_NULL, &dummy);
150     }
151     if (test_abort) {
152         printf( "Calling MPI_Abort...\n" );
153         MPI_Abort(MPI_COMM_WORLD, 123456768);
154     }
155 #endif
156
157     MPI_Comm_free( &dup_comm_world );
158
159 #if 0
160     errs = global_errors;
161     MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
162     if (world_rank == 0) {
163         if (toterrs == 0) 
164             printf( " No Errors\n" );
165         else
166             printf( " Found %d errors\n", toterrs );
167     }
168 #endif    
169     return 0;
170 }
171
172
173 /*
174
175   Trivial error handler.  Note that FORTRAN error handlers can't
176   deal with the varargs stuff the C handlers can.
177  
178  */
179 #if defined(USE_STDARG)
180 void error_handler(MPI_Comm *comm, int *err, ...)
181 #else
182 void error_handler(MPI_Comm *comm, int *err)
183 #endif
184 {
185     int class;
186     int resultlen;
187     char string[MPI_MAX_ERROR_STRING];
188     
189     MPI_Error_class(*err, &class);
190     MPI_Error_string(*err, string, &resultlen);
191     if (verbose) 
192         printf( "(errhandler) %d : %s\n", class, string );
193     else {
194         if (class != MPI_ERR_GROUP) {
195             printf( "(errhandler) class = %d, expected %d (MPI_ERR_GROUP)\n",
196                     class, MPI_ERR_GROUP );
197             printf( "   message %s\n", string );
198             global_errors++;
199         }
200     }
201 }
202 /* 
203   Error handler A, used for save/restore testing.
204  */
205
206 #if defined(USE_STDARG)
207 void handler_a( MPI_Comm *comm, int *err, ...)
208 #else
209 void handler_a(MPI_Comm *comm, int err)
210 #endif
211 {
212     int class;
213
214     MPI_Error_class(*err, &class);
215     if (class != MPI_ERR_GROUP) {
216         printf( "handler_a: incorrect error class %d\n", class );
217     }
218     *err = MPI_SUCCESS;
219     a_errors++;
220 }
221
222 /* 
223   Error handler B, used for save/restore testing.
224  */
225
226 #if defined(USE_STDARG)
227 void handler_b(MPI_Comm *comm, int *err, ...)
228 #else
229 void handler_b(comm, err)
230 MPI_Comm *comm;
231 int      *err;
232 #endif
233 {
234     int class;
235
236     MPI_Error_class(*err, &class);
237     if (class != MPI_ERR_GROUP) {
238         printf( "handler_b: incorrect error class %d\n", class );
239     }
240     *err = MPI_SUCCESS;
241     b_errors++;
242 }