Actual source code: err.c

  1: /*
  2:       Code that allows one to set the error handlers
  3:       Portions of this code are under:
  4:       Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved.
  5: */
  6: #include <petsc/private/petscimpl.h>

  8: typedef struct _EH *EH;
  9: struct _EH {
 10:   PetscErrorCode (*handler)(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *);
 11:   PetscCtx ctx;
 12:   EH       previous;
 13: };

 15: /* This is here to allow the traceback error handler (or potentially other error handlers)
 16:    to certify that PETSCABORT is being called on all MPI processes, and that it should be possible to call
 17:    MPI_Finalize() and exit().  This should only be used when `PetscCIEnabledPortabeErrorOutput == PETSC_TRUE`
 18:    to allow testing of error messages.  Do not rely on this for clean exit in production. */
 19: PetscBool petscabortmpifinalize = PETSC_FALSE;

 21: static EH eh = NULL;

 23: /*@
 24:   PetscEmacsClientErrorHandler - Error handler that uses the emacsclient program to
 25:   load the file where the error occurred. Then calls the "previous" error handler.

 27:   Not Collective, No Fortran Support

 29:   Input Parameters:
 30: + comm - communicator over which error occurred
 31: . line - the line number of the error (usually indicated by `__LINE__` in the calling routine)
 32: . file - the file in which the error was detected (usually indicated by `__FILE__` in the calling routine)
 33: . fun  - the function name of the calling routine
 34: . mess - an error text string, usually just printed to the screen
 35: . n    - the generic error number
 36: . p    - `PETSC_ERROR_INITIAL` indicates this is the first time the error handler is being called while `PETSC_ERROR_REPEAT` indicates it was previously called
 37: - ctx  - error handler context

 39:   Options Database Key:
 40: . -on_error_emacs machinename - will contact machinename to open the Emacs client there

 42:   Level: developer

 44:   Note:
 45:   You must put (server-start) in your .emacs file for the emacsclient software to work

 47:   Developer Note:
 48:   Since this is an error handler it cannot call `PetscCall()`; thus we just return if an error is detected.
 49:   But some of the functions it calls do perform error checking that may not be appropriate in a error handler call.

 51: .seealso: `PetscError()`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`,
 52:           `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscReturnErrorHandler()`,
 53:           `PetscErrorType`, `PETSC_ERROR_INITIAL`, `PETSC_ERROR_REPEAT`, `PetscErrorCode`
 54:  @*/
 55: PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, PetscCtx ctx)
 56: {
 57:   PetscErrorCode ierr;
 58:   char           command[PETSC_MAX_PATH_LEN];
 59:   const char    *pdir;
 60:   FILE          *fp;

 62:   ierr = PetscGetPetscDir(&pdir);
 63:   if (ierr) return ierr;
 64:   ierr = PetscSNPrintf(command, PETSC_STATIC_ARRAY_LENGTH(command), "cd %s; emacsclient --no-wait +%d %s\n", pdir, line, file);
 65:   if (ierr) return ierr;
 66: #if PetscDefined(HAVE_POPEN)
 67:   ierr = PetscPOpen(MPI_COMM_WORLD, (char *)ctx, command, "r", &fp);
 68:   if (ierr) return ierr;
 69:   ierr = PetscPClose(MPI_COMM_WORLD, fp);
 70:   if (ierr) return ierr;
 71: #else
 72:   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine");
 73: #endif
 74:   ierr = PetscPopErrorHandler();
 75:   if (ierr) return ierr; /* remove this handler from the stack of handlers */
 76:   if (!eh) {
 77:     ierr = PetscTraceBackErrorHandler(comm, line, fun, file, n, p, mess, NULL);
 78:     if (ierr) return ierr;
 79:   } else {
 80:     ierr = (*eh->handler)(comm, line, fun, file, n, p, mess, eh->ctx);
 81:     if (ierr) return ierr;
 82:   }
 83:   return PETSC_SUCCESS;
 84: }

 86: /*@
 87:   PetscPushErrorHandler - Sets a routine to be called on detection of errors.

 89:   Not Collective, No Fortran Support

 91:   Input Parameters:
 92: + handler - error handler routine
 93: - ctx     - optional handler context that contains information needed by the handler (for
 94:             example file pointers for error messages etc.)

 96:   Calling sequence of `handler`:
 97: + comm - communicator over which error occurred
 98: . line - the line number of the error (usually indicated by `__LINE__` in the calling routine)
 99: . file - the file in which the error was detected (usually indicated by `__FILE__` in the calling routine)
100: . fun  - the function name of the calling routine
101: . n    - the generic error number (see list defined in include/petscerror.h)
102: . p    - `PETSC_ERROR_INITIAL` if error just detected, otherwise `PETSC_ERROR_REPEAT`
103: . mess - an error text string, usually just printed to the screen
104: - ctx  - the error handler context

106:   Options Database Keys:
107: + -on_error_attach_debugger [noxterm,][(gdb|lldb)] - starts up the debugger if an error occurs
108: - -on_error_abort                                  - aborts the program if an error occurs

110:   Level: intermediate

112:   Note:
113:   The currently available PETSc error handlers include `PetscTraceBackErrorHandler()`,
114:   `PetscAttachDebuggerErrorHandler()`, `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, and `PetscReturnErrorHandler()`.

116:   Fortran Note:
117:   You can only push a single error handler from Fortran before popping it.

119: .seealso: `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscAbortErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscPushSignalHandler()`,
120:           `PetscErrorType`, `PETSC_ERROR_INITIAL`, `PETSC_ERROR_REPEAT`, `PetscErrorCode`
121: @*/
122: PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, PetscCtx ctx), PetscCtx ctx)
123: {
124:   EH neweh;

126:   PetscFunctionBegin;
127:   PetscCall(PetscNew(&neweh));
128:   if (eh) neweh->previous = eh;
129:   else neweh->previous = NULL;
130:   neweh->handler = handler;
131:   neweh->ctx     = ctx;
132:   eh             = neweh;
133:   PetscFunctionReturn(PETSC_SUCCESS);
134: }

