Skip to content

Commit 703b8c3

Browse files
committed
Make error_class and error_string callable before/after
MPI_INIT/FINALIZE Signed-off-by: Aurélien Bouteiller <bouteill@icl.utk.edu> make lazy initialization opal unlikely Signed-off-by: Aurelien Bouteiller <bouteill@icl.utk.edu>
1 parent 7702dfc commit 703b8c3

File tree

4 files changed

+129
-74
lines changed

4 files changed

+129
-74
lines changed

ompi/errhandler/errcode.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,17 @@ do { \
131131
opal_pointer_array_set_item(&ompi_mpi_errcodes, (ERRCODE), &(VAR)); \
132132
} while (0)
133133

134+
static opal_mutex_t errcode_init_lock = OPAL_MUTEX_STATIC_INIT;
135+
134136
int ompi_mpi_errcode_init (void)
135137
{
138+
opal_mutex_lock(&errcode_init_lock);
139+
if ( 0 != ompi_mpi_errcode_lastpredefined ) {
140+
/* Already initialized (presumably by an API call before MPI_init */
141+
opal_mutex_unlock(&errcode_init_lock);
142+
return OMPI_SUCCESS;
143+
}
144+
136145
/* Initialize the pointer array, which will hold the references to
137146
the error objects */
138147
OBJ_CONSTRUCT(&ompi_mpi_errcodes, opal_pointer_array_t);
@@ -223,6 +232,7 @@ int ompi_mpi_errcode_init (void)
223232
MPI_ERR_LASTCODE. So just start it as == MPI_ERR_LASTCODE. */
224233
ompi_mpi_errcode_lastused = MPI_ERR_LASTCODE;
225234
ompi_mpi_errcode_lastpredefined = MPI_ERR_LASTCODE;
235+
opal_mutex_unlock(&errcode_init_lock);
226236
return OMPI_SUCCESS;
227237
}
228238

@@ -231,6 +241,7 @@ int ompi_mpi_errcode_finalize(void)
231241
int i;
232242
ompi_mpi_errcode_t *errc;
233243

