1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
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 "mpitest.h"
10 
11 /*
12 static char MTEST_Descrip[] = "Test the routines to control error handlers on windows";
13 */
14 
15 static int calls = 0;
16 static int errs = 0;
17 static MPI_Win mywin;
18 static int expected_err_class = MPI_ERR_OTHER;
19 
20 static int w1Called = 0;
21 static int w2Called = 0;
22 
23 void weh1( MPI_Win *win, int *err, ... );
weh1(MPI_Win * win,int * err,...)24 void weh1( MPI_Win *win, int *err, ... )
25 {
26     int errclass;
27     w1Called ++;
28     MPI_Error_class( *err, &errclass );
29     if (errclass != expected_err_class) {
30 	errs++;
31 	printf( "Unexpected error code (class = %d)\n", errclass );
32     }
33     if (*win != mywin) {
34 	errs++;
35 	printf( "Unexpected window (got %x expected %x)\n",
36 		(int)*win, (int)mywin );
37     }
38     calls++;
39     return;
40 }
41 
42 void weh2( MPI_Win *win, int *err, ... );
weh2(MPI_Win * win,int * err,...)43 void weh2( MPI_Win *win, int *err, ... )
44 {
45     int errclass;
46     w2Called ++;
47     MPI_Error_class( *err, &errclass );
48     if (errclass != expected_err_class) {
49 	errs++;
50 	printf( "Unexpected error code (class = %d)\n", errclass );
51     }
52     if (*win != mywin) {
53 	errs++;
54 	printf( "Unexpected window (got %x expected %x)\n",
55 		(int)*win, (int)mywin );
56     }
57     calls++;
58     return;
59 }
60 
main(int argc,char * argv[])61 int main( int argc, char *argv[] )
62 {
63     int err;
64     int buf[2];
65     MPI_Win       win;
66     MPI_Comm      comm;
67     MPI_Errhandler newerr1, newerr2, olderr;
68 
69 
70     MTest_Init( &argc, &argv );
71     comm = MPI_COMM_WORLD;
72     MPI_Win_create_errhandler( weh1, &newerr1 );
73     MPI_Win_create_errhandler( weh2, &newerr2 );
74 
75     MPI_Win_create( buf, 2*sizeof(int), sizeof(int),
76 		    MPI_INFO_NULL, comm, &win );
77 
78     mywin = win;
79     MPI_Win_get_errhandler( win, &olderr );
80     if (olderr != MPI_ERRORS_ARE_FATAL) {
81 	errs++;
82 	printf( "Expected errors are fatal\n" );
83     }
84 
85     MPI_Win_set_errhandler( win, newerr1 );
86     /* We should be able to free the error handler now since the window is
87        using it */
88     MPI_Errhandler_free( &newerr1 );
89 
90     expected_err_class = MPI_ERR_RANK;
91     err = MPI_Put( buf, 1, MPI_INT, -5, 0, 1, MPI_INT, win );
92     if (w1Called != 1) {
93 	errs ++;
94 	printf( "newerr1 not called\n" );
95 	w1Called = 1;
96     }
97     expected_err_class = MPI_ERR_OTHER;
98     MPI_Win_call_errhandler( win, MPI_ERR_OTHER );
99     if (w1Called != 2) {
100 	errs ++;
101 	printf( "newerr1 not called (2)\n" );
102     }
103 
104     if (w1Called != 2 || w2Called != 0) {
105 	errs++;
106 	printf( "Error handler weh1 not called the expected number of times\n" );
107     }
108 
109     /* Try another error handler.  This should allow the MPI implementation to
110        free the first error handler */
111     MPI_Win_set_errhandler( win, newerr2 );
112     MPI_Errhandler_free( &newerr2 );
113 
114     expected_err_class = MPI_ERR_RANK;
115     err = MPI_Put( buf, 1, MPI_INT, -5, 0, 1, MPI_INT, win );
116     if (w2Called != 1) {
117 	errs ++;
118 	printf( "newerr2 not called\n" );
119 	calls = 1;
120     }
121     expected_err_class = MPI_ERR_OTHER;
122     MPI_Win_call_errhandler( win, MPI_ERR_OTHER );
123     if (w2Called != 2) {
124 	errs ++;
125 	printf( "newerr2 not called (2)\n" );
126     }
127     if (w1Called != 2 || w2Called != 2) {
128 	errs++;
129 	printf( "Error handler weh1 not called the expected number of times\n" );
130     }
131 
132     MPI_Win_free( &win );
133 
134     MTest_Finalize( errs );
135     MPI_Finalize();
136     return 0;
137 }
138