136: /*@
137:   PetscPopErrorHandler - Removes the latest error handler that was
138:   pushed with `PetscPushErrorHandler()`.

140:   Not Collective

142:   Level: intermediate

144: .seealso: `PetscPushErrorHandler()`
145: @*/
146: PetscErrorCode PetscPopErrorHandler(void)
147: {
148:   EH tmp;

150:   PetscFunctionBegin;
151:   if (!eh) PetscFunctionReturn(PETSC_SUCCESS);
152:   tmp = eh;
153:   eh  = eh->previous;
154:   PetscCall(PetscFree(tmp));
155:   PetscFunctionReturn(PETSC_SUCCESS);
156: }

158: /*@
159:   PetscReturnErrorHandler - Error handler that causes a return without printing an error message.

161:   Not Collective, No Fortran Support

163:   Input Parameters:
164: + comm - communicator over which error occurred
165: . line - the line number of the error (usually indicated by `__LINE__` in the calling routine)
166: . fun  - the function name
167: . file - the file in which the error was detected (usually indicated by `__FILE__` in the calling routine)
168: . mess - an error text string, usually just printed to the screen
169: . n    - the generic error number
170: . p    - `PETSC_ERROR_INITIAL` indicates this is the first time the error handler is being called while `PETSC_ERROR_REPEAT` indicates it was previously called
171: - ctx  - error handler context

173:   Level: developer

175:   Notes:
176:   Users do not directly employ this routine

178:   Use `PetscPushErrorHandler()` to set the desired error handler.  The
179:   currently available PETSc error handlers include `PetscTraceBackErrorHandler()`,
180:   `PetscAttachDebuggerErrorHandler()`, and `PetscAbortErrorHandler()`.

182: .seealso: `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscError()`, `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`, `PetscTraceBackErrorHandler()`,
183:           `PetscAttachDebuggerErrorHandler()`, `PetscEmacsClientErrorHandler()`,
184:           `PetscErrorType`, `PETSC_ERROR_INITIAL`, `PETSC_ERROR_REPEAT`, `PetscErrorCode`
185:  @*/
186: PetscErrorCode PetscReturnErrorHandler(MPI_Comm comm, int line, const char *fun, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, PetscCtx ctx)
187: {
188:   (void)comm;
189:   (void)line;
190:   (void)fun;
191:   (void)file;
192:   (void)p;
193:   (void)mess;
194:   (void)ctx;
195:   return n;
196: }

