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>
  7: #include <petscpkg_version.h>

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

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

 22: static EH eh = NULL;

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

 28:   Not Collective, No Fortran Support

 30:   Input Parameters:
 31: + comm - communicator over which error occurred
 32: . line - the line number of the error (usually indicated by `__LINE__` in the calling routine)
 33: . file - the file in which the error was detected (usually indicated by `__FILE__` in the calling routine)
 34: . fun  - the function name of the calling routine
 35: . mess - an error text string, usually just printed to the screen
 36: . n    - the generic error number
 37: . 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
 38: - ctx  - error handler context

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

 43:   Level: developer

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

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

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

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

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

 90:   Not Collective, No Fortran Support

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

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

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

111:   Level: intermediate

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

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

120: .seealso: `PetscPopErrorHandler()`, `PetscAttachDebuggerErrorHandler()`, `PetscAbortErrorHandler()`, `PetscTraceBackErrorHandler()`, `PetscPushSignalHandler()`,
121:           `PetscErrorType`, `PETSC_ERROR_INITIAL`, `PETSC_ERROR_REPEAT`, `PetscErrorCode`
122: @*/
123: 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)
124: {
125:   EH neweh;

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

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

141:   Not Collective

143:   Level: intermediate

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

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

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

162:   Not Collective, No Fortran Support

164:   Input Parameters:
165: + comm - communicator over which error occurred
166: . line - the line number of the error (usually indicated by `__LINE__` in the calling routine)
167: . fun  - the function name
168: . file - the file in which the error was detected (usually indicated by `__FILE__` in the calling routine)
169: . mess - an error text string, usually just printed to the screen
170: . n    - the generic error number
171: . 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
172: - ctx  - error handler context

174:   Level: developer

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

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

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

199: static char PetscErrorBaseMessage[1024];
200: /*
201:        The numerical values for these are defined in include/petscerror.h; any changes
202:    there must also be made here
203: */
204: static const char *PetscErrorStrings[] = {/*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) PETSCABORT(comm, ierr);
416:   }
417: #if PetscDefined(CLANGUAGE_CXX)
418:   if (p == PETSC_ERROR_IN_CXX) PetscCxxErrorThrow();
419: #endif
420:   return ierr;
421: }

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

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

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

628:   Not Collective, No Fortran Support

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

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

637:   Level: developer

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

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

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