244+
opal_mutex_lock(&errcode_init_lock);
234245
for (i=ompi_mpi_errcode_lastpredefined+1; i<=ompi_mpi_errcode_lastused; i++) {
235246
/*
236247
* there are some user defined error-codes, which
@@ -317,6 +328,8 @@ int ompi_mpi_errcode_finalize(void)
317328
OBJ_DESTRUCT(&ompi_t_err_invalid_name);
318329

319330
OBJ_DESTRUCT(&ompi_mpi_errcodes);
331+
ompi_mpi_errcode_lastpredefined = 0;
332+
opal_mutex_unlock(&errcode_init_lock);
320333
return OMPI_SUCCESS;
321334
}
322335

ompi/errhandler/errcode.h

Lines changed: 86 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
44
* University Research and Technology
55
* Corporation. All rights reserved.
6-
* Copyright (c) 2004-2007 The University of Tennessee and The University
6+
* Copyright (c) 2004-2020 The University of Tennessee and The University
77
* of Tennessee Research Foundation. All rights
88
* reserved.
99
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
@@ -55,11 +55,68 @@ OMPI_DECLSPEC extern int ompi_mpi_errcode_lastpredefined;
5555

5656
OMPI_DECLSPEC extern ompi_mpi_errcode_t ompi_err_unknown;
5757

58+
/**
59+
* Initialize the error codes
60+
*
61+
* @returns OMPI_SUCCESS Upon success
62+
* @returns OMPI_ERROR Otherwise
63+
*
64+
* Invoked from ompi_mpi_init(); sets up all static MPI error codes,
65+
*/
66+
int ompi_mpi_errcode_init(void);
67+
68+
/**
69+
* Finalize the error codes.
70+
*
71+
* @returns OMPI_SUCCESS Always
72+
*
73+
* Invokes from ompi_mpi_finalize(); tears down the error code array.
74+
*/
75+
int ompi_mpi_errcode_finalize(void);
76+
77+
/**
78+
* Add an error code
79+
*
80+
* @param: error class to which this new error code belongs to
81+
*
82+
* @returns the new error code on SUCCESS (>0)
83+
* @returns OMPI_ERROR otherwise
84+
*
85+
*/
86+
int ompi_mpi_errcode_add (int errclass);
87+
88+
/**
89+
* Add an error class
90+
*
91+
* @param: none
92+
*
93+
* @returns the new error class on SUCCESS (>0)
94+
* @returns OMPI_ERROR otherwise
95+
*
96+
*/
97+
int ompi_mpi_errclass_add (void);
98+
99+
/**
100+
* Add an error string to an error code
101+
*
102+
* @param: error code for which the string is defined
103+
* @param: error string to add
104+
* @param: length of the string
105+
*
106+
* @returns OMPI_SUCCESS on success
107+
* @returns OMPI_ERROR on error
108+
*/
109+
int ompi_mpi_errnum_add_string (int errnum, const char* string, int len);
110+
58111
/**
59112
* Check for a valid error code
60113
*/
61114
static inline bool ompi_mpi_errcode_is_invalid(int errcode)
62115
{
116+
if (OPAL_UNLIKELY( 0 == ompi_mpi_errcode_lastpredefined )) {
117+
ompi_mpi_errcode_init();
118+
}
119+
63120
if ( errcode >= 0 && errcode <= ompi_mpi_errcode_lastused )
64121
return 0;
65122
else
@@ -73,23 +130,31 @@ static inline int ompi_mpi_errcode_get_class (int errcode)
73130
{
74131
ompi_mpi_errcode_t *err = NULL;
75132

133+
if (OPAL_UNLIKELY( 0 == ompi_mpi_errcode_lastpredefined )) {
134+
ompi_mpi_errcode_init();
135+
}
136+
76137
if (errcode >= 0) {
77138
err = (ompi_mpi_errcode_t *)opal_pointer_array_get_item(&ompi_mpi_errcodes, errcode);
78139
/* If we get a bogus errcode, return MPI_ERR_UNKNOWN */
79140
}
80141

81142
if (NULL != err) {
82-
if ( err->code != MPI_UNDEFINED ) {
83-
return err->cls;
84-
}
143+
if ( err->code != MPI_UNDEFINED ) {
144+
return err->cls;
145+
}
85146
}
86147
return ompi_err_unknown.cls;
87148
}
88149

89150
static inline int ompi_mpi_errcode_is_predefined ( int errcode )
90151
{
152+
if (OPAL_UNLIKELY( 0 == ompi_mpi_errcode_lastpredefined )) {
153+
ompi_mpi_errcode_init();
154+
}
155+
91156
if ( errcode >= 0 && errcode <= ompi_mpi_errcode_lastpredefined )
92-
return true;
157+
return true;
93158

94159
return false;
95160
}
@@ -98,23 +163,27 @@ static inline int ompi_mpi_errnum_is_class ( int errnum )
98163
{
99164
ompi_mpi_errcode_t *err;
100165

166+
if (OPAL_UNLIKELY( 0 == ompi_mpi_errcode_lastpredefined )) {
167+
ompi_mpi_errcode_init();
168+
}
169+
101170
if (errnum < 0) {
102171
return false;
103172
}
104173

105174
if ( errnum <= ompi_mpi_errcode_lastpredefined ) {
106-
/* Predefined error values represent an error code and
107-
an error class at the same time */
108-
return true;
175+
/* Predefined error values represent an error code and
176+
an error class at the same time */
177+
return true;
109178
}
110179

111180
err = (ompi_mpi_errcode_t *)opal_pointer_array_get_item(&ompi_mpi_errcodes, errnum);
112181
if (NULL != err) {
113-
if ( MPI_UNDEFINED == err->code) {
114-
/* Distinction between error class and error code is that for the
115-
first one the code section is set to MPI_UNDEFINED */
116-
return true;
117-
}
182+
if ( MPI_UNDEFINED == err->code) {
183+
/* Distinction between error class and error code is that for the
184+
first one the code section is set to MPI_UNDEFINED */
185+
return true;
186+
}
118187
}
119188

120189
return false;
@@ -128,6 +197,10 @@ static inline char* ompi_mpi_errnum_get_string (int errnum)
128197
{
129198
ompi_mpi_errcode_t *err = NULL;
130199

200+
if (OPAL_UNLIKELY( 0 == ompi_mpi_errcode_lastpredefined )) {
201+
ompi_mpi_errcode_init();
202+
}
203+
131204
if (errnum >= 0) {
132205
err = (ompi_mpi_errcode_t *)opal_pointer_array_get_item(&ompi_mpi_errcodes, errnum);
133206
/* If we get a bogus errcode, return a string indicating that this
@@ -142,59 +215,6 @@ static inline char* ompi_mpi_errnum_get_string (int errnum)
142215
}
143216

144217

145-
/**
146-
* Initialize the error codes
147-
*
148-
* @returns OMPI_SUCCESS Upon success
149-
* @returns OMPI_ERROR Otherwise
150-
*
151-
* Invoked from ompi_mpi_init(); sets up all static MPI error codes,
152-
*/
153-
int ompi_mpi_errcode_init(void);
154-
155-
/**
156-
* Finalize the error codes.
157-
*
158-
* @returns OMPI_SUCCESS Always
159-
*
160-
* Invokes from ompi_mpi_finalize(); tears down the error code array.
161-
*/
162-
int ompi_mpi_errcode_finalize(void);
163-
164-
/**
165-
* Add an error code
166-
*
167-
* @param: error class to which this new error code belongs to
168-
*
169-
* @returns the new error code on SUCCESS (>0)
170-
* @returns OMPI_ERROR otherwise
171-
*
172-
*/
173-
int ompi_mpi_errcode_add (int errclass);
174-
175-
/**
176-
* Add an error class
177-
*
178-
* @param: none
179-
*
180-
* @returns the new error class on SUCCESS (>0)
181-
* @returns OMPI_ERROR otherwise
182-
*
183-
*/
184-
int ompi_mpi_errclass_add (void);
185-
186-
/**
187-
* Add an error string to an error code
188-
*
189-
* @param: error code for which the string is defined
190-
* @param: error string to add
191-
* @param: length of the string
192-
*
193-
* @returns OMPI_SUCCESS on success
194-
* @returns OMPI_ERROR on error
195-
*/
196-
int ompi_mpi_errnum_add_string (int errnum, const char* string, int len);
197-
198218
END_C_DECLS
199219

200220
#endif /* OMPI_MPI_ERRCODE_H */

ompi/mpi/c/error_class.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,22 @@ int MPI_Error_class(int errorcode, int *errorclass)
4242
OPAL_CR_NOOP_PROGRESS();
4343

4444
if ( MPI_PARAM_CHECK ) {
45-
OMPI_ERR_INIT_FINALIZE(FUNC_NAME);
46-
4745
if ( ompi_mpi_errcode_is_invalid(errorcode)) {
48-
return OMPI_ERRHANDLER_INVOKE(MPI_COMM_WORLD, MPI_ERR_ARG,
49-
FUNC_NAME);
46+
/* If we have an error, the action that we take depends on
47+
whether we're currently (after MPI_Init and before
48+
MPI_Finalize) or not */
49+
int32_t state = ompi_mpi_state;
50+
if (state >= OMPI_MPI_STATE_INIT_COMPLETED &&
51+
state < OMPI_MPI_STATE_FINALIZE_PAST_COMM_SELF_DESTRUCT) {
52+
return OMPI_ERRHANDLER_INVOKE(MPI_COMM_WORLD, MPI_ERR_ARG,
53+
FUNC_NAME);
54+
} else {
55+
/* We have no MPI object here so call ompi_errhandle_invoke
56+
* directly */
57+
return ompi_errhandler_invoke(NULL, NULL, -1,
58+
ompi_errcode_get_mpi_code(MPI_ERR_ARG),
59+
FUNC_NAME);
60+
}
5061
}
5162
}
5263

ompi/mpi/c/error_string.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,22 @@ int MPI_Error_string(int errorcode, char *string, int *resultlen)
4747
OPAL_CR_NOOP_PROGRESS();
4848

4949
if ( MPI_PARAM_CHECK ) {
50-
OMPI_ERR_INIT_FINALIZE(FUNC_NAME);
51-
5250
if ( ompi_mpi_errcode_is_invalid(errorcode)) {
53-
return OMPI_ERRHANDLER_INVOKE(MPI_COMM_WORLD, MPI_ERR_ARG,
54-
FUNC_NAME);
51+
/* If we have an error, the action that we take depends on
52+
whether we're currently (after MPI_Init and before
53+
MPI_Finalize) or not */
54+
int32_t state = ompi_mpi_state;
55+
if (state >= OMPI_MPI_STATE_INIT_COMPLETED &&
56+
state < OMPI_MPI_STATE_FINALIZE_PAST_COMM_SELF_DESTRUCT) {
57+
return OMPI_ERRHANDLER_INVOKE(MPI_COMM_WORLD, MPI_ERR_ARG,
58+
FUNC_NAME);
59+
} else {
60+
/* We have no MPI object here so call ompi_errhandle_invoke
61+
* directly */
62+
return ompi_errhandler_invoke(NULL, NULL, -1,
63+
ompi_errcode_get_mpi_code(MPI_ERR_ARG),
64+
FUNC_NAME);
65+
}
5566
}
5667
}
5768

0 commit comments

Comments
 (0)