198: static char PetscErrorBaseMessage[1024];
199: /*
200:        The numerical values for these are defined in include/petscerror.h; any changes
201:    there must also be made here
202: */
203: static const char *PetscErrorStrings[] = {/*55 */ "Out of memory",
204:                                           "No support for this operation for this object type",
205:                                           "No support for this operation on this system",
206:                                           /*58 */ "Operation done in wrong order",
207:                                           /*59 */ "Signal received",
208:                                           /*60 */ "Nonconforming object sizes",
209:                                           "Argument aliasing not permitted",
210:                                           "Invalid argument",
211:                                           /*63 */ "Argument out of range",
212:                                           "Corrupt argument: https://petsc.org/release/faq/#valgrind",
213:                                           "Unable to open file",
214:                                           "Read from file failed",
215:                                           "Write to file failed",
216:                                           "Invalid pointer",
217:                                           /*69 */ "Arguments must have same type",
218:                                           /*70 */ "Attempt to use a pointer that does not point to a valid accessible location",
219:                                           /*71 */ "Zero pivot in LU factorization: https://petsc.org/release/faq/#zeropivot",
220:                                           /*72 */ "Floating point exception",
221:                                           /*73 */ "Object is in wrong state",
222:                                           "Corrupted PETSc object",
223:                                           "Arguments are incompatible",
224:                                           "Error in external library",
225:                                           /*77 */ "PETSc has generated inconsistent data",
226:                                           "Memory corruption: https://petsc.org/release/faq/#valgrind",
227:                                           "Unexpected data in file",
228:                                           /*80 */ "Arguments must have same communicators",
229:                                           /*81 */ "Zero pivot in Cholesky factorization: https://petsc.org/release/faq/#zeropivot",
230:                                           "",
231:                                           "",
232:                                           "Overflow in integer operation: https://petsc.org/release/faq/#64-bit-indices",
233:                                           /*85 */ "Null argument, when expecting valid pointer",
234:                                           /*86 */ "Unknown type. Check for miss-spelling or missing package: https://petsc.org/release/install/install/#external-packages",
235:                                           /*87 */ "MPI library at runtime is not compatible with MPI used at compile time",
236:                                           /*88 */ "Error in system call",
237:                                           /*89 */ "Object Type not set: https://petsc.org/release/faq/#object-type-not-set",
238:                                           /*90 */ "",
239:                                           /*   */ "",
240:                                           /*92 */ "See https://petsc.org/release/overview/linear_solve_table/ for possible LU and Cholesky solvers",
241:                                           /*93 */ "You cannot overwrite this option since that will conflict with other previously set options",
242:                                           /*94 */ "Example/application run with number of MPI ranks it does not support",
243:                                           /*95 */ "Missing or incorrect user input",
244:                                           /*96 */ "GPU resources unavailable",
245:                                           /*97 */ "GPU error",
246:                                           /*98 */ "General MPI error",
247:                                           /*99 */ "PetscError() incorrectly returned an error code of 0",
248:                                           /*   */ "",
249:                                           /*101*/ "Unhandled Python Exception",
250:                                           NULL};

