1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
2 /*
3  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
4  *                         University Research and Technology
5  *                         Corporation.  All rights reserved.
6  * Copyright (c) 2004-2017 The University of Tennessee and The University
7  *                         of Tennessee Research Foundation.  All rights
8  *                         reserved.
9  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
10  *                         University of Stuttgart.  All rights reserved.
11  * Copyright (c) 2004-2005 The Regents of the University of California.
12  *                         All rights reserved.
13  * Copyright (c) 2008-2012 Cisco Systems, Inc.  All rights reserved.
14  * Copyright (c) 2009      Sun Microsystems, Inc.  All rights reserved.
15  * Copyright (c) 2015      Research Organization for Information Science
16  *                         and Technology (RIST). All rights reserved.
17  * Copyright (c) 2015-2016 Intel, Inc. All rights reserved.
18  * $COPYRIGHT$
19  *
20  * Additional copyrights may follow
21  *
22  * $HEADER$
23  */
24 
25 #include "ompi_config.h"
26 
27 #include <string.h>
28 
29 #include "ompi/communicator/communicator.h"
30 #include "ompi/win/win.h"
31 #include "ompi/errhandler/errhandler.h"
32 #include "ompi/errhandler/errhandler_predefined.h"
33 #include "opal/class/opal_pointer_array.h"
34 #include "opal/mca/pmix/pmix.h"
35 
36 
37 /*
38  * Table for Fortran <-> C errhandler handle conversion
39  */
40 opal_pointer_array_t ompi_errhandler_f_to_c_table = {{0}};
41 
42 /*
43  * default errhandler id
44  */
45 static size_t default_errhandler_id = SIZE_MAX;
46 
47 /*
48  * Class information
49  */
50 static void ompi_errhandler_construct(ompi_errhandler_t *eh);
51 static void ompi_errhandler_destruct(ompi_errhandler_t *eh);
52 
53 
54 /*
55  * Class instance
56  */
57 OBJ_CLASS_INSTANCE(ompi_errhandler_t, opal_object_t, ompi_errhandler_construct,
58                    ompi_errhandler_destruct);
59 
60 
61 /*
62  * _addr flavors are for F03 bindings
63  */
64 ompi_predefined_errhandler_t ompi_mpi_errhandler_null = {{{0}}};
65 ompi_predefined_errhandler_t *ompi_mpi_errhandler_null_addr =
66     &ompi_mpi_errhandler_null;
67 ompi_predefined_errhandler_t ompi_mpi_errors_are_fatal = {{{0}}};
68 ompi_predefined_errhandler_t *ompi_mpi_errors_are_fatal_addr =
69     &ompi_mpi_errors_are_fatal;
70 ompi_predefined_errhandler_t ompi_mpi_errors_return = {{{0}}};
71 ompi_predefined_errhandler_t *ompi_mpi_errors_return_addr =
72     &ompi_mpi_errors_return;
73 ompi_predefined_errhandler_t ompi_mpi_errors_throw_exceptions = {{{0}}};
74 ompi_predefined_errhandler_t *ompi_mpi_errors_throw_exceptions_addr =
75     &ompi_mpi_errors_throw_exceptions;
76 
77 
78 /*
79  * Initialize OMPI errhandler infrastructure
80  */
ompi_errhandler_init(void)81 int ompi_errhandler_init(void)
82 {
83   /* initialize ompi_errhandler_f_to_c_table */
84 
85   OBJ_CONSTRUCT( &ompi_errhandler_f_to_c_table, opal_pointer_array_t);
86   if( OPAL_SUCCESS != opal_pointer_array_init(&ompi_errhandler_f_to_c_table, 8,
87                                               OMPI_FORTRAN_HANDLE_MAX, 16) ) {
88     return OMPI_ERROR;
89   }
90 
91   /* Initialize the predefined error handlers */
92   OBJ_CONSTRUCT( &ompi_mpi_errhandler_null.eh, ompi_errhandler_t );
93   if( ompi_mpi_errhandler_null.eh.eh_f_to_c_index != OMPI_ERRHANDLER_NULL_FORTRAN )
94       return OMPI_ERROR;
95   ompi_mpi_errhandler_null.eh.eh_mpi_object_type = OMPI_ERRHANDLER_TYPE_PREDEFINED;
96   ompi_mpi_errhandler_null.eh.eh_lang = OMPI_ERRHANDLER_LANG_C;
97   ompi_mpi_errhandler_null.eh.eh_comm_fn = NULL;
98   ompi_mpi_errhandler_null.eh.eh_file_fn = NULL;
99   ompi_mpi_errhandler_null.eh.eh_win_fn  = NULL ;
100   ompi_mpi_errhandler_null.eh.eh_fort_fn = NULL;
101   strncpy (ompi_mpi_errhandler_null.eh.eh_name, "MPI_ERRHANDLER_NULL",
102 	   strlen("MPI_ERRHANDLER_NULL")+1 );
103 
104 
105   OBJ_CONSTRUCT( &ompi_mpi_errors_are_fatal.eh, ompi_errhandler_t );
106   if( ompi_mpi_errors_are_fatal.eh.eh_f_to_c_index != OMPI_ERRORS_ARE_FATAL_FORTRAN )
107       return OMPI_ERROR;
108   ompi_mpi_errors_are_fatal.eh.eh_mpi_object_type = OMPI_ERRHANDLER_TYPE_PREDEFINED;
109   ompi_mpi_errors_are_fatal.eh.eh_lang = OMPI_ERRHANDLER_LANG_C;
110   ompi_mpi_errors_are_fatal.eh.eh_comm_fn = ompi_mpi_errors_are_fatal_comm_handler;
111   ompi_mpi_errors_are_fatal.eh.eh_file_fn = ompi_mpi_errors_are_fatal_file_handler;
112   ompi_mpi_errors_are_fatal.eh.eh_win_fn  = ompi_mpi_errors_are_fatal_win_handler ;
113   ompi_mpi_errors_are_fatal.eh.eh_fort_fn = NULL;
114   strncpy (ompi_mpi_errors_are_fatal.eh.eh_name, "MPI_ERRORS_ARE_FATAL",
115 	   strlen("MPI_ERRORS_ARE_FATAL")+1 );
116 
117   OBJ_CONSTRUCT( &ompi_mpi_errors_return.eh, ompi_errhandler_t );
118   if( ompi_mpi_errors_return.eh.eh_f_to_c_index != OMPI_ERRORS_RETURN_FORTRAN )
119       return OMPI_ERROR;
120   ompi_mpi_errors_return.eh.eh_mpi_object_type  = OMPI_ERRHANDLER_TYPE_PREDEFINED;
121   ompi_mpi_errors_return.eh.eh_lang = OMPI_ERRHANDLER_LANG_C;
122   ompi_mpi_errors_return.eh.eh_comm_fn = ompi_mpi_errors_return_comm_handler;
123   ompi_mpi_errors_return.eh.eh_file_fn = ompi_mpi_errors_return_file_handler;
124   ompi_mpi_errors_return.eh.eh_win_fn  = ompi_mpi_errors_return_win_handler;
125   ompi_mpi_errors_return.eh.eh_fort_fn = NULL;
126   strncpy (ompi_mpi_errors_return.eh.eh_name, "MPI_ERRORS_RETURN",
127 	   strlen("MPI_ERRORS_RETURN")+1 );
128 
129   /* If we're going to use C++, functions will be fixed up during
130      MPI::Init.  Note that it is proper to use ERRHANDLER_LANG_C here;
131      the dispatch function is in C (although in libmpi_cxx); the
132      conversion from C handles to C++ handles happens in that dispatch
133      function -- not the errhandler_invoke.c stuff here in libmpi. */
134   OBJ_CONSTRUCT( &ompi_mpi_errors_throw_exceptions.eh, ompi_errhandler_t );
135   ompi_mpi_errors_throw_exceptions.eh.eh_mpi_object_type =
136       OMPI_ERRHANDLER_TYPE_PREDEFINED;
137   ompi_mpi_errors_throw_exceptions.eh.eh_lang = OMPI_ERRHANDLER_LANG_C;
138   ompi_mpi_errors_throw_exceptions.eh.eh_comm_fn =
139       ompi_mpi_errors_are_fatal_comm_handler;
140   ompi_mpi_errors_throw_exceptions.eh.eh_file_fn =
141       ompi_mpi_errors_are_fatal_file_handler;
142   ompi_mpi_errors_throw_exceptions.eh.eh_win_fn  =
143       ompi_mpi_errors_are_fatal_win_handler ;
144   ompi_mpi_errors_throw_exceptions.eh.eh_fort_fn = NULL;
145   strncpy (ompi_mpi_errors_throw_exceptions.eh.eh_name, "MPI_ERRORS_THROW_EXCEPTIONS",
146 	   strlen("MPI_ERRORS_THROW_EXCEPTIONS")+1 );
147 
148   /* All done */
149 
150   return OMPI_SUCCESS;
151 }
152 
153 
154 /*
155  * Clean up the errorhandler resources
156  */
ompi_errhandler_finalize(void)157 int ompi_errhandler_finalize(void)
158 {
159     OBJ_DESTRUCT(&ompi_mpi_errhandler_null.eh);
160     OBJ_DESTRUCT(&ompi_mpi_errors_return.eh);
161     OBJ_DESTRUCT(&ompi_mpi_errors_throw_exceptions.eh);
162     OBJ_DESTRUCT(&ompi_mpi_errors_are_fatal.eh);
163 
164     /* JMS Add stuff here checking for unreleased errorhandlers,
165        similar to communicators, info handles, etc. */
166     opal_pmix.deregister_evhandler(default_errhandler_id, NULL, NULL);
167 
168     /* Remove errhandler F2C table */
169 
170     OBJ_DESTRUCT(&ompi_errhandler_f_to_c_table);
171 
172     /* All done */
173 
174     return OMPI_SUCCESS;
175 }
176 
177 
ompi_errhandler_create(ompi_errhandler_type_t object_type,ompi_errhandler_generic_handler_fn_t * func,ompi_errhandler_lang_t lang)178 ompi_errhandler_t *ompi_errhandler_create(ompi_errhandler_type_t object_type,
179 					                      ompi_errhandler_generic_handler_fn_t *func,
180                                           ompi_errhandler_lang_t lang)
181 {
182   ompi_errhandler_t *new_errhandler;
183 
184   /* Create a new object and ensure that it's valid */
185 
186   new_errhandler = OBJ_NEW(ompi_errhandler_t);
187   if (NULL != new_errhandler) {
188     if (0 > new_errhandler->eh_f_to_c_index) {
189       OBJ_RELEASE(new_errhandler);
190       new_errhandler = NULL;
191     } else {
192 
193       /* We cast the user's callback function to any one of the
194          function pointer types in the union; it doesn't matter which.
195          It only matters that we dereference/use the right member when
196          invoking the callback. */
197 
198       new_errhandler->eh_mpi_object_type = object_type;
199       new_errhandler->eh_lang = lang;
200       switch (object_type ) {
201 	  case (OMPI_ERRHANDLER_TYPE_COMM):
202 	      new_errhandler->eh_comm_fn = (MPI_Comm_errhandler_function *)func;
203 	      break;
204 	  case (OMPI_ERRHANDLER_TYPE_FILE):
205 	      new_errhandler->eh_file_fn = (ompi_file_errhandler_fn *)func;
206 	      break;
207 	  case (OMPI_ERRHANDLER_TYPE_WIN):
208 	      new_errhandler->eh_win_fn = (MPI_Win_errhandler_function *)func;
209 	      break;
210 	  default:
211 	      break;
212       }
213 
214       new_errhandler->eh_fort_fn = (ompi_errhandler_fortran_handler_fn_t *)func;
215     }
216   }
217 
218   /* All done */
219 
220   return new_errhandler;
221 }
222 
223 /* registration callback */
ompi_errhandler_registration_callback(int status,size_t errhandler_ref,void * cbdata)224 void ompi_errhandler_registration_callback(int status,
225                                            size_t errhandler_ref,
226                                            void *cbdata)
227 {
228     ompi_errhandler_errtrk_t *errtrk = (ompi_errhandler_errtrk_t*)cbdata;
229 
230     default_errhandler_id = errhandler_ref;
231     errtrk->status = status;
232     errtrk->active = false;
233 }
234 
235 /**
236  * Default errhandler callback
237  */
ompi_errhandler_callback(int status,const opal_process_name_t * source,opal_list_t * info,opal_list_t * results,opal_pmix_notification_complete_fn_t cbfunc,void * cbdata)238 void ompi_errhandler_callback(int status,
239                               const opal_process_name_t *source,
240                               opal_list_t *info, opal_list_t *results,
241                               opal_pmix_notification_complete_fn_t cbfunc,
242                               void *cbdata)
243 {
244     /* tell the event chain engine to go no further - we
245      * will handle this */
246     if (NULL != cbfunc) {
247         cbfunc(OMPI_ERR_HANDLERS_COMPLETE, NULL, NULL, NULL, cbdata);
248     }
249     /* our default action is to abort */
250     ompi_mpi_abort(MPI_COMM_WORLD, status);
251 }
252 
253 /**************************************************************************
254  *
255  * Static functions
256  *
257  **************************************************************************/
258 
259 /**
260  * Errhandler constructor
261  */
ompi_errhandler_construct(ompi_errhandler_t * new_errhandler)262 static void ompi_errhandler_construct(ompi_errhandler_t *new_errhandler)
263 {
264   int ret_val;
265 
266   /* assign entry in fortran <-> c translation array */
267 
268   ret_val = opal_pointer_array_add(&ompi_errhandler_f_to_c_table,
269                                    new_errhandler);
270   new_errhandler->eh_f_to_c_index = ret_val;
271 
272   new_errhandler->eh_lang = OMPI_ERRHANDLER_LANG_C;
273 
274   new_errhandler->eh_comm_fn      = NULL;
275   new_errhandler->eh_win_fn       = NULL;
276   new_errhandler->eh_file_fn      = NULL;
277   new_errhandler->eh_fort_fn      = NULL;
278 
279   new_errhandler->eh_cxx_dispatch_fn = NULL;
280 
281   memset (new_errhandler->eh_name, 0, MPI_MAX_OBJECT_NAME);
282 }
283 
284 
285 /**
286  * Errhandler destructor
287  */
ompi_errhandler_destruct(ompi_errhandler_t * errhandler)288 static void ompi_errhandler_destruct(ompi_errhandler_t *errhandler)
289 {
290   /* reset the ompi_errhandler_f_to_c_table entry - make sure that the
291      entry is in the table */
292 
293   if (NULL!= opal_pointer_array_get_item(&ompi_errhandler_f_to_c_table,
294                                         errhandler->eh_f_to_c_index)) {
295     opal_pointer_array_set_item(&ompi_errhandler_f_to_c_table,
296                                 errhandler->eh_f_to_c_index, NULL);
297   }
298 }
299