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

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

256:   Not Collective, No Fortran Support

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

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

266:   Level: developer

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

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

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

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

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

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

329:   Collective

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

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

344:   Level: intermediate

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

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

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

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

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

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

373:   Developer Note:
374:   Since this is called after an error condition it should not be calling any error handlers (currently it ignores any error codes)
375:   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
376:   but this annoying.

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

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

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

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

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

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

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

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

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

631:   Not Collective, No Fortran Support

633:   Input Parameters:
634: + err  - the MPI error code
635: - slen - length of `string`, should be at least as large as `MPI_MAX_ERROR_STRING`

637:   Output Parameter:
638: . string - the MPI error message

640:   Level: developer

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

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

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