252: /*@
253:   PetscErrorMessage - Returns the text string associated with a PETSc error code.

255:   Not Collective, No Fortran Support

257:   Input Parameter:
258: . errnum - the error code

260:   Output Parameters:
261: + text     - the error message (`NULL` if not desired)
262: - specific - the specific error message that was set with `SETERRQ()` or
263:              `PetscError()`. (`NULL` if not desired)

265:   Level: developer

267: .seealso: `PetscErrorCode`, `PetscPushErrorHandler()`, `PetscAttachDebuggerErrorHandler()`,
268:           `PetscError()`, `SETERRQ()`, `PetscCall()`, `PetscAbortErrorHandler()`,
269:           `PetscTraceBackErrorHandler()`
270: @*/
271: PetscErrorCode PetscErrorMessage(PetscErrorCode errnum, const char *text[], const char *specific[])
272: {
273:   PetscFunctionBegin;
274:   if (text) {
275:     if (errnum > PETSC_ERR_MIN_VALUE && errnum < PETSC_ERR_MAX_VALUE) {
276:       size_t len;

278:       *text = PetscErrorStrings[errnum - PETSC_ERR_MIN_VALUE - 1];
279:       PetscCall(PetscStrlen(*text, &len));
280:       if (!len) *text = NULL;
281:     } else if (errnum == PETSC_ERR_BOOLEAN_MACRO_FAILURE) {
282:       /* this "error code" arises from failures in boolean macros, where the || operator is
283:          used to short-circuit the macro call in case of error. This has the side effect of
284:          "returning" either 0 (PETSC_SUCCESS) or 1 (PETSC_ERR_UNKNONWN):

286:          #define PETSC_FOO(x) ((PetscErrorCode)(PetscBar(x) || PetscBaz(x)))

288:          If PetscBar() fails (returns nonzero) PetscBaz() is not executed but the result of
289:          this expression is boolean false, hence PETSC_ERR_UNNOWN
290:        */
291:       *text = "Error occurred in boolean short-circuit in macro";
292:     } else {
293:       *text = NULL;
294:     }
295:   }
296:   if (specific) *specific = PetscErrorBaseMessage;
297:   PetscFunctionReturn(PETSC_SUCCESS);
298: }

300: #if PetscDefined(CLANGUAGE_CXX)
301:   /* C++ exceptions are formally not allowed to propagate through extern "C" code. In practice, far too much software
302:  * would be broken if implementations did not handle it in some common cases. However, keep in mind
303:  *
304:  *   Rule 62. Don't allow exceptions to propagate across module boundaries
305:  *
306:  * in "C++ Coding Standards" by Sutter and Alexandrescu. (This accounts for part of the ongoing C++ binary interface
307:  * instability.) Having PETSc raise errors as C++ exceptions was probably misguided and should eventually be removed.
308:  *
309:  * Here is the problem: You have a C++ function call a PETSc function, and you would like to maintain the error message
310:  * and stack information from the PETSc error. You could make everyone write exactly this code in their C++, but that
311:  * seems crazy to me.
312:  */
313:   #include <sstream>
314:   #include <stdexcept>
315: static void PetscCxxErrorThrow()
316: {
317:   if (eh && eh->ctx) {
318:     std::ostringstream *msg;
319:     msg = (std::ostringstream *)eh->ctx;
320:     throw std::runtime_error(msg->str());
321:   } else throw std::runtime_error("Error detected in C PETSc");
322: }
323: #endif

325: /*@
326:   PetscError - Routine that is called when an error has been detected, usually called through the macro `SETERRQ`(`PETSC_COMM_SELF`,)` or by `PetscCall()`.

328:   Collective

330:   Input Parameters:
331: + comm - communicator over which error occurred.  ALL MPI processes of this communicator MUST call this routine
332: . line - the line number of the error (usually indicated by `__LINE__` in the calling routine)
333: . func - the function name in which the error was detected
334: . file - the file in which the error was detected (usually indicated by `__FILE__` in the calling routine)
335: . n    - the generic error number
336: . p    - `PETSC_ERROR_INITIAL` indicates the error was initially detected, `PETSC_ERROR_REPEAT` indicates this is a traceback from a previously detected error
337: - mess - formatted message string - aka printf

339:   Options Database Keys:
340: + -error_output_stdout - output the error messages to `stdout` instead of the default `stderr`
341: - -error_output_none   - do not output the error messages

343:   Level: intermediate

345:   Notes:
346:   PETSc error handling is done with error return codes. A non-zero return indicates an error
347:   was detected. The return-value of this routine is what is ultimately returned by
348:   `SETERRQ()`.

350:   Numerical errors (potential divide by zero, for example) are not managed by the
351:   error return codes; they are managed via, for example, `KSPGetConvergedReason()` that
352:   indicates if the solve was successful or not. The option `-ksp_error_if_not_converged`, for
353:   example, turns numerical failures into hard errors managed via `PetscError()`.

355:   PETSc provides a rich supply of error handlers, see the list below, and users can also
356:   provide their own error handlers.

358:   If the user sets their own error handler (via `PetscPushErrorHandler()`) they may return any
359:   arbitrary value from it, but are encouraged to return nonzero values. If the return value is
360:   zero, `SETERRQ()` will ignore the value and return `PETSC_ERR_RETURN` (a nonzero value)
361:   instead.

363:   Most users need not directly use this routine and the error handlers, but can instead use
364:   the simplified interface `PetscCall()` or `SETERRQ()`.

366:   Fortran Note:
367:   This routine is used differently from Fortran
368: .vb
369:   PetscError(MPI_Comm comm, PetscErrorCode n, PetscErrorType p, char *message)
370: .ve

372:   Developer Note:
373:   Since this is called after an error condition it should not be calling any error handlers (currently it ignores any error codes)
374:   BUT this routine does call regular PETSc functions that may call error handlers, this is problematic and could be fixed by never calling other PETSc routines
375:   but this annoying.

377: .seealso: `PetscErrorCode`, `PetscPushErrorHandler()`, `PetscPopErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscAbortErrorHandler()`, `PetscMPIAbortErrorHandler()`,
378:           `PetscReturnErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscEmacsClientErrorHandler()`,
379:           `SETERRQ()`, `PetscCall()`, `CHKMEMQ`, `PetscErrorMessage()`, `PETSCABORT()`, `PetscErrorType`, `PETSC_ERROR_INITIAL`, `PETSC_ERROR_REPEAT`
380: @*/
381: PetscErrorCode PetscError(MPI_Comm comm, int line, const char *func, const char *file, PetscErrorCode n, PetscErrorType p, const char *mess, ...)
382: {
383:   va_list        Argp;
384:   size_t         fullLength;
385:   char           buf[2048], *lbuf = NULL;
386:   PetscBool      ismain;
387:   PetscErrorCode ierr;

389:   if (!PetscErrorHandlingInitialized) return n;
390:   if (comm == MPI_COMM_NULL) comm = PETSC_COMM_SELF;

392:   /* Compose the message evaluating the print format */
393:   if (mess) {
394:     va_start(Argp, mess);
395:     (void)PetscVSNPrintf(buf, 2048, mess, &fullLength, Argp);
396:     va_end(Argp);
397:     lbuf = buf;
398:     if (p == PETSC_ERROR_INITIAL) (void)PetscStrncpy(PetscErrorBaseMessage, lbuf, sizeof(PetscErrorBaseMessage));
399:   }

401:   if (p == PETSC_ERROR_INITIAL && n != PETSC_ERR_MEMC) (void)PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__);

403:   if (!eh) ierr = PetscTraceBackErrorHandler(comm, line, func, file, n, p, lbuf, NULL);
404:   else ierr = (*eh->handler)(comm, line, func, file, n, p, lbuf, eh->ctx);
405:   PetscStackClearTop;

407:   /*
408:       If this is called from the main() routine we abort the program.
409:       We cannot just return because them some MPI processes may continue to attempt to run
410:       while this process simply exits.
411:   */
412:   if (func) {
413:     (void)PetscStrncmp(func, "main", 4, &ismain);
414:     if (ismain) PETSCABORT(comm, ierr);
415:   }
416: #if PetscDefined(CLANGUAGE_CXX)
417:   if (p == PETSC_ERROR_IN_CXX) PetscCxxErrorThrow();
418: #endif
419:   return ierr;
420: }

422: #if PetscDefined(HAVE_CUDA)
423: #include <petscdevice_cuda.h>
424: PETSC_EXTERN const char *PetscCUBLASGetErrorName(cublasStatus_t status)
425: {
426:   switch (status) {
427:   #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */
428:   case CUBLAS_STATUS_SUCCESS:
429:     return "CUBLAS_STATUS_SUCCESS";
430:   case CUBLAS_STATUS_NOT_INITIALIZED:
431:     return "CUBLAS_STATUS_NOT_INITIALIZED";
432:   case CUBLAS_STATUS_ALLOC_FAILED:
433:     return "CUBLAS_STATUS_ALLOC_FAILED";
434:   case CUBLAS_STATUS_INVALID_VALUE:
435:     return "CUBLAS_STATUS_INVALID_VALUE";
436:   case CUBLAS_STATUS_ARCH_MISMATCH:
437:     return "CUBLAS_STATUS_ARCH_MISMATCH";
438:   case CUBLAS_STATUS_MAPPING_ERROR:
439:     return "CUBLAS_STATUS_MAPPING_ERROR";
440:   case CUBLAS_STATUS_EXECUTION_FAILED:
441:     return "CUBLAS_STATUS_EXECUTION_FAILED";
442:   case CUBLAS_STATUS_INTERNAL_ERROR:
443:     return "CUBLAS_STATUS_INTERNAL_ERROR";
444:   case CUBLAS_STATUS_NOT_SUPPORTED:
445:     return "CUBLAS_STATUS_NOT_SUPPORTED";
446:   case CUBLAS_STATUS_LICENSE_ERROR:
447:     return "CUBLAS_STATUS_LICENSE_ERROR";
448:   #endif
449:   default:
450:     return "unknown error";
451:   }
452: }
453: PETSC_EXTERN const char *PetscCUSolverGetErrorName(cusolverStatus_t status)
454: {
455:   switch (status) {
456:   #if (CUDART_VERSION >= 8000) /* At least CUDA 8.0 of Sep. 2016 had these */
457:   case CUSOLVER_STATUS_SUCCESS:
458:     return "CUSOLVER_STATUS_SUCCESS";
459:   case CUSOLVER_STATUS_NOT_INITIALIZED:
460:     return "CUSOLVER_STATUS_NOT_INITIALIZED";
461:   case CUSOLVER_STATUS_INVALID_VALUE:
462:     return "CUSOLVER_STATUS_INVALID_VALUE";
463:   case CUSOLVER_STATUS_ARCH_MISMATCH:
464:     return "CUSOLVER_STATUS_ARCH_MISMATCH";
465:   case CUSOLVER_STATUS_INTERNAL_ERROR:
466:     return "CUSOLVER_STATUS_INTERNAL_ERROR";
467:     #if (CUDART_VERSION >= 9000) /* CUDA 9.0 had these defined on June 2021 */
468:   case CUSOLVER_STATUS_ALLOC_FAILED:
469:     return "CUSOLVER_STATUS_ALLOC_FAILED";
470:   case CUSOLVER_STATUS_MAPPING_ERROR:
471:     return "CUSOLVER_STATUS_MAPPING_ERROR";
472:   case CUSOLVER_STATUS_EXECUTION_FAILED:
473:     return "CUSOLVER_STATUS_EXECUTION_FAILED";
474:   case CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED:
475:     return "CUSOLVER_STATUS_MATRIX_TYPE_NOT_SUPPORTED";
476:   case CUSOLVER_STATUS_NOT_SUPPORTED:
477:     return "CUSOLVER_STATUS_NOT_SUPPORTED ";
478:   case CUSOLVER_STATUS_ZERO_PIVOT:
479:     return "CUSOLVER_STATUS_ZERO_PIVOT";
480:   case CUSOLVER_STATUS_INVALID_LICENSE:
481:     return "CUSOLVER_STATUS_INVALID_LICENSE";
482:     #endif
483:   #endif
484:   default:
485:     return "unknown error";
486:   }
487: }
488: PETSC_EXTERN const char *PetscCUFFTGetErrorName(cufftResult result)
489: {
490:   switch (result) {
491:   case CUFFT_SUCCESS:
492:     return "CUFFT_SUCCESS";
493:   case CUFFT_INVALID_PLAN:
494:     return "CUFFT_INVALID_PLAN";
495:   case CUFFT_ALLOC_FAILED:
496:     return "CUFFT_ALLOC_FAILED";
497:   case CUFFT_INVALID_TYPE:
498:     return "CUFFT_INVALID_TYPE";
499:   case CUFFT_INVALID_VALUE:
500:     return "CUFFT_INVALID_VALUE";
501:   case CUFFT_INTERNAL_ERROR:
502:     return "CUFFT_INTERNAL_ERROR";
503:   case CUFFT_EXEC_FAILED:
504:     return "CUFFT_EXEC_FAILED";
505:   case CUFFT_SETUP_FAILED:
506:     return "CUFFT_SETUP_FAILED";
507:   case CUFFT_INVALID_SIZE:
508:     return "CUFFT_INVALID_SIZE";
509:   case CUFFT_UNALIGNED_DATA:
510:     return "CUFFT_UNALIGNED_DATA";
511:   case CUFFT_INVALID_DEVICE:
512:     return "CUFFT_INVALID_DEVICE";
513:   case CUFFT_NO_WORKSPACE:
514:     return "CUFFT_NO_WORKSPACE";
515:   case CUFFT_NOT_IMPLEMENTED:
516:     return "CUFFT_NOT_IMPLEMENTED";
517:   case CUFFT_NOT_SUPPORTED:
518:     return "CUFFT_NOT_SUPPORTED";
519:   #if PETSC_PKG_CUDA_VERSION_LT(13, 0, 0)
520:   case CUFFT_INCOMPLETE_PARAMETER_LIST:
521:     return "CUFFT_INCOMPLETE_PARAMETER_LIST";
522:   case CUFFT_PARSE_ERROR:
523:     return "CUFFT_PARSE_ERROR";
524:   case CUFFT_LICENSE_ERROR:
525:     return "CUFFT_LICENSE_ERROR";
526:   #endif
527:   default:
528:     return "unknown error";
529:   }
530: }
531: #endif

533: #if PetscDefined(HAVE_HIP)
534: #include <petscdevice_hip.h>
535: PETSC_EXTERN const char *PetscHIPBLASGetErrorName(hipblasStatus_t status)
536: {
537:   switch (status) {
538:   case HIPBLAS_STATUS_SUCCESS:
539:     return "HIPBLAS_STATUS_SUCCESS";
540:   case HIPBLAS_STATUS_NOT_INITIALIZED:
541:     return "HIPBLAS_STATUS_NOT_INITIALIZED";
542:   case HIPBLAS_STATUS_ALLOC_FAILED:
543:     return "HIPBLAS_STATUS_ALLOC_FAILED";
544:   case HIPBLAS_STATUS_INVALID_VALUE:
545:     return "HIPBLAS_STATUS_INVALID_VALUE";
546:   case HIPBLAS_STATUS_ARCH_MISMATCH:
547:     return "HIPBLAS_STATUS_ARCH_MISMATCH";
548:   case HIPBLAS_STATUS_MAPPING_ERROR:
549:     return "HIPBLAS_STATUS_MAPPING_ERROR";
550:   case HIPBLAS_STATUS_EXECUTION_FAILED:
551:     return "HIPBLAS_STATUS_EXECUTION_FAILED";
552:   case HIPBLAS_STATUS_INTERNAL_ERROR:
553:     return "HIPBLAS_STATUS_INTERNAL_ERROR";
554:   case HIPBLAS_STATUS_NOT_SUPPORTED:
555:     return "HIPBLAS_STATUS_NOT_SUPPORTED";
556:   default:
557:     return "unknown error";
558:   }
559: }
560: PETSC_EXTERN const char *PetscHIPSPARSEGetErrorName(hipsparseStatus_t status)
561: {
562:   switch (status) {
563:   case HIPSPARSE_STATUS_SUCCESS:
564:     return "HIPSPARSE_STATUS_SUCCESS";
565:   case HIPSPARSE_STATUS_NOT_INITIALIZED:
566:     return "HIPSPARSE_STATUS_NOT_INITIALIZED";
567:   case HIPSPARSE_STATUS_ALLOC_FAILED:
568:     return "HIPSPARSE_STATUS_ALLOC_FAILED";
569:   case HIPSPARSE_STATUS_INVALID_VALUE:
570:     return "HIPSPARSE_STATUS_INVALID_VALUE";
571:   case HIPSPARSE_STATUS_ARCH_MISMATCH:
572:     return "HIPSPARSE_STATUS_ARCH_MISMATCH";
573:   case HIPSPARSE_STATUS_MAPPING_ERROR:
574:     return "HIPSPARSE_STATUS_MAPPING_ERROR";
575:   case HIPSPARSE_STATUS_EXECUTION_FAILED:
576:     return "HIPSPARSE_STATUS_EXECUTION_FAILED";
577:   case HIPSPARSE_STATUS_INTERNAL_ERROR:
578:     return "HIPSPARSE_STATUS_INTERNAL_ERROR";
579:   case HIPSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED:
580:     return "HIPSPARSE_STATUS_MATRIX_TYPE_NOT_SUPPORTED";
581:   case HIPSPARSE_STATUS_ZERO_PIVOT:
582:     return "HIPSPARSE_STATUS_ZERO_PIVOT";
583:   case HIPSPARSE_STATUS_NOT_SUPPORTED:
584:     return "HIPSPARSE_STATUS_NOT_SUPPORTED";
585:   case HIPSPARSE_STATUS_INSUFFICIENT_RESOURCES:
586:     return "HIPSPARSE_STATUS_INSUFFICIENT_RESOURCES";
587:   default:
588:     return "unknown error";
589:   }
590: }
591: PETSC_EXTERN const char *PetscHIPSolverGetErrorName(hipsolverStatus_t status)
592: {
593:   switch (status) {
594:   case HIPSOLVER_STATUS_SUCCESS:
595:     return "HIPSOLVER_STATUS_SUCCESS";
596:   case HIPSOLVER_STATUS_NOT_INITIALIZED:
597:     return "HIPSOLVER_STATUS_NOT_INITIALIZED";
598:   case HIPSOLVER_STATUS_ALLOC_FAILED:
599:     return "HIPSOLVER_STATUS_ALLOC_FAILED";
600:   case HIPSOLVER_STATUS_MAPPING_ERROR:
601:     return "HIPSOLVER_STATUS_MAPPING_ERROR";
602:   case HIPSOLVER_STATUS_INVALID_VALUE:
603:     return "HIPSOLVER_STATUS_INVALID_VALUE";
604:   case HIPSOLVER_STATUS_EXECUTION_FAILED:
605:     return "HIPSOLVER_STATUS_EXECUTION_FAILED";
606:   case HIPSOLVER_STATUS_INTERNAL_ERROR:
607:     return "HIPSOLVER_STATUS_INTERNAL_ERROR";
608:   case HIPSOLVER_STATUS_NOT_SUPPORTED:
609:     return "HIPSOLVER_STATUS_NOT_SUPPORTED ";
610:   case HIPSOLVER_STATUS_ARCH_MISMATCH:
611:     return "HIPSOLVER_STATUS_ARCH_MISMATCH";
612:   case HIPSOLVER_STATUS_HANDLE_IS_NULLPTR:
613:     return "HIPSOLVER_STATUS_HANDLE_IS_NULLPTR";
614:   case HIPSOLVER_STATUS_INVALID_ENUM:
615:     return "HIPSOLVER_STATUS_INVALID_ENUM";
616:   case HIPSOLVER_STATUS_UNKNOWN:
617:   default:
618:     return "HIPSOLVER_STATUS_UNKNOWN";
619:   }
620: }
621: #endif

623: /*@
624:   PetscMPIErrorString - Given an MPI error code returns the `MPI_Error_string()` appropriately
625:   formatted for displaying with the PETSc error handlers.

627:   Not Collective, No Fortran Support

629:   Input Parameters:
630: + err  - the MPI error code
631: - slen - length of `string`, should be at least as large as `MPI_MAX_ERROR_STRING`

633:   Output Parameter:
634: . string - the MPI error message

636:   Level: developer

638:   Note:
639:   Does not return an error code or do error handling because it may be called from inside an error handler

641: .seealso: `PetscErrorCode` `PetscErrorMessage()`
642: @*/
643: void PetscMPIErrorString(PetscMPIInt err, size_t slen, char *string)
644: {
645:   char        errorstring[MPI_MAX_ERROR_STRING];
646:   PetscMPIInt len;
647:   size_t      j = 0;

649:   MPI_Error_string(err, (char *)errorstring, &len);
650:   for (PetscMPIInt i = 0; i < len && j < slen - 2; i++) {
651:     string[j++] = errorstring[i];
652:     if (errorstring[i] == '\n') {
653:       for (PetscMPIInt k = 0; k < 16 && j < slen - 2; k++) string[j++] = ' ';
654:     }
655:   }
656:   string[j] = 0;